I am going through Domain Driven Design by Eric Evans where he outlines the interplay between Repositories and Factories. The repository itself will call an DB interface to get a result set. This result set would then be passed to a factory that would understand that result set to reconstitute the object.
What if the data was hierarchical in nature, like some sort of tree structure. For example:
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
public Foo Parent { get; set; }
public ICollection<Foo> { get; set; }
// Other business like methods here
}
Using DDD I would have my interfaces and implementations:
public interface IFooRepository
{
Foo Get(int id);
}
public interface IFooFactory<TSource>
{
Foo Create(TSource source);
}
public class SqlFooRepository: IFooRepository
{
private readonly IFooDao dao;
private readonly IFooFactory<SqlDataReader> factory;
public SqlFooRepository(IFooDao dao, IFooFactory factory)
{
this.dao = dao;
this.factory = factory;
}
public Foo Get(int id)
{
var resultSet = dao.Find(id);
return factory.Create(resultSet);
}
}
public class SqlFooFactory: IFooFactory<SqlDataReader>
{
public Foo Get(SqlDataReader reader)
{
var foo = new Foo();
foo.Id = (int) reader["id];
foo.Name = (string) reader["name"];
// Do I build the children accounts here
return foo;
}
}
If I try to build the children in the factory then I need access to the repo again. If I do it in the Repo I feel like I am doing work that should be for the factory. Not sure how to tackle this one.
One thought I had is that the Foo is not the aggregate root but rather the FooTree is the aggregate root. So trying to get any Foo I would need to create the entire tree, which means I could pass a collection of Foo objects to a FooTreeFactory.
Any help would be very appreciated.