How do you bring Denormalized Values ββto your business objects?
I have two tables.
Order - with Columns OrderID, OrderStatusID
OrderStatus - with columns OrderStatusID, Description
I have an Order object that calls the database and populates 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 designs?
I usually prefer to treat requests as Value objects . I am also using the 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;
}
}
The system I'm currently working with instantiates another business object and sets an id. Then another business object is retrieved when it is in use. eg.
Order properties
int OrderId = 5
int OrderStatusId = 3
OrderStatus OrderStatus_ref
{
get
{
if OrderStatus_ref == null
OrderStatus_ref = new OrderStatus(OrderStatusId)
return OrderStatus_ref
}
}
This is generally an idea.
You can use a SQL select statement with the Join to the OrderStatus table and include yo columns from each table ...
Select O.OrderId, O.OrderStatusId, S.Descriptiuon
From Order O
Join OrderStatus S
On S.OrderStatusId = O.OrderStatusId
Where OrderId = 23 -- or whatever
Is this a one-to-one relationship between Order and OrderStatus? I suppose it depends on the purpose, why would you have an OrderStatus table as I would argue that there is actually no need for a separate OrderStatus table?
Basically, this whole table gives you the ability to change the description of the order status. From code, you should write code according to a predefined OrderStatusID (from initial data?) Or using a description. If so, why not have an OrderStatus column in the Order table, which is an integer and can represent the enumeration type?
The My Order object is likely to include a state description field (read-only [for non-inner classes]) as well as any other similar fields.
Under the hood, my recipients (like LoadByID, LoadAll, etc.) will probably use a View (like OrdersView) that contains all of these descriptive fields. These description fields are read-only, so you don't accidentally set these fields thinking you can save your changes to the database.