ADO.NET Data Services Client context
One of the great points with EF is the eco-system around it. Even if it can be used without EF, I think that ADO .NET Data Services is one of the technologies in this eco-system. Indeed, using ADO.NET Data Services with EF implies almost no code to write some LINQ queries in the client tier or to save the changes.
However, when we use the generated proxy in the client tier there are some bad points:
- For example with Northwind, if we add an orderDetail to the order.OrderDetails collection, the orderDetail.Order is still null. In reverse, if we set the orderDetail.Order, the orderDetail isn’t added to the order.OrderDetails collection.
- Moreover, if we load the orders and then the orderDetails, the OrderDetails collection of the orders is empty and the orderDetails order is null. However, because OrderID is a part of the orderDetails key, we should be able to get automatically the relation as EF do.
//To have this relation with ADO.NET Data Services, we have to use the Expand method in the query or to use the LoadProperty method.
With EF4, we can have FK in the model (with EF v1, we have only the FKs which are included in the PK) which is really great because, in theory, it’s now possible to reform the relations.
- Another point: if you want to add an order with some (new) orderDetails, you need to call the Add for the order, then the Add for all orderDetails (the MS client context ignore the navigation properties) and then you also need to call the methods SetLink and AddLink (because the order key is an Identity). So in fact this is the code you have to write:
foreach (var od in o.OrderDetails)
context.AddLink(o, "OrderDetails", od);
context.SetLink(od, "Order", o);
- One more point, the context doesn’t track changes alone. You have to call yourself the UpdateObject method.
- Finally, if we add an orderDetail to the context and only then the associated order, when we call the SaveChanges, we have an exception because the context doesn’t change the Add order
So my idea was to code another client tier context. But of course, I wanted a generic solution. It means that I wanted to code something usable with every model. To realize it, I used a T4 template based on the edmx (in the server tier). This implies two things:
- We need to use EF in the server tier
- We need to have the server tier edmx when we code the client tier
My template fixes all the above issues.
You can download it here.
You also can download all the solution (with unit tests) here and the DB creation script here.
//I admit that some parts of the code aren’t great but ADO.NET Data Services is an extremely “closed” technology so it isn’t so easy and I would probably have some better ideas if I didn’t write it so late in the night