views:

39

answers:

1

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 string 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..

+2  A: 

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.

Jim Ecker
The problem is, when I make the Company class a property of the User - public Company UserCompany { get; set; } - and put the User in a Collection (ex. Collection<User> user) and bind it to a Gridview. The Gridview's Boundfield wont let you access the property of the contained class...
CSharpNoob
@CsharpNoob indeed, objectdatasource/asp.net simple databinding cannot walk trough complex classses. Take a look here: http://www.codeproject.com/KB/database/CustomBindableObject.aspx
Caspar Kleijne
Is there any solution about this? I'm using DTOs and its nasty, to see too many CompanyCode property in my value objects..
CSharpNoob
@CSharpNoob, I know I am going to get slated for this. But with WinForms I pretty much avoided data binding. I would just loop through the list and add the items to the control, except for the simple cases. In this regard WPF and related technologies are much more powerful and it is wise to take full advantage of the data binding infrastructure, but for WinForms it just did not always work as smoothly as one would hope.
Chris Taylor
I thought you could access the property of a contained class like "User.UserCompany.CompanyCode". I thought that worked (at least with DataGrids it did). If it doesn't, you can use templates (ItemTemplates) http://msdn.microsoft.com/en-us/library/aa479353.aspx
Jim Ecker
Also, to handle any Null cases; you would want UserCompany get property to check for null ( if (_userCompany == null) return ""; etc)
Jim Ecker
I tried it using Gridview's Boundfield to no avail.. It works in Itemtemplates though. I dont want to rework my millions of boundfield though :)
CSharpNoob