views:

148

answers:

1

If I have a class similar to this:

public class Person
{
    public string firstName { get; set; }
    public string lastName { get; set; }
    public Pet myPet { get; set; }
}

When I create a custom model binder, the Post from my form will not be sending in a Pet, it would send in data like this: firstName: "myFirstName" lastName: "myLastName" myPet: "myPetsName"

Since the Pet's name is passed in, and not the actual Pet object, the Pet object needs to be retrieved from within the model binder.

My question is, should the model binder be interacting with the Service Layer, the Repository Layer, or should it even be retrieving the Pet? The problem with the Service Layer is that I don't seem to have access to ModelState when initializing the service: ((this.ModelState) gives me an error)

_petService = new PetService(new ModelStateWrapper(this.ModelState));

If I need the model binder to create a Person object, then the Pet would need to be assigned somehow... how am I supposed to do this?

Thanks,
Matt

+1  A: 

What I do is use a view/presentation model instead of binding to business objects. Remember that your users might enter invalid values, and you will need to re-display these invalid values to the user when prompting them to correct their mistakes. But your "real" business objects probably won't accept invalid values. So the view model must allow for any user input, even input the business objects won't accept.

This makes the binding easy. You don't need to hit a repository to bind, only when updating the business object with (valid) user input.

Craig Stuntz
So are you saying you create a PetBinding object with a bunch of public string's?
James S
Right... so if I want to do an Ajax call to a controller method, I shouldn't be binding it? But it makes it so much cleaner doing:public ActionResult Create(Person personToCreate) {}Rather than actually passing the parameters... I know the View needs to accept any input, but as long as the input is validated before the form is submitted it shouldn't be a problem. So What exactly is it you suggest I do? When exactly should I hit a repository? And if the validation is supposed to be in the Service layer then shouldn't I be interacting with that?
Matt
James, not necessarily strings. Any bindable type will do. But the view model is typically "flatter" than the business object since it is structured like the view.
Craig Stuntz
Matt, no, I think you *should* bind to a model. Just not necessarily to a business object directly. Yes, you still validate -- you do it when you apply the changes from the view model onto the business object.
Craig Stuntz
Okay, so you're saying I need to bind to a model that represents a simpler version of the business object, that way I can pass all the information without requiring things like object references?
Matt
Matt, pretty much, though the part about error handling is also important. Here are some links with more details: http://www.martinfowler.com/eaaDev/PresentationModel.htmlhttp://msdn.microsoft.com/en-us/library/dd458859.aspx
Craig Stuntz