开发者

Sharp Architecture inheritance problem

my problem is with inheritance.

I have a Actor class

using System.Collections.Generic; using SharpArch.Core; using SharpArch.Core.DomainModel;

namespace ProTeria.NET.Common.Domain { public class Actor : Entity { public Actor() { Init(); }

    private void Init()
    {
        Addresses = new List<Address>();
    }

    public virtual Account Account { get; set; }
    public virtual string Number { get; set; }
    public virtual string Telephone { get; set; }
    public virtual string Fax { get; set; }
    public virtual string Email { get; set; }
    public virtual string IdNumber { get; set; }
    public virtual CountryCode Country { get; set; } 

    public virtual IList<Address> Addresses { get; set; }

    public virtual void AddAddress(Address address)
    {
        addr开发者_如何学Cess.Actor = this;
        Addresses.Add(address);
    }
}

}

and also two derived classes,

using System.Collections.Generic; using SharpArch.Core; using SharpArch.Core.DomainModel;

namespace ProTeria.NET.Common.Domain { public class Company : Actor { private string _companyName;

    protected Company()
    {
         Init();
    }

    public Company(string companyName)
        : this()
    {
        Check.Require(!string.IsNullOrEmpty(companyName) && companyName.Trim() != string.Empty,
                      "Company name must be provided");
        _companyName = companyName;
    }

    private void Init()
    {
        Employees = new List<Employee>();
    }

    public virtual Account Account { get; set; }
    public virtual string EoriNumber { get; set; }

    [DomainSignature]
    public virtual string CompanyName
    {
        get { return _companyName; }
        protected set { _companyName = value; }
    }

    public virtual CompanyNcts CompanyNcts {get;set;} 
    public virtual IList<Employee> Employees { get; set; }

    public virtual void AddEmployee(Employee employee)
    {
        employee.Company = this;
        Employees.Add(employee);
    }
}

} and

using SharpArch.Core; using SharpArch.Core.DomainModel;

namespace ProTeria.NET.Common.Domain { public class Contact : Actor { private string _foreName;

    protected Contact()
    {
    }

    public Contact(string foreName)
        : this()
    {
        Check.Require(!string.IsNullOrEmpty(foreName) && foreName.Trim() != string.Empty,
                      "Contact first name must be provided");
        _foreName = foreName;
    }

    public virtual Account Account { get; set; }


    [DomainSignature]
    public virtual string ForeName
    {
        get { return _foreName; }
        protected set { _foreName = value; }
    }

    public virtual string Surname { get; set; }
    public virtual string Mobile { get; set; }

}

}

When I call

Actor actor = _actorRepository.Get(id);

it works fine. And I get the correct type of actor -> company or contact

The problem is when I embed the Actor class with another class as below.

using System; using System.Collections.Generic; using NHibernate.Validator.Constraints; using SharpArch.Core; using SharpArch.Core.DomainModel;

namespace ProTeria.NET.Common.Domain { public class Article : Entity { private string _number;

    public Article(string number, Account account)
        : this()
    {
        Check.Require(!string.IsNullOrEmpty(number)
                      && number.Trim() != String.Empty,
                      "ArticleNumber must be provided");

        Check.Require((account != null), "Account must be provided");

        _account = account;

        _number = number;
    }

    protected Article()
    {
         Init();
    }


    private void Init()
    {
        Descriptions = new List<ArticleDescription>();
        UnitPrices = new List<ArticlePrice>();
    }

    private Account _account;
    public virtual Account Account
    {
        get { return _account; }
        set { _account = value; }
    }

    [DomainSignature]
    [NotNull, NotEmpty]
    public virtual string Number
    {
        get { return _number; }
        protected set { _number = value; }
    }

    public virtual Actor Sender { get; set; }
    public virtual CurrencyCode CurrencyCode { get; set; }
    [NotNull]
    public virtual LanguageCode LanguageCode { get; set; }
    public virtual ArticleNcts ArticleNcts { get; set; }
    public virtual ArticleDe ArticleDe { get; set; }
    public virtual ArticleSe ArticleSe { get; set; }
    public virtual ArticleNo ArticleNo { get; set; }
    public virtual CountryCode CountryCode { get; set; }
    public virtual HsCode HsCode { get; set; }
    public virtual double GrossWeight { get; set; }
    public virtual double NetWeight { get; set; }
    public virtual string ExportCode { get; set; }
    public virtual string ImportCode { get; set; }
    public virtual string TaricCode { get; set; }
    public virtual IList<ArticleDescription> Descriptions { get; set; }
    public virtual IList<ArticlePrice> UnitPrices { get; set; }

    public virtual void AddArticleDescription(ArticleDescription articleDescription)
    {
        Descriptions.Add(articleDescription);
    }

    public virtual void AddArticlePrice(ArticlePrice articlePrice)
    {
        UnitPrices.Add(articlePrice);
    }
}

}

Then if I call

Article article = _articleRepository.Get(articleId);

article.Sender won't be mapped correctly, it is in its base type, not the derived type.

I am not sure if I am doing something wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜