Castle Windsor:当程序集无法访问基础类型时,如何注册工厂方法? [英] Castle Windsor: How do I register a factory method, when the underlying type isn't accessible to my assembly?
问题描述
我有一个项目,其中的业务层是使用DI构建的,但是我想进一步尝试使用Windsor来管理对象的构建。
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.
只是说我有一个预先存在的数据层(我不想修改),可以通过以下界面进行访问:
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; }
}
我的业务层中的一系列类取决于通过IDataFactory公开的服务:
A series of classes in my business layer depend on the services exposed through IDataFactory:
IFactory factory = DataFactory.NewFactory();
IBusinessService service = new ConcreteBusinessService(factory.Service);
我知道,为了将IBusinessService注册到温莎城堡容器,我将使用类似于
I understand that in order to register IBusinessService to the Castle Windsor container, I'd use code similar to this:
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);
温莎似乎要我说 container.AddComponent( dataService,typeof(IDataService ),typeOf(SomeConcreteDataService)),但在这种情况下,ConcreteDataService在该程序集内部,因此在我的程序集中无法访问。
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.
鉴于我的程序集不知道SomeConcreteDataService,该如何连接数据服务?
How would I go about wiring up the data service, given that SomeConcreteDataService isn't known to my assembly?
这个问题非常类似于我自己的,除了在我的例子中,AddComponent( calculator,typeof(ICalcService),typeof(CalculatorService), Create);调用将无法正常工作-CalculatorService将在另一个程序集内部,而不是用于将容器连接到容器的程序集。
This question is 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.
推荐答案
使用Windsor 2.0:
Using Windsor 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);
}
}
请参见流利的API Wiki页面。
这篇关于Castle Windsor:当程序集无法访问基础类型时,如何注册工厂方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!