Search code examples
ooparchitectureclass-design

How do you bring Denormalized Values Into your Business Objects?


I have two Tables.

Order - With Columns OrderID, OrderStatusID
OrderStatus - With Columns OrderStatusID, Description

I have an Order Object which calls to the database and fills its properties for use in my code. Right now I have access to Order.OrderStatusID, but in my application I really need access to the "Description" field.

How do you handle this elegantly with good OO design?


Solution

  • Usually I prefer to handle lookups as Value objects. I also use Null Object pattern.

    public class Order {
      private int statusID;
    
      public OrderStatus Status {
        get {
          return OrderStatus.Resolve(statusID);
        }
        set {
          statusID = value != null ? value.ID : null;
        }
      }
    }
    
    public class OrderStatus {
       public static OrderStatus Resolve(int statusID)
       {
         OrderStatus status = null;
         // read from cache or DB
         ...
         // if not found return Null object
         if (status == null)
           status = new OrderStatus(null, string.Empty);
         return status;
       }
    }