I need to make a change to an app using dependency injection.
The change goes into the ResponseModelFilter. I could new up my class and do my work right there, but I was thinking, to keep with the spirit of DI, I should use the container to create the object.
I'm using Unity.
I created an interface for my new class.
I created my new class inheriting from the interface.
I added this to the configuration of the container...
container.RegisterType<IMyNewClass, MyNewClass>();
I created a new abstract class ActionFilterAttributeCustom which inherits from ActionFilterAttribute and has a Constructor that takes my new class.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
public abstract class ActionFilterAttributeCustom : ActionFilterAttribute
{
// Summary:
// Initializes a new instance of the System.Web.Http.Filters.ActionFilterAttribute
// class.
protected ActionFilterAttributeCustom(IMyNewClass myNewClass)
I return to the container configuration and add...
container.RegisterType<ActionFilterAttributeCustom, ResponseModelFilter>(
new InjectionConstructor(new ResolvedParameter<IMyNewClass>())
);
I alter the ResponseModelFilter to inherit from ActionFilterAttributeCustom and create a constructor that takes IMyNewClass
private readonly IMyNewClass _myNewClass;
//Constructor for Dep Injection
public ResponseModelFilter (IMyNewClass myNewClass)
{
_myNewClass = myNewClass;
}
Now, the build is largely successful except every class with
[ResponseModelFilter]
before the public class declaration
Error 6 'ResponseModelFilter' does not contain a constructor that takes 0 arguments.
I'm not sure how to alter these Controller classes.
Is it possible that I need a controller factory to resolve this issue?
http://stackoverflow.com/questions/14649545/how-to-inject-constructor-in-controllers-by-unity-on-mvc-4
Now, on all the controller classes that have
[ResponseModelFilter]
How can I go about using Unity to resolve the dependency for the constructor?