[英]Dependency Injection ActionFilterAttribute alternative
Is there an elegant way to implement the following in a ActionFilterAttribute
. 有没有一种优雅的方法可以在
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. 我正在使用SimpleInjector,但是这与我在使用解析器时是可以互换的。
You can just use whatever containers, get instance of concrete class using interface method. 您可以使用任何容器,使用接口方法获取具体类的实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.