views:

72

answers:

2

Hi,

When I want to extend an existing Object I create my own and use inherits clause, and works fantastic... problem is when I want to use this new Object instead of the original, the populate part is a "pain".

is there any AUTO POPULATE way of doing this?

Original Object: Customer My Object: CustomerWithGroup

public class CustomerWithGroup : Customer
{
    public CustomerWithGroup() { }
    public string GroupName { get; set; }
    public string Fullname
    {
        get
        {
            return string.Format("{0} {1} {2}", firstname, middlename, lastname).Replace("  ", " ");
        }
    }
}

and I'm populating it like this:

customerswithgroup = new List<CustomerWithGroup>();
string groupname;
foreach (Customer c in customers)
{
    groupname = customergroup.Find(x => x.customer_group_id == c.group_id).customer_group_code;

    customerswithgroups.Add(
        new CustomerWithGroup { 
            GroupName = groupname,
            created_at = c.created_at, customer_id = c.customer_id, default_billing = c.default_billing, default_shipping = c.default_shipping, email = c.email, firstname = c.firstname, group_id = c.group_id, increment_id = c.increment_id, lastname = c.lastname, middlename = c.middlename, password_hash = c.password_hash, prefix = c.prefix, store_id = c.store_id, suffix = c.suffix, taxvat = c.taxvat, updated_at = c.updated_at, website_id = c.website_id });
}

It's a bunch of code! and you can imagine doing this for really big objects!

Ins't there a way of telling, like

Hei object, please load all the base members from this Object!

newCG = new CustomerWithGroup().LoadFromBase(c);
newCG.groupName = "my group";
customerswithgroup.Add( newCG );

Is there any trick we can use?

+1  A: 

You cannot easily do this automatically.

You could make a constructor or method in the derived class that takes an instance of the base class and copies all of the properties. (You'd still need to write it by hand, but you'd only need to write it once)

If you really want to do it automatically, you could use reflection to loop through the properties and make an expression tree that copies them from an original.

However, the best way is to use a has-a relationship, making CustomerWithGroups not inherit Customer and instead have a read-only Customer property of type Customer.

SLaks
humm... Good idea :)
balexandre
Perhaps a frivolous comment : I wonder if the "automatic copying" of internal values that occurs in assigning one struct to another struct (both being instances of the same Struct definition, of course) might be exploited here to copy all the fields (assuming they are all value types).
BillW
@BillW: No; value types cannot have base types.
SLaks
+2  A: 

No, there is no easy way. You could use reflection and write a bunch of code to do that, but it would be a lot of work.

In my opinion you should rethink your design. Inheritance is not a good solution in this case, composition here is better, like so:

public class CustomerWithGroups
{
    public CustomerWithGroups(Customer c) { Customer = c; }

    public Customer Customer { get; private set; }

    public string GroupName { get; set; }
    public string Fullname
    {
        get
        {
            return string.Format("{0} {1} {2}", Customer.firstname, Customer.middlename, Customer.lastname).Replace("  ", " ");
        }
    }
}
silk
I choose your as Accept Answer as you have code and Slaks have more than 24000 points :D (even if I didn't need, it's always good to show a code example)
balexandre