c# - IoC: How to create objects dynamically -
I have a problem in understanding how to use IOC in the scenario where I need to create objects dynamically Suppose I have these classes:
Abstract class area {Public area (ICMM & Stack command stack) {}} Abstract class unit {public readonly collections & lt; Field & gt; Field {get; }} Class EntityA {Public EntityA (ICommandStack command stack) {Fields.Add (new StringField (commandStack)); }} Category EntitiyB {Public EntityB (ICommandStack command stack) {Fields.Add (new IntField (commandStack)); Field.add (new intended (command stack)); Field.add (new intended (command stack)); }} So my problem is to build fields in the consultants. My field needs ICMMM stack, but the institutions do not. They only get ICMMMand stacks to build their fields.
Requesting the field as an argument in the constructor of each unit can be easy. But the number of fields> can be 10 for single units. I do not want to be a constructor with so many parameters.
So I think that a FieldFactory is assigned to Entites:
class EntityA {Public EntityA (IFieldFactory FieldFactory) {// Many required through the factory field Create fields. Add (fieldFactory.CreateStringField ()); }} The ineffective ICommandStack is now gone (for the unit) but how can Fieldfactor create a field? It can only get ICMandstock injection - but the field construction is still to be done through the 'new' keyword. Or should I give the factory a reference of my di container?
What is a good design solution here?
I will inject the factory using a fieldfactor in the context of the container (or if you are on your container Not happy with taking a strong dependency, with an interface).
Otherwise, it's all turtle in the bottom. For a new instance at some point you need some object to ask the container. If you want your field to be de-injected, you have to ask the container to make them or you.
To summarize, I had to go with the factory.
Comments
Post a Comment