温莎城堡:如何防止工厂创建的对象中的圆形引用被创建的对象返回工厂 [英] Castle Windsor: How to prevent circular references in factory-created objects were the created objects refers back to the factory
问题描述
我正在使用温莎城堡作为我的IoC容器,但遇到了一些问题。最好用代码解释一下,因此我将尝试一下。
我有一个工厂类,应该为我提供某些接口的实现:
I am using windsor castle as my IoC container, and has run in to a bit of a problem. This is best explained in code, so I´ll give it a try. I have a factory class, that should provide me with implementations of a certain interface:
public interface IObjectCreatorFactory
{
IObjectCreator GetObjectCreator(Type objectType);
}
public interface IObjectCreator
{
T CreateObject<T>(IDataRow data);
bool SupportsType(Type type);
}
工厂类的实现可能看起来像这样,尽管我不确定是这样的方式:
公共接口ObjectCreatorFactory:IObjectCreatorFactory
{
IEnumerable specificCreators;
IObjectCreator defaultCreator;
Implementation of the factory class could look like this, though I am not sure this is the way to go: public interface ObjectCreatorFactory:IObjectCreatorFactory { IEnumerable specificCreators; IObjectCreator defaultCreator;
public ObjectCreatorFactory(IEnumerable<IObjectCreator> specificCreators, IObjectCreator defaultCreator)
{
this.specificCreators= specificCreators;
this.defaultCreator= defaultCreator;
}
public IObjectCreator GetObjectCreator(Type objectType)
{
foreach (IObjectCreator creator in specificCreators)
{
if (creator.SupportsType(objectType))
{
return creator;
}
}
return defaultCreator;
}
}
现在这行得通,但是如果我想要我的IObjectCreator实例使用特定的IObjectCreator创建子对象,我想调用ObjectCreatorFactory,这显然导致循环引用:
Now this would work out ok, but if I want my IObjectCreator instance to create child objects using a specific IObjectCreator, i would like to call ObjectCreatorFactory, and this obviously results in a circular reference:
public void SpecificObjectCreator:IObjectCreator
{
IObjectCreatorFactory objCreatorFactory;
public SpecificObjectCreator(IObjectCreatorFactory objCreatorFactory)
{
this.objCreatorFactory = objCreatorFactory;
}
T CreateObject<T>(IDataRow data)
{
T obj = new T;
ChildObject childObject = objCreatorFactory.GetObjectCreator(typeof(ChildObject)).CreateObject<ChildObject>(data);
.......
}
bool SupportsType(Type type);
}
这不可行。
推荐答案
我只是将工厂从各种特定对象创建者的构造函数中移出,并在IObjectCreator接口上引入一种方法,负责初始化创建者:
I would simply move the factory out of the constructors of the various specific object creators, and introduce a method on the IObjectCreator interface instead, responsible for initialising the creators:
public interface IObjectCreator
{
T CreateObject<T>(IDataRow data);
bool SupportsType(Type type);
void Initialize(IObjectCreatorFactory factory);
}
然后在传递给工厂的每个对象创建者上调用Initialze(this)
And then just invoke Initialze(this) on each object creator passed into the factory.
过去,我使用自定义生命周期阶段来自动调用组件的构造后设置,以避免循环依赖并采取照顾其他相关问题(例如,从外部资源(如数据库)中应用其他组件配置)-但这可能对于您所需的功能而言过于矫kill过正。
In the past I've used custom life cycle stages to take care of automatically invoking "post-construction" setup of components to both avoid circular dependencies and also to take care of other associated concerns (i.e. applying additional component configuration from an external source like a database) - but it's probably overkill for what you need.
这篇关于温莎城堡:如何防止工厂创建的对象中的圆形引用被创建的对象返回工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!