2

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.

Bret Walker
  • 1,796
  • 5
  • 20
  • 41
  • Are you actually asking if it's possible to mock Linq2Sql or Linq2EF? I don't think that anyone has taken the burden of this one. – Robert Koritnik Oct 26 '09 at 22:46
  • 1
    Please give us the specific problem you want to solve by using dependency injection in Model. I do it in controller because I want to mock the repository easily. Why do you want to do it in model? Maybe, you are over complicate the problem. – J.W. Nov 03 '09 at 03:03
  • I need to pull a list of states from a database. – Bret Walker Nov 03 '09 at 16:24

7 Answers7

0

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

JRoppert
  • 5,854
  • 5
  • 32
  • 37
  • 1
    Really has nothing to do with the question. The OP is looking for how to inject dependencies into his model, NOT into his controllers. – Ed DeGagne May 09 '14 at 15:56
0

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
  • 69,856
  • 92
  • 277
  • 407
  • 1
    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. – Bret Walker Oct 26 '09 at 22:36
  • 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 Oct 27 '09 at 04:01
  • 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 Oct 27 '09 at 07:04
0

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.

Bret Walker
  • 1,796
  • 5
  • 20
  • 41
0

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

Open source framework addon for asp.net mvc.

Will Shaver
  • 12,471
  • 5
  • 49
  • 64
0

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
  • 2,216
  • 14
  • 11
  • 2
    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 – Bret Walker Oct 27 '09 at 13:30
0

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.

Andriy Volkov
  • 18,653
  • 9
  • 68
  • 83
0

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
  • 996
  • 8
  • 18