Dependecy Injection with MVC other than controllers - dependency-injection

I am trying to understand dependency injection in MVC. I got some idea and implemented successfully with Controllers using extending DefaultControllerFactory and using ninject. But i read something like DI in views , Actions etc.
I didn't get DI in Views or my understanding is wrong. If yes, please tell me use and how to do that. I didn't understand other DI areas in MVC. Please explain the areas where DI is used in MVC other than Controllers

In MVC, you should try and make your Views as independent as possible. Ideally, the only dependency it rely on is the ViewModel (other view services and helpers are available through the WebPageBase class that all views inherit by default).

Related

WebApi controller dependencies cannot be resolved using StructureMap

I'm currently using StructureMap with MVC, but its complaining that my api controllers don't have a default constructor. Can someone point me in the right direction in order to set this up with StructureMap? As I understand, MVC and WebApi have separate resolvers (which makes sense since they are separate frameworks).
I have done it for AutoFac here. Should give you enough pointers to implement it yourself.

Can Ninject inject a class and make it reference the target class as a back reference?

I want to inject a presenter into my webform class. While doing so I would like the injected presenter to contain a reference to the view it is being injected into. Inject a class with a reference to the class it is injected into. Is it possible to do this with Ninject? When injecting into a webforms class, is the injection performed before the constructor is executed? Should I rather just inject the presenter and use the constructor to pass the view?
For webforms this is not possible because the form is not created by ninject. You have to pass it manually to the presenter.
I think the solution is to use MVC there you have support for your problem form the framework. Webforms and MVC can be mixed. You can create new pages with MVC and still use the old webform ones. Whenyou have to change a webforms page then you better refactor it to MVC befor the change.

Should I make my ASP.NET MVC controller actions virtual?

File -> New Project for ASP.NET MVC projects used to generate controllers with virtual actions. I'm not sure if that stopped with MVC 2 or MVC 3, but is this no longer a best practice?
T4MVC Does make action methods virtual. If you are using it, it should make action methods virtual, no other way it can work
The current documentation for ASP.NET MVC 3 does not show virtual methods. I'm not really sure what making them virtual would gain you, as I've never subclassed controllers to override actions.
Generated code may be made virtual for various reasons. It's not good practice to make your own code virtual unless required by some tool. See Liskov Substitution Principle and Open/Closed Principle. I think some frameworks do this to facilitate creating proxies but I can't imagine any reason to make all of your methods virtual. To me it screams copy/paste or cargo cult programmer.

How do I use dependency injection with an ASP.NET MVC model?

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.
You can find a reasonable How-To on Shiju Vargheses Blog: ASP.NET MVC Tip: Dependency Injection with Unity Application Block
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>();
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.
I'd recommend reviewing S#arp Architecture
http://www.sharparchitecture.net/
Open source framework addon for asp.net mvc.
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.
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.
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.

How can I use Windsor to inject a dependency into an ASP.NET MVC model?

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.
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.

Resources