Autofac - 解决运行参数,而不必来回传递容器 [英] Autofac - resolving runtime parameters without having to pass container around

查看:991
本文介绍了Autofac - 解决运行参数,而不必来回传递容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的ServiceHelper类,它在构造函数中两个参数:

I have a simpler "ServiceHelper" class that takes two parameters in the constructor:

public ServiceHelper(ILogger<ServiceHelper> log, string serviceName)

(ILogger为NLOG通用包装,Autofac是提供蛮好的,该服务名是一个Windows服务的名称来控制,我需要在运行时提供。)

(ILogger generic wrapper for NLog that Autofac is providing just fine, and the serviceName is the name of a Windows service to control that I need to provide at runtime.)

我在遇到麻烦缠绕我的头围绕如何创建该类的新实例在运行时传递不同的服务名称,使用Autofac。像这样的东西,因为我需要在运行时指定不同的服务名称并不当然工作:

I'm having trouble wrapping my head around how to create new instances of this class at runtime passing in different service names, using Autofac. Something like this doesn't work of course since I need to specify different service names at runtime:

builder.RegisterType<ServiceHelper>().As<IServiceHelper>().WithParameter(new NamedParameter("serviceName", null)).InstancePerDependency();



从我读过,它的一个坏习惯是传递的容器,并呼吁解决手动权(服务定位器反模式的AutoFac发出警告),或者是什么呢?如果我这样做,那么我可以做

From what I've read, its a bad habit to pass the container around and call Resolve manually right (the Service Locator "anti-pattern" the AutoFac warns about), or is it? If I did that then I could do

container.Resolve<ServiceHelper>(new NamedParameter("serviceName", "some service name"));



但即使走到这一步我不太知道如何让Autofac到容器注入类,那就只需要本身究竟如何注册,这样吗?再有我的课需要它们的构造函数的的IContainer? (这是一个C#服务使用构造器注入)

But to even get that far I'm not quite sure how to get Autofac to inject the container into the classes, it would just need to register itself how exactly, like this? And then have my classes require an IContainer in their constructors? (This is in a C# Service using constructor injection)

builder.RegisterType<Container>().As<IContainer>().InstancePerDependency();



我读到委托工厂太多,但似乎并没有从有到通过集装箱脱身周围。

I read about delegate factories too but that doesn't seem to get away from having to pass the container around.

最真我的课消耗的ServiceHelper,只需要1或2 ServiceHelpers特定的服务名称,所以它不是像我使成千上万意想不到的服务名参数,这只是让我的头有点痛。

Really most of my classes that consume the ServiceHelper, just need 1 or 2 ServiceHelpers for specific service names, so its not like I'm making thousands with unexpected serviceName parameters, this is just making my head hurt a little.

推荐答案

我去了上述办法的道路,但它工作得很好,我发现根本无法进行单元测试,由于事实解决<>中的IContainer方法是一个扩展方法。它也从来没有真正感受到正确与所有人都在谈论不在身边路过你的容器。

I went down the path of the above approach and it works fine, however I found it impossible to unit test due to the fact the "Resolve<>" method in IContainer is a extension method. It also never really felt "right" with all the talk about not passing your container around.

我回到绘图板,并发现了正确的方式来实例使用Autofac委托工厂
HTTP对象://docs.autofac .ORG / EN /最新/高级/代表,factories.html

I went back to the drawing board, and found the "correct" way to instantiate objects using Autofac Delegate Factories http://docs.autofac.org/en/latest/advanced/delegate-factories.html

这篇关于Autofac - 解决运行参数,而不必来回传递容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆