Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a design pattern for light & heavy versions of an object?

I have the need for both light-weight, and heavy-weight versions of an object in my application.

  • A light-weight object would contain only ID fields, but no instances of related classes.
  • A heavy-weight object would contain IDs, and instances of those classes.

Here is an example class (for purpose of discussion only):

public class OrderItem
{
    // FK to Order table
    public int OrderID;
    public Order Order;

    // FK to Prodcut table
    public int ProductID;
    public Product Product;

    // columns in OrderItem table
    public int Quantity;
    public decimal UnitCost;

    // Loads an instance of the object without linking to objects it relates to.
    // Order, Product will be NULL.
    public static OrderItem LoadOrderItemLite()
    {
        var reader = // get from DB Query
        var item = new OrderItem();
        item.OrderID = reader.GetInt("OrderID");
        item.ProductID = reader.GetInt("ProductID");
        item.Quantity = reader.GetInt("Quantity");
        item.UnitCost = reader.GetDecimal("UnitCost");
        return item;
    }

    // Loads an instance of the objecting and links to all other objects.
    // Order, Product objects will exist.
    public static OrderItem LoadOrderItemFULL()
    {
        var item = LoadOrderItemLite();
        item.Order = Order.LoadFULL(item.OrderID);
        item.Product = Product.LoadFULL(item.ProductID);
        return item;
    }
}

Is there a good design pattern to follow to accomplish this?

I can see how it can be coded into a single class (as my example above), but it is not apparent in which way an instance is being used. I would need to have NULL checks throughout my code.

Edit: This object model is being used on client side of client-server application. In the case where I'm using the light-weight objects, I don't want lazy load because it will be a waste of time and memory ( I will already have the objects in memory on client side elsewhere)

like image 260
TheSean Avatar asked Apr 25 '12 19:04

TheSean


2 Answers

Lazy initialization, Virtual Proxy and Ghost are three implementations of that lazy loading pattern. Basically they refer to load properties once you need them. Now, I suppose you'll be using some repo to store objects so I'll encourage you to use any of the ORM tools available. (Hibernate, Entity Framework and so on), they all implement these functionality free for you.

like image 159
Erre Efe Avatar answered Nov 10 '22 23:11

Erre Efe


Have you considered using an ORM tool like NHibernate for accessing DB? If you use something like NHibernate, you would get this behavior by means of lazy loading.

Most ORM tools do exactly what you are looking for within lazy loading - they first get the object identifiers, and upon accessing a method, they issue subsequent queries to load the related objects.

like image 31
Sathya Srinivasan Avatar answered Nov 11 '22 01:11

Sathya Srinivasan