Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property Injection into an Action Filter

I'm trying to get Property Injection working on a Custom Action Filter Attribute. It is working as it is supposed to, however, I'd like to use DI on the Property itself. My filter looks like this

[AttributeUsage(AttributeTargets.Class)]
public sealed class HeaderFilterAttribute : ActionFilterAttribute
{
    public IMarketService MarketService
    { get; set; }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        var view = (ViewResultBase)filterContext.Result;

        if (view != null)
        {
            BaseViewModel viewModel = view.ViewData.Model as BaseViewModel;
            if (viewModel != null)
                viewModel.Header = GetHeaderScript();
        }
        base.OnActionExecuted(filterContext);
    }

   private string GetHeaderScript()
   {
     //Use MarketService here and return header script
     return "script";
   }
}

This is how I'm configuring the property using StructureMap inside my BootStrapper class.

            //HeaderFilterAttribute
        IMarketRepository marketRepository = new SqlMarketRepository();
        IMarketService marketService = new MarketService(marketRepository);
        ObjectFactory.Container.Configure(r => r.ForConcreteType<HeaderFilterAttribute>().
                                          Configure.WithProperty("MarketService").
                                          EqualTo(marketService));

My problem is I do not have access to SqlMarketRepository since all my concrete types are injected via DI and I really don't want to use concrete types in my bootstrapper. So the ultimate question now is, how do I inject MarketService into the Filter attribute without resorting to the above? :)

like image 873
Praveen Avatar asked Oct 06 '10 20:10

Praveen


People also ask

Can we inject the dependency to individual action method of the controller?

You can take advantage of the ServiceFilter attribute to inject dependencies in your controller or your controller's action methods.

What is the action of a filter?

Filter actions send information between worksheets. Typically, a filter action sends information from a selected mark to another sheet showing related information. Behind the scenes, filter actions send data values from the relevant source fields as filters to the target sheet.

What are the two methods defined by action filter?

Output Cache − This action filter caches the output of a controller action for a specified amount of time. Handle Error − This action filter handles errors raised when a controller action executes.

Can we set custom action filter in action methods in Web API?

Now we can modify our web API controller to add the Action Filter we have just created.An Action Filter can modify a whole class: in that case all the methods in the API Controller are affected. Or we can set it to affect only a determined class.


1 Answers

In your ObjectFactory.Initialize() call, add the following line:

SetAllProperties(x => x.OfType<IMarketService>());

That will inject the configured IMarketService instance into any property of type IMarketService, on any object retrieved from the container.

like image 175
Joshua Flanagan Avatar answered Sep 25 '22 15:09

Joshua Flanagan