Fabiano
Fabiano

Reputation: 5194

Spring.Net: Inject ApplicationContext / ObjectFactory itself

Is there a way to pass the IObjectFactory or the ApplicationContext that is currently executing as dependency to an object? For example

  <object id="SpringActionInvoker" type="xxx.SpringActionInvoker, xxx">
    <constructor-arg ref="reference_to_the_ApplicationContext_or_ObjectFactor_that_is_executing" />
  </object>

I want to use it for my Spring.Net implementation of the Asp.Net MVC ControllerActionInvoker that will be injected to the controllers

public class SpringActionInvoker : ControllerActionInvoker
{
    private IObjectFactory objectFactory;

    public SpringActionInvoker(IObjectFactory objectFactory)
    {
        this.objectFactory = objectFactory;
    }

    protected override FilterInfo GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
        //use objectFactory to inject dependencies into filters
    }
}

Upvotes: 0

Views: 1044

Answers (2)

Sebastian Piu
Sebastian Piu

Reputation: 8008

just make your class implement IApplicationContextAware that should get you the current IApplicationContext injected automagically.

Upvotes: 4

Darin Dimitrov
Darin Dimitrov

Reputation: 1038730

How about:

<object id="SpringActionInvoker" type="xxx.SpringActionInvoker, xxx" />

And then:

public class SpringActionInvoker : ControllerActionInvoker
{
    protected override FilterInfo GetFilters(ControllerContext controllerContext, ActionDescriptor actionDescriptor)
    {
        IObjectFactory objectFactory = ContextRegistry.GetContext();
        //use objectFactory to inject dependencies into filters
    }
}

Upvotes: 0

Related Questions