I'd like to get some advice on what would be a good approach at hiding the creation of an object graph. In the code below I would like SomeClass
to not be directly responsible for the creation of the entire Thing object graph (all the objects in the CreateThing
method. I don't have any control over any of the objects in the Thing
graph (for a little context, they are all WCF objects)
Additionally, I feel like there is a code smell here where IConfigService
is passed into SomeClass
just to initialize a property in the Thing object graph. I'd like to have the IConfigService
dependency removed from this class as it does not seem to be a direct concern of this class.
public class SomeClass : ISomeClass
{
private readonly IFirstDependency _firstDependency;
private readonly ISecondDependency _secondDependency;
private readonly IConfigService _configService;
public SomeClass(IFirstDependency, firstDependency, ISecondDependency secondDependency, IConfigService configService)
{
_firstDependency = firstDependency;
_secondDependency = secondDependency;
_configService = configService;
}
public List<Foo> GetFoo(BarDto bar)
{
Thing thing = CreateThing(bar);
/// other code omitted
}
private Thing CreateThing(BarDto bar)
{
return new Thing()
{
Child = new Child()
{
SimpleProp = "simple",
GrandChild = new GrandChild()
{
Items = new List<Item>()
{
Item = new Item()
{
Prop1 = _configService.GetSettings...
Prop2 = $"{bar.This} {bar.That}",
Prop3 = $"{bar.MoreBar} {bar.EvenMoreBar}"
}
}
}
}
};
}
}
Should I hide the creation of the object graph behind an interface and have the implementation take the IConfigService
dependency? This doesn't seem like a proper factory per se. Thoughts?