Unity Inject dependencies into MVC filter class with parameters

As per the post Passive Attributes, the DI-friendly solution is to separate the AuthorizeAttribute into 2 parts: An attribute that contains no behavior to flag your controllers and action methods with. A DI-friendly class that implements IAuthorizationFilter and contains the desired behavior. For our purposes, we just inherit AuthorizeAttribute to take advantage of some of … Read more

MVC, EF – DataContext singleton instance Per-Web-Request in Unity

Yes do not share context and use one context per request. You can also check linked questions in that post to see all problems which a shared context caused. Now about Unity. Idea of PerCallContextLifetimeManager works but I think provided implementation will not work for more than one object. You should use PerHttpRequestLifetimeManager directly: public … Read more

Make sure that the controller has a parameterless public constructor error

What’s happening is that you’re bitten by this problem. Basically, what happened is that you didn’t register your controllers explicitly in your container. Unity tries to resolve unregistered concrete types for you, but because it can’t resolve it (caused by an error in your configuration), it return null. It is forced to return null, because … Read more

Dependency Injection Unity – Conditional Resolving

A simple way to solve this is with the strategy pattern. Note that you can add or remove login providers without changing the design – you simply need to change the DI configuration. Interfaces public interface IAuthenticate{ bool Login(string user, string pass); bool AppliesTo(string providerName); } public interface IAuthenticateStrategy { bool Login(string providerName, string user, … Read more

Is it better to create a singleton to access unity container or pass it through the application? [closed]

The correct approach to DI is to use Constructor Injection or another DI pattern (but Constructor Injection is the most common) to inject the dependencies into the consumer, irrespective of DI Container. In your example, it looks like you require the dependencies TestSuite and TestCase, so your TestSuiteParser class should statically announce that it requires … Read more

Dependency Injection in attributes

You should prevent doing dependency injection into attributes completely. The reason for this is explained in this article: Dependency Injection in Attributes: don’t do it!. In summary the article explains that: Constructor injection is not possible, because creation of an Attribute instance cannot be intercepted; the CLR is in control. The use of property injection … Read more

Is there a pattern for initializing objects created via a DI container

Any place where you need a run-time value to construct a particular dependency, Abstract Factory is the solution. Having Initialize methods on the interfaces smells of a Leaky Abstraction. In your case I would say that you should model the IMyIntf interface on how you need to use it – not how you intent to … Read more