How To Perform CRUD Operations in ADO.NET Data Services With Custom Provider
After we learned how to consume an
ADO.NET data service with a .Net
client in the previous post, the next
thing to learn is how to do CRUD
(Create, Update and Delete) operations
with the client we built. This post will
explain how to use these operations.
The IUpdatable Interface
In order to enable a a custom provider to do CRUD operations the data
context object need to implement the IUpdatable interface. That interface can be
found in the System.Data.Services namespace. The interface include a variety of
methods to implement and in the example I will show only a couple of those methods.
Returning to the example of the last post, I changed a bit the CoursesDataContext
to implement the IUpdatable interface and also to hold a static Course list which will
be the object that the data service will use.
The Data Context Example
I didn’t implemented the following methods – RemoveReferenceFromCollection,
AddReferenceToCollection, ClearChanges, ResetResource and SetReference.
The reason is that in my example I don’t use them.
The Calling Method Example
- Create – when I want to add a new course I use the method AddToCourses of
the proxy or AddObject method (which I need to supply the name of the
resource which the object will the added to). After the call for one of these
method, a call to SaveChanges method will create the object on the
data service’s side. The following methods will be called on the data context in
the order I use - CreateResource, SetValue (for every property), SaveChanges
and ResolveResource. In the CreateResource method I use the fullTypeName
parameter to create the type with reflection, add it to the courses list and
return it as the created object. The SetValue method will be called for every
property of the object and set all them also using reflection. SaveChanges is an
empty method which do nothing because we are using an in-memory object.
ResolveResource return the object it get because we don’t need to resolve
- Update – when we want to update an existing object we call the UpdateObject
method of the proxy class. After that call the call to SaveChanges will pass the
request to the data context. The methods that will be called on the data context
will be – GetResource, SetValue, SaveChanges and ResolveResource.
The GetResource gets a IQueryable query which holds the data to update. I extract
the resource to update and return it. The other methods where explained in the
- Delete – in order to the delete an object I use the DeleteObject method followed
by the SaveChanges method. The chain of method that will be called on the data
context will be – GetResource, DeleteResource, SaveChanges and
ResolveResource. The only method that concerns us (all the other where explain
earlier) is the DeleteResource method that only removes the object it gets from
the courses list.
Lets sum up the post, in order to use CRUD operations in a custom provider
of a data service you need to implement the IUpdatable interface. I provided
an example of how to implement a simple data context and I hope it will help
you to build your own. In the next post in the ADO.NET data services I’ll
write about how to perform batch operations.