在Unity中指定运行时参数依赖性
我有一个类需要一个字符串作为其构造函数中的参数,但该参数将由调用代码决定。同时,此类的生命周期必须与每个 HTTP 请求相关联。因此,我创建了一个自定义 PerWebRequestTimelineManager 并将其用作配置文件中的目标类型。但由于构造函数中的字符串必须动态确定,因此我无法通过配置文件使用 ConstructorInjection。我可以使用抽象工厂来解决动态依赖问题,但我不确定实现:您可以检查下面的代码并验证该方法吗?具体来说,尽管应用程序中连续的 Resolve 调用将能够检索相同的实例,但 RegisterType 和 Resolve 调用似乎有点不合适。:
public class PerformanceTracerFactory : IPerformanceTracerFactory
{
private readonly IPerformanceTracer tracer;
public IPerformanceTracer CreateInstance(string operationTitle)
{
_container.RegisterType<IPerformanceTracer, PerformanceTracer>(new InjectionConstructor(operationTitle));
return _container.Resolve<IPerformanceTracer>();
}
}
配置文件的相关部分:
<register type="IPerformanceTracer" mapTo="PerformanceTracer">
<lifetime type="PerWebRequest"/>
</register>
<register type="IPerformanceTracerFactory" mapTo="PerformanceTracerFactory"/>
我还有另一个问题。如果上述使用代码配置和注入依赖项的方法是正确的,那么我认为我不需要配置条目。我始终可以使用合适的重载来推送自定义生命周期管理器。如果我想仅使用配置文件来实现相同的目标,那么如何编写解决方案?
I have a class which needs a string as a parameter in its constructor but this parameter will be decided by the calling code. At the same point of time, the life time of this class has to be tied to per HTTP request. So, I created a custom PerWebRequestTimelineManager and used that for my target type in the config file. But since the string in the constructor has to be dynamically determined, I cannot use the ConstructorInjection via the config file. I can use an abstract factory to solve the problem of dynamic dependency, but I am not sure about the implementation: Can you check the code below and validate the approach. Specifically the RegisterType and Resolve calls seem a bit out of place though the successive Resolve calls across the application will be able to retrieve the same instance.:
public class PerformanceTracerFactory : IPerformanceTracerFactory
{
private readonly IPerformanceTracer tracer;
public IPerformanceTracer CreateInstance(string operationTitle)
{
_container.RegisterType<IPerformanceTracer, PerformanceTracer>(new InjectionConstructor(operationTitle));
return _container.Resolve<IPerformanceTracer>();
}
}
Relevant portion of config file:
<register type="IPerformanceTracer" mapTo="PerformanceTracer">
<lifetime type="PerWebRequest"/>
</register>
<register type="IPerformanceTracerFactory" mapTo="PerformanceTracerFactory"/>
I have another question. In case if the above way of configuring and injecting the dependency using code is correct, then I think I do not need the config entries. I can always use the suitable overload to push the custom lifetime manager. In case, I would want to achieve the same thing using only config file, then how do I code the solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用基于容器的工厂,则不必在每次调用中注册/解析您的
IPerformanceTracer
。注册映射
IPerformanceTracer
-->PerformanceTracer
在您的配置文件中一次,并在解析接口时使用ParameterOverride
。If you use a container-based factory you don't have to register/resolve your
IPerformanceTracer
in each call.Register the mapping
IPerformanceTracer
-->PerformanceTracer
once in your config file and use aParameterOverride
when you resolve your interface.