KISS: Data Relations

by Chris 12. June 2009 05:45

As I've said before, my favorite data handling approach is POCOs all the way from the server to the client utilizing technologies like ADO.NET Entity Framework, ADO.NET Data Services, LINQ, and WCF. As the entities created in the ORM is propagated to all tiers (even onto the mobile device), we already have all that we need to work with data in an efficient way. Even offline, as I showed in my post KISS: Online and Offline Data.

I've had a few questions on how to handle data relations, specifically related to the (RESTful) data tier of the KISS Architecture. Beginning in the mobile client, here's the code for a typical "buy" button in a sales application:

OrderDetail orderDetail = new OrderDetail();
orderDetail.ProductID = selectedProductID;
orderDetail.Quantity = selectedQ
uantity;

// New order?
if(currentOrder == null)
{
   
Order currentOrder = new Order();
    currentOrder.OrderDetails =
new OrderDetail[1];
    currentOrder.OrderDetails[0] = orderDetail;
}
else
{
   
OrderDetail[] orderDetails = currentOrder.OrderDetails;
   
Array.Resize(ref orderDetails, orderDetails.Length + 1); 
   
Array.Copy(new OrderDetail[] { orderDetail }, 0, orderDetails,
        orderDetails.Length - 1, 1);
}

The order is created (if it's a new one), and then the selected product and quantity is added as a new order (details) row. Of course, we could do some extra roundtrips to the server and provide the necessary foreign keys, but that is both more efficient and simpler to handle on the server side. This is the code from the "domain" that add a new order:

public void AddOrder(string customerID, Order order)
{
   
// Get customer
    var q = from c in data.CustomerSet
           
where c.CustomerID == customerID
           
select c;
   
Customer customer = q.SingleOrDefault();

   
// Create new order
    data.AddObject("OrderSet", order);
    data.SetLink(order,
"Customer", customer);
    data.SaveChanges();

   
// Add product(s) to order
    foreach(OrderDetail orderDetail in order.OrderDetails)
    {
       
Product product = (from p in data.ProductSet
           
where p.ProductID == orderDetail.ProductID select p).Single();
        data.AddObject(
"OrderDetailSet", orderDetail);
        data.SetLink(orderDetail,
"Order", order);
        data.SetLink(orderDetail,
"Product", product);
        orderDetail.UnitPrice = product.UnitPrice !=
null ?
            (
decimal)product.UnitPrice : 0;
        orderDetail.Discount = 0;
    }
    data.SaveChanges();
}

The data variable points to the ADO.NET Data Services, and entities are simply added with the AddObject method. Note how the relations are defined (i.e. foreign keys are set) using the SetLink method.

Using the above approach means that the client can create simple object hierarchies that are later passed to business logic components (domains) that handle the data integrity as well as other business rules. This minimizes the required bandwidth with as few calls as possible, and relieve the client code from handling things about the data that are better done on the server.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Chris | Compact Framework

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen