How to populate Lazy object from database

I have these classes: public class Order { private Lazy _volumes; long ID { get; private set; } string Description { get; private set; } IEnumerable 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 GetAll(); Order Create(long ID, string Description); void Update(Order O); void Delete(Order O); } public interface IVolumeRepository { Volume GetByID(long ID); IEnumerable 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 GetAll(Order O); } //Order Constructor public Order(long ID, string Description, IVolumeFetcher VF) { _volumes = new Lazy(() => 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 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.

Apr 12, 2025 - 04:15
 0
How to populate Lazy object from database

I have these classes:

public class Order
{
    private Lazy> _volumes;
    long ID { get; private set; }
    string Description { get; private set; }
    IEnumerable 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 GetAll();
    Order Create(long ID, string Description);
    void Update(Order O);
    void Delete(Order O);
}
public interface IVolumeRepository
{
    Volume GetByID(long ID);
    IEnumerable 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 GetAll(Order O); }
//Order Constructor
public Order(long ID, string Description, IVolumeFetcher VF)
{
    _volumes = new Lazy>(() => 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 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.