简体   繁体   中英

Dependency Injection ActionFilterAttribute alternative

Is there an elegant way to implement the following in a ActionFilterAttribute .

_provider = new DetailsProvider(new DetailsService(new CachingService()))

As you can see this does not look good. What I want to get away from, is 'newing' up these concrete classes.

Is there a nice alternative?

I solved this using

public HandlerAttribute() :
        this(DependencyResolver.Current.GetService<IDetailsProvider>()) { }

public HandlerAttribute(IDetailsProvider detailsProvider)
{
    _detailsProvider = detailsProvider;
}

I am using SimpleInjector but this is interchangeable as I am using a Resolver.

You can just use whatever containers, get instance of concrete class using interface method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM