views:

42

answers:

1

If you have an Order that references a customer, does the model include the ID of the customer or a copy of the customer object like a value object (thinking DDD)?

I would like to do ths:

public class Order {
    public int ID {get;set;}
    public Customer customer {get;set;}
    ...
}

right now I do this:

public class Order {
    public int ID {get;set;}
    public int customerID {get;set;}
    ...
}

It would be more convenient to include the complete customer object rather than an ID to the View Model passed to the form. Otherwise, I need to figure out how to get the vendor information to the view that the order references by ID.

This also implies that the repository understand how to deal with the customer object that it finds within the order object when they call save (if we select the first option). If we select the second option, we will need to know where in the view model to put it.

It is certain they will select an existing customer. However, it is also certain they may want to change the information in-place on the display form. One could argue to have the controller extract the customer object, submit customer changes separately to the repository, then submit changes to the order, keeping the customerID in the order.

+3  A: 

If you have the following model:

public class Order {
    public int ID {get;set;}
    public Customer Customer {get;set;}
    public object OrderProperty {get;set;}
    ...
}

and the following view (from ViewPage<Order>)

you can do in your view

Html.TextBoxFor(m=>m.OrderProperty);

but also

Html.HiddenFor(m=>m.Customer.Id)
Html.TextBoxFor(m=>m.Customer.Name)

in your controller it will be bound automatically

public ActionResult SomeAction(Order order)
{
    // order.OrderProperty will be bound
    // order.Customer.Id will be bound
    // order.Customer.Name will be bound
}
Gregoire