views:

175

answers:

1

This is a general model binding question that applies to MVC 1 & 2. I'm wondering if MVC2 would be better for this, but here's my question:

I have a fairly complex model:

public interface IEvent
  public int Id
  public string Title
  public List<EventContact> Contacts
  public List<EventDatesLocations> DatesLocations

public class EventDatesLocations
  public int Id
  public DateTime StartDate
  public DateTime EndDate
  public List<EventLocation> Locations

I have a custom model binder for my IEvent class that does basically all the binding for IEvent. I call the default model binder for binding the Contacts list, which works well.

I am ready to start trying to bind the DatesLocations stuff, but I want to make sure I'm doing things correctly.

Overall, I'm not sure I have all the details on model binding understood. Would it be better to have multiple model binders for the lists within IEvent or just have one IEvent model binder (as I'm doing now) which calls the default binder for the lists it needs.

How do the experts do it? :P

A: 

Take a look at this question to see some nested model binding.

Back to this question, you mention you need to do some binding on different implementations of IEvent, but then you want to bind specific properties depending on what type implements IEvent. I would say this is generally bad. Why? Because you are trying to get one model binder knowing about an Interface knowing about specific implementations of it.

If you require multiple classes to implement IEvent (for some reason) then you will probably need custom binders for each (if going down this route, although as they will be concrete types then the default binder should work), or you need to expand the IEvent interface to include the properties you will be setting on all.

Remember Interfaces are there to define a contract that needs to be implemented ... you can't work with objects which implement it and then check for other properties depending on what type they are :-)

WestDiscGolf