Avatar of gswitz
gswitz
 asked on

Dependency Injection on ResponseModelFilter

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?
C#

Avatar of undefined
Last Comment
gswitz

8/22/2022 - Mon
gswitz

ASKER
I found this...
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?
ASKER CERTIFIED SOLUTION
kblau

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
gswitz

ASKER
This isn't the answer at all, but it's nice to give the points.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck