views:

49

answers:

1

I have a a model class that needs access to my repository class (used for DB access).

I have created an interface for my repository and have successfully configured Castle Windsor to inject my the appropriate IRepository-based class into my controllers via a custom ControllerFactory.

I'm having a little more trouble figuring out how to do the same thing with my model.

Does anyone know of a way to use Windsor to inject a dependency into an MVC model?

As an aside, the reason I need Windsor to handle this is because MVC automatically instantiates an instance of my model when data is posted to my controller, and this automatic instantiation doesn't allow for me to pass any constructor parameters.

+1  A: 

You may want to take a look at MVC Contrib's Castle Binder.

However, personally, I think that Models should be simple POCO's, or dumb containers of data, free of any DI. In this approach, it is the Controller's responsibility to read, manipulate and persist data.

Mark Seemann
What about when, for example, a model contains a SelectList of items that have to be pulled from the database?
AlexWalker
IMO, it shouldn't. It should contain an IList<T> (or similar), and it is the Controller's responsibility to fill that list in some way.
Mark Seemann