dependency injection - Castle Windsor: How to prevent circular references in factory-created objects were the created objects refers back to the factory -
I am using Windosor Castle as my IoC container, and has gone into some parts of a problem. This code is best explained, so I'll give it a try. I have a factory class, which I should provide with the implementation of a specific interface:
Public interface IObjectCreatorFactory { IObjectCreator GetObjectCreator (object type type); } Public Interface IObjectCreator {T CreateObject & lt; T & gt; (IDataRow data); Boolean support type (type type); }
Implementation of the factory class may look like this, although I'm not sure this is the same way: Public Interface ObjectCreatorFactory: IObjectCreatorFactory {IEnumerable specificCreators; IObjectCreator defaultCreator;
Public ObjectCreatorFactory (IEnumerable & lt; IObjectCreator & gt; Featured Creator, IObjectCreator defaultCreator) {this.specificCreators = Exclusive Creator; This.defaultCreator = defaultCreator; } Public IObjectCreator GetObjectCreator (type object type) {foreach (iObjectCreator creator in specific manufacturer) {if (creator.SupportsType (objectType)) {return creator; }} Return default creator; }}
Now it will work properly, but if I want my IObjectCreator instance to create child objects using a specific IObjectCreator, I would like to call the ObjectCreator Factory, and this Obviously a circular result will result in:
Public zero specific object creator: IObjectCreator {IObjectCreatorFactory objCreatorFactory; Public Specific Object Builder (IObjectCreatorFactory objCreatorFactory) {this.objCreatorFactory = objCreatorFactory; } T Create Object & lt; T & gt; (IDataRow data) {T obj = new T; ChildObject childObject = objCreatorFactory.GetObjectCreator (typeOf (ChildObject) create object & lt; ChildObject & gt; (Data); .......} bool support type (type type); }
This does not work, what will be the way to go for this scenario, where the built object is returning to the factory for the Child Object Creator?
I will simply move the factory from the constructors of various object creators, and instead the one on the IObjectCreator interface Will present the method, which is responsible for initiating the creators:
Public Interface IObjectCreator {T CreateObject & lt; T & gt; (IDataRow data); Boolean support type (type type); Zero start (IObjectCreatorFactory factory); }
And then just start the entry (escapes) on each object manufacturer in the factory.
In the past, I have used the steps of custom life cycle to avoid circular dependence and to take care of other related concerns (i.e. implementing an additional source configuration such as external source as a database ) Automatically setting up "post-creation" components - but probably what you need for it. / P>
Comments
Post a Comment