So, the object model is rather complex and hierarchical. Grandfather accepts is created by a GrandfatherFactory that accepts a GrandfatherRepository in it's constructor.
All is well.
Now when Grandfather needs to load it's Children how does it do it?
How does it know about the ChildFactory and ChildRepository? It shouldn't correct?
But then how does it load them? I wouldn't think you pass a ChildFactory into the Grandfather constructor, as if you have Grandchildren (which this app does) you'd need to pass in a GrandchildFactory as well!
Do you instantiate some kind of EverythingFactory that has all of the different types of factories inside of it and pass that into each constructor and they make use of the one they care about?
Or is there some simple solution I'm just not seeing?
Currently all the objects call a webservice to load their data, and I'm trying to pull that out and make a Repository interface that can be mocked for testing purposes.
EDIT: Ignore the factories, I don't need them, I think I was just over-complicating things.
Currently the objects load their data, and child data by way of webservice calls scattered about in the objects.
Most of the objects constructors are marked private, and they have static methods like "LoadByID(int)" to retrieve them from the db.
Then when the property which contains it's child methods is referenced, if it's private "child list" variable is empty, it calls Child.GetAllChildrenFor(this); which is a static method of Child, and "this" is the Parent class. It calls the webservice, creates all the Childs, and returns a list of them, each of which has a reference to it's parent.
I know this isn't quite right...but I don't know where I've gone wrong, or how to correct it to be able to REMOVE the webservice calls and put in a Repository interface that can be real or mock... and where in the code I set which to use!
EDIT2: The code is something along these lines currently
public interface IEntity
{
int ID { get; set; }
}
public class Parent : IEntity
{
public int ID { get; set; }
private Children children;
public Children Children
{
get
{
if (this.children == default(Children))
{
this.children = Children.GetChildrenFor(this);
}
return this.children;
}
}
}
public class Children : List<Child>
{
private Children() { }
public static Children GetChildrenFor(Parent parent)
{
Children children = new Children();
DataSet childrenDataSet = new DataSet();
using (webservice)
{
childrenDataSet = webservice.Retrieve(parent.ID)
}
if (childrenDataSet.HasRows())
{
foreach (DataRow dr in childrenDataSet.Tables[0].Rows)
{
Child rr = Child.LoadByDataRow(dr);
rr.Parent = parent;
children.Add(rr);
}
}
return children;
}
}
public class Child : IEntity
{
public Parent Parent { get; set; }
public int ID { get; set; }
internal static Child LoadByDataRow(DataRow dr)
{
Child child = new Child();
child.ID = dr.Field<int>("ID");
child.GrandChild = GrandChildren.GetGrandChildrenFor(this);
return child;
}
}