Is there a design template for light and heavy versions of an object?

I have a need for both light and heavy versions of the object in my application.

  • A lightweight object will contain only ID fields, but there will be no instances of related classes.
  • An object with a heavy weight will contain identifiers and instances of these classes.

Here is an example class (for 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 accomplish this?

I see how it can be encoded into one class (as my example above), but it is unclear how the instance is used. I would need to check for NULL in all my code.

Edit: This object model is used on the client side of the client-server application. In the case when I use light objects, I do not need a lazy load, because it will be a waste of time and memory (I will already have objects in memory on the client side elsewhere)

+6
source share
4 answers

Lazy initialization, Virtual Proxy and Ghost are three implementations of this lazy boot pattern. They mainly refer to download properties when you need them. Now, I suppose you will use some repo to store objects, so I recommend that you use any of the available ORM tools. (Hibernate, Entity Framework, etc.), They all implement these features for free for you.

+3
source

Do you consider using an ORM tool like NHibernate to access a database? If you use something like NHibernate, you will get this behavior through lazy loading.

Most ORM tools do exactly what you are looking for in lazy loading - they first get the identifiers of the objects, and when accessing the method they cause subsequent requests to load the related objects.

+2
source

It looks like you might need a data transfer object (DTO), just a "dumb" wrapper class that summarizes a business object. I usually use something like this when I need to flatten an object for display. However, be careful: excessive use leads to an anti-pattern.

But rendering the object to display is different from restricting hits on the database. As Randolph points out, if your intention is the latter, use one of the existing deferred loading patterns or, better yet, use ORM.

0
source

Take a look at the registry template, you can use it to search for objects, as well as to better manage these objects, for example, to save them in the cache.

0
source

Source: https://habr.com/ru/post/914162/


All Articles