Hello,
I have a problem to understand how to use IoC in a scenario where I need to create objects dynamically. Lets assume I have this classes:
abstract class Field {
public Field( ICommandStack commandStack ) {}
}
abstract class Entity {
public readonly Collection<Field> Fields { get; }
}
class EntityA {
public EntityA( ICommandStack commandStack ) {
Fields.Add( new StringField( commandStack ) );
}
}
class EntitiyB {
public EntityB( ICommandStack commandStack ) {
Fields.Add( new IntField( commandStack ) );
Fields.Add( new IntField( commandStack ) );
Fields.Add( new IntField( commandStack ) );
}
}
So my problem is the creation of Fields in the constructors. My Fields need an ICommandStack, but the Entities do not. They only get the ICommandStack for the creation of their Fields.
It could be easier to request the Fields as an argument in each Entity's constructor. But the number of Fields could be >10 for single Entities. I don't want to create constructors with so many parameters.
So my idea was to hand over a FieldFactory to the Entites:
class EntityA {
public EntityA( IFieldFactory fieldFactory ) {
// create as many fields as needed via the factory
Fields.Add( fieldFactory.CreateStringField() );
}
}
At least the (for Entity) unneccessary ICommandStack is now gone. But how does the FieldFactory create a Field? It only can get the ICommandStack injected - but the creation of Fields has still to be done via the 'new' keyword. Or should I give the factory a reference to my DI-container?
What is a good design solution here?