I have these classes:
public class Order
{
private Lazy<IEnumerable<Volume>> _volumes;
long ID { get; private set; }
string Description { get; private set; }
IEnumerable<Volume> Volumes { get { return _volumes.Value; } }
//etc..
}
public class Volume
{
long ID { get; private set; }
string Description { get; private set; }
//etc..
}
I need to instantiate and populate several Orders. To supply the objects of these classes I have these Repositories:
public interface IOrderRepository
{
Order GetByID(long ID);
IEnumerable<Order> GetAll();
Order Create(long ID, string Description);
void Update(Order O);
void Delete(Order O);
}
public interface IVolumeRepository
{
Volume GetByID(long ID);
IEnumerable<Volume> GetAll(Order O);
Volume Create(long ID, string Description, Order O);
void Update(Volume V);
void Delete(Volume V);
}
I don't need to populate the Volumes of each Order upon instantiation, it would be best to implement lazy loading, in my opinion, as there can be a lot of Volumes.
So how should I let the Order know where to fetch the Volumes? I have this code that populates the Order it is instantiated:
//Interface
public interface IVolumeFetcher { IEnumerable<Volume> GetAll(Order O); }
//Order Constructor
public Order(long ID, string Description, IVolumeFetcher VF)
{
_volumes = new Lazy<IEnumerable<Volume>>(() => VF.GetAll(this));
}
This gives the Order a reference to the Repository, it doesn't seem a good idea to me.
I could keep the Order's Volumes a simple List and fill it when needed:
public List<Volume> Volumes { get; private set; }
But this approach would be easily corrupted, any caller could add a Volume to the list and it wouldn't be persisted to the DB. Btw, this is a three layered application, with a Order and Volume class for each layer, as well as a repository.
virtual
.