开发者

Containership Question

Consider this example:

namespace ValueObjects
{
  public class User
  {
    public string UserCode { get; set; }
    public string UserName { get; set; }
  }

  public class Company
  {
    public string CompanyCode { get; set; }
  }
}

Now, I want the User class to have a CompanyCode property.. the first obvious solution is just simply put a CompanyCode property on the User class

public class User
{
  public string UserCode { get; set; }
  public string UserName { get; set; }
  public string CompanyCode { get; set; }
}

Now the problem here is redunduncy because there's already a property CompanyCode to the Company Class, is there any way that User will be able to just use Company's CompanyCode property WITHOUT inheriting the Company class (problem here is C# doesnt allow multiple inheritance of classes) or using an ICompany interface (but interface is a pain in Value Objects). Im thinking about just contain the Company Class to the User class but i will use only the propery CompanyCode of it.

something like this..

  public class User
  {
    public string UserCode { get; set; }
    public 开发者_如何学Cstring UserName { get; set; }
    private Company company;
    public string CompanyCode { get { company.CompanyCode } set {company.CompanyCode }

  }

but then, it's no difference to just adding a string property CompanyCode.. Need your advice guys. Thanks in advance..


Your solution is valid. You want containment; a "has a" relationship and not a "is-a" relationship (inheritance). Another option would be a

public Company UserCompany;

which would let the User object access other public properties on the Company object if they are added in the future.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜