Sunday, February 5, 2012

MVC Model design

So, I've been toying about with MVC, and have a parent model, that has a list of children associated with it. Originally, I had my list of child objects loading up in the model though a lazy load. Big mistake, realised this through my integration tests. Now in my model, I simply declare my list of child objects like this:


And I populate it in my controller. Something like this:



Where do I put my AddChild functionality?  In the model, like this...




Or do I shove it in the Controller?


2 comments:

  1. I posted a link to the blog on my facebook page and got a response from Stu Leake:

    not a good idea to put that in your controller. in fact looking at your code you should never expose your controller to the inner workings of your model. that's just plain bad !

    you've got dependencies between your objects and from a design perspective they're too tightly coupled. Learn to prgram with interfaces !

    Have a look at this article, it'll explain what you need to do to resolve this ...

    http://msdn.microsoft.com/en-us/library/aa973811.aspx

    ReplyDelete
  2. I also posted in on Linked In and got a response from Alan Watson:

    'm assuming EF4.1 code first goodness! and in the example I've illustrated DI with repository layer only - clearly you might want to think about DI with the context too, carry the theme all the way down the dependency tree...

    I've added an implementation of a UnityDependencyResolver and how to wire this in to the global asax - by no means the only way to address either!

    Finally, I've showed a viewmodel too - but a very basic example of it!



    BW - Unfortunately, it won't let me post the code example. I'm going to go away, read and digest the Article from Stu, and the code suggestion from Alan, and blog about it at a later date. Watch this space! :-)

    ReplyDelete