C# Castle Windsor:当我的程序集无法访问基础类型时,如何注册工厂方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/858668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Castle Windsor: How do I register a factory method, when the underlying type isn't accessible to my assembly?
提问by Jeremy Frey
I have a project where my business layer is constructed using DI, but I'm trying to go an additional step and use Windsor to manage object construction.
我有一个项目,其中我的业务层是使用 DI 构建的,但我正在尝试进行额外的步骤并使用 Windsor 来管理对象构建。
Let's just say I have a pre-existing data layer (that I don't want to modify), that can be accessed via the following interface:
假设我有一个预先存在的数据层(我不想修改),可以通过以下接口访问:
interface IDataFactory {
IDataService Service { get; }
}
A series of classes in my business layer depend on the services exposed through IDataFactory:
我的业务层中的一系列类依赖于通过 IDataFactory 公开的服务:
IFactory factory = DataFactory.NewFactory();
IBusinessService service = new ConcreteBusinessService(factory.Service);
I understand that in order to register IBusinessService to the Castle Windsor container, I'd use code similar to this:
我知道为了将 IBusinessService 注册到 Castle Windsor 容器,我将使用类似于以下的代码:
IWindsorContainer container = new WindsorContainer();
container.AddComponent("businessService", typeof(IBusinessService), typeof(ConcreteBusinessService));
But for the life of me, I can't figure out how to register services from my data layer, using the my existing factory object. In essence, I'd like to say:
但是对于我的一生,我无法弄清楚如何使用我现有的工厂对象从我的数据层注册服务。本质上,我想说:
container.AddComponent("dataService", typeof(IDataService), factory.service);
Windsor seems to want me to say container.AddComponent("dataService", typeof(IDataService), typeOf(SomeConcreteDataService)), but in this instance, ConcreteDataService is internal to that assembly, and thus not accessible in mine.
Windsor 似乎想让我说container.AddComponent("dataService", typeof(IDataService), typeOf(SomeConcreteDataService)),但在这种情况下,ConcreteDataService 是该程序集的内部,因此在我的无法访问。
How would I go about wiring up the data service, given that SomeConcreteDataService isn't known to my assembly?
鉴于我的程序集不知道 SomeConcreteDataService,我将如何连接数据服务?
This questionis very similar to my own, except in my case, the AddComponent("calculator", typeof(ICalcService), typeof(CalculatorService), "Create"); call wouldn't work -- CalculatorService would be internal to another assembly, not available to the assembly wiring up the container.
这个问题与我自己的问题非常相似,除了在我的情况下, AddComponent("calculator", typeof(ICalcService), typeof(CalculatorService), "Create"); call 不起作用——CalculatorService 将位于另一个程序集的内部,不能用于连接容器的程序集。
采纳答案by Mauricio Scheffer
Using Windsor 2.0:
使用温莎 2.0:
[TestFixture]
public class WindsorTests {
public interface IDataService {}
public class DataService: IDataService {}
public interface IDataFactory {
IDataService Service { get; }
}
public class DataFactory: IDataFactory {
public IDataService Service {
get { return new DataService(); }
}
}
[Test]
public void FactoryTest() {
var container = new WindsorContainer();
container.AddFacility<FactorySupportFacility>();
container.AddComponent<IDataFactory, DataFactory>();
container.Register(Component.For<IDataService>().UsingFactory((IDataFactory f) => f.Service));
var service = container.Resolve<IDataService>();
Assert.IsInstanceOfType(typeof(DataService), service);
}
}
See the fluent API wiki pagefor more information.
有关更多信息,请参阅fluent API wiki 页面。
回答by Jeremy Frey
Got it. The answer is, you don't need to worry about the implementation type:
知道了。答案是,您无需担心实现类型:
IWindsorContainer container = new WindsorContainer();
container.AddFacility("factories", new FactorySupportFacility());
container.AddComponent("standard.interceptor", typeof(StandardInterceptor));
container.AddComponent("factory", typeof(DataFactory));
MutableConfiguration config = new MutableConfiguration("dataService");
config.Attributes["factoryId"] = "factory";
config.Attributes["factoryCreate"] = "get_Service";
container.Kernel.ConfigurationStore.AddComponentConfiguration("dataService", config);
container.Kernel.AddComponent("dataService", typeof(IDataService));
IDataService dataService = (IDataService) container["dataService"];
I had a "whoa" moment when I saw it execute successfully, because I hadn't passed in the specific implementation type to Kernel.AddComponent()
当我看到它成功执行时,我有一个“哇”的时刻,因为我没有将特定的实现类型传递给 Kernel.AddComponent()