views:

121

answers:

5

Say I have an object that gets some data from HttpPost and some from the database. I think I want to allow the ModelBinder to go to the database/repository for the that data missing from the post. In practice, is this a good or bad idea?

+1  A: 

I would say, no.

Here's why: It would create a dependency on your database for testing your controller actions that would not be easy to abstract out.

Daniel A. White
How so, usually I give the action a fully populated model as a parameter, skipping the model binder entirely in my unit tests? Why should the code under test care how the model is built?
tvanfosson
If your model is being bound from the HttpPost and some from the database and its either going to be bound in the controller/repository/model binder, then wouldnt using the model binder be a clean way of avoiding duplicate functionality that might be required on add AND edit operations?
Jimbo
Regarding: " It would create a dependency on your database for testing your controller actions that would not be easy to abstract out." This is false. You can easily have access to anything inside of your controller inside of your modelbinder, there is no additional dependency your creating. Therefore if your injecting a DAL/Repository/Service into your controller you also have can get it in your Modelbinder.
jfar
The thing that did it for me was testing. That's enough reason to stay away from mixing modelbinders with db access.
Byron Sommardahl
@ByronIf you had to do some database access to populate a parameter to an action method for a unit test then something has gone awry in the universe :)All you should have needed to do is call _controller.SomeAction(new Entity { Param1 = 1, Param2 = 2, Param 3 = 3});. That is, if your action actually needed those values stubbed out for the particular behavior you were testing.This answer provides no real justification, so it shouldn't have been marked as the answer. Doing so sounds more like it was just the first answer that agreed with a predisposed idea you had.
Derek Greer
+2  A: 

I would say a bad idea. The idea of the model binder is that it takes the parameters from the request and creates your model from those. If your model binder, behind the scenes, fills in some of the details from the database this breaks the paradigm. I'd much rather expose the database call in my controller by explicitly fetching the required extra data from the database directly. Note that this could be refactored into a method if used frequently.

tvanfosson
+2  A: 

I think this is perfectly fine and use this technique all the time.

The only arguments against are very pedantic and amount to arguing over philosophy. IMHO you can put "fill in missing posted data" code into you MVC app as a method in your base controller vs. method in you ActionFilter vs method in you ModelBinder. It all depends on who get what responsibility. To me the model binder can do a lot more than simply wire up some properties from posted values.

The reason I love doing database calls in my modelbinder is because it helps clean up your action methods.

    //logic not in modelbinder
    public ActionResult Edit( KittyCat cat )
    {
        DoSomeOrthagonalDatabaseCall( cat );

        return View( new MODEL() );
    }

vs.

    //logic in model binder
    public ActionResult Add( KittyCat cat )
    {
        return View( new MODEL() );
    }
jfar
Except that when someone else comes along who is familiar with MVC, he now gets a nice "happy" WTF moment when he finds that the model is fully populated with data even though it wasn't posted from the database and there is no indication of where it might be coming from. Then he gets to dig through a lot of unrelated code in a completely different section to find that "aha" -- there's a custom model binder here that performs magic, filling in the missing properties, including the one that unintentionally got left off the form. Sorry, but I'd rather make it obvious for the poor sap.
tvanfosson
The "new guy" argument is a pretty poor one. Anybody familiar with asp.net mvc will know to check the model binder if an action method parameter "mysteriously" has some values set that weren't posted with the form. Your criticism is poor considering simply asking another dev. "hey, where are these values coming from?" solves the entire issue. These "new guy" arguments seem to carefully construct a world were experience or sharing of information is non-existent.
jfar
+2  A: 

It violates the way MVC is supposed to work. ModelBinder is for binging Models from the data that comes from the view. Populating missing info from the database is something that is supposed to be handled by the controller. Ideally, it would have same data layer/repository class that it uses to do this.

The reason it should be in the controller is because this code is business logic. The business rules dictate that some data may be missing and thus it must be handled by the brains of the operation, the controller.

Take it a step further, say you want to log in the DB what info the user isn't posting, or catch an exception when getting the missing data and email admins about it. You have to put these in your model binder this way and it gets more and more ugly with the ModelBinder becoming more and more warped from its original purpose.

Basically you want everything but the controller to be as dumb and as specialized as possible, only knowing out how to carry out its specific area of expertise which is purely to assist the controller.

Alistair
Model binder arguments aside, business logic belongs in the domain layer of your application, not in your controllers. You don't want everything but your controllers to be as dumb as possible, in fact you want the exact opposite. Controllers are merely the entry point to the application which translates a user's request into actions performed on domain model objects. I would recommend you go explore Domain-Driven Design concepts and then bring what you've learned there back to MVC. I think you'll see things quite differently.
Derek Greer
What do you mean by business logic? Maybe I am confused in my understanding of it. I'm not saying that the controller is performing low level operations and is thus smart. I am saying the controller decides what to do, but doesn't know how it is done. This deciding of what to do is what I consider business logic. It is dumb in the sense that without the model it is helpless. Do you have a good article on domain driven design?
Alistair
+2  A: 

Upon encountering Model Binders for the first time, my first instinct was that they shouldn't be used to do anything beyond populating a complex type with user input. This thinking may have been the result of some indirect conditioning I picked up from the book I was working through, or from the fact that that's how I'd always done things with previous MVC-based frameworks as well as MVP implementations in the past, but it only took me a few days to consider that I really didn't have any good justification for this thinking.

Model Binders are about translating a user request into a more object-oriented request for the controller to operate upon. If the request data represents a persisted entity, why shouldn't it be passed directly to the controller? Is it because one person says it couples the model binder to the database? Uh, that's just if you're using some 1990's approach to database access. So that isn't a good reason. Is it because another thinks it breaks some paradigm or that they would just rather do it from the controller? Well, that's not really a reason either now is it. Is it because the framework designers didn't mean for you to use them that way? Well, I haven't actually come across anything that indicates this, but even if they did say they didn't mean for us to use them that way, is that a good enough reason?

I'm not saying there isn't necessarily a reason, but thus far I can't think of one. It doesn't violate any principles I'm aware of and upon further reflection it actually seems more object-oriented IMO, so for now I'm gonna keep and open mind on how this new (to me) construct can be leveraged.

Derek Greer