views:

235

answers:

7

I would like to inject a dependency into an ASP.NET MVC model, but I can't figure out where in the pipeline to do the injection.

It's very straightforward with a ControllerFactory, but not nearly as much when dealing with models.

A: 

You can find a reasonable How-To on Shiju Vargheses Blog: ASP.NET MVC Tip: Dependency Injection with Unity Application Block

JRoppert
A: 

usually i inject dependencies in the controller like this

PersonController(IPersonRepository r)
{
\\ constrtuctor code
}

in the models probably when need some instance of something that inherits an interface you do something like this :

var r = container.Resolve<IPersonRepository>();
Omu
As I mentioned, I was trying to inject into a Model. The model is instantiated by the MVC framework, so I need to be able to hook into it at some point.
AlexWalker
I have found it useful to instantiate my own Model objects (with whatever injections they require) and pass them in to UpdateModel/TryUpdateModel, rather than relying on the auto-wiring/auto-binding that occurs when you specify these as part of your action method's parameters... However, using a Service Locator (as you mentioned elsewhere) means you wouldn't have to worry about this...
Funka
if you want to inject in the model that is passed to the view than you must create a custom model class kind off, look here http://stackoverflow.com/questions/1361092/asp-net-mvc-viewmodel-pattern i do here this kind of thing
Omu
A: 

I ended up creating a service locator: http://martinfowler.com/articles/injection.html#UsingAServiceLocator

I find it easier than dealing with an IoC container and trying to insert my DI code all over the MVC pipeline.

AlexWalker
A: 

I'd recommend reviewing S#arp Architecture http://www.sharparchitecture.net/

Open source framework addon for asp.net mvc.

Will Shaver
A: 

Are you completely sure you need to inject a dependency into your domain model itself? An entity or business object will typically encapsulate the state and expose methods to modify that state according to business rules. Code that does not fall into this category typically will be found in a service. Have you read into the concept of a domain service at all? Perhaps using one would better suit your needs and you won't need to inject any dependencies into your domain itself.

Lance Harper
Isn't it a matter of preference? According to the MS MVC tutorials, "Often, model objects retrieve and store model state in a database. For example, a Product object might retrieve information from a database, operate on it, and then write updated information back to a Products table in SQL Server."http://www.asp.net/learn/mvc/tutorial-01-cs.aspx
AlexWalker
A: 

Checkout this sample I've created based on Ayende's explanations on his blog. Basically, I use Castle as my IoC container and I use Mvc Contrib to add all controllers to the container and make Mvc get them from it. Then I can inject anything into the containers, such as NHibernate ISession.

If you want to inject stuff inside your model classes (entities), NH now supports Dependency Injection of Hibernate-managed objects. See this, this, and this for specific examples for Spring and Windsor.

zvolkov
A: 

What your talking about is more along the lines of the Active Record pattern.

Whether AR is possible or not will depend on which ORM/DAO your using.

The AR pattern is generally better suited for small projects.

flukus