Read More on Telerik Blogs
June 29, 2009 Productivity
Get A Free Trial

In the first part of this post we looked into providing data to the client using ADO.NET Data Services and Telerik OpenAccess. However it is very rare just to present data, as usually some processing is done by the application on the client side, data gets modified and it is pushed back to the server for persistence. With ADO.NET Data Services this is done by implementing the IUpdateable interface by the data context class used on the server.

NOTE: We do not implement IExpandProvider at the moment.


They say that a picture is worth a thousand words, so same is true for the code – I just present you the code required for updates:

 

#region IUpdatable Members

public   object   CreateResource( string   containerName,   string   fullTypeName)
{
   
Type   t   =   Type .GetType(fullTypeName) ;
   
Debug.Assert(t   ! =   null ) ;   // assume can find type
    object   resource   =   Activator .CreateInstance(t) ;
   
Scope.Add(resource) ;
   
return   resource ;
}

public   object   GetResource(IQueryable   query,   string   fullTypeName)
{
   
object   resource   =   null;

   
foreach   ( object   o   in   query)
   
{
       
if   (resource   ! =   null )
       
{
           
throw   new   Exception ( "Expected a single response" ) ;
       
}
       
resource   =   o ;
   
}

   
if   (resource   ==   null )
       
return   new   Exceptions.NoSuchObjectException( "Object cannot be found." ,   null ) ;

   
if   (fullTypeName   ! =   null   &&   resource.GetType()   ! =   Type .GetType(fullTypeName))
       
throw   new   Exception ( "Unexpected type for resource" ) ;

   
return   resource ;
}

public   object   ResetResource( object   resource)
{
   
Debug.Assert(resource   ! =   null ) ;
   
Scope.Refresh(resource) ;
   
return   resource ;
}

public   void   SetValue( object   targetResource,   string   propertyName,   object   propertyValue)
{
   
PropertyInfo   pi   =   targetResource.GetType().GetProperty(propertyName) ;
   
if   (pi   ==   null )
       
throw   new   Exception ( "Can't find property" ) ;
   
pi.SetValue(targetResource,   propertyValue,   null ) ;
}

public   object   GetValue( object   targetResource,   string   propertyName)
{
   
PropertyInfo   pi   =   targetResource.GetType().GetProperty(propertyName) ;
   
if   (pi   ==   null )
       
throw   new   Exception ( "Can't find property" ) ;
   
return   pi.GetValue(targetResource,   null ) ;
}

public   void   SetReference( object   targetResource,   string   propertyName,   object   propertyValue)
{
   
SetValue(targetResource,   propertyName,   propertyValue) ;
}

public   void   AddReferenceToCollection( object   targetResource,   string   propertyName,   object   resourceToBeAdded)
{
   
PropertyInfo   pi   =   targetResource.GetType().GetProperty(propertyName) ;
   
if   (pi   ==   null )
       
throw   new   Exception ( "Can't find property" ) ;
   
IList   collection   =   ( IList )pi.GetValue(targetResource,   null ) ;
   
collection.Add(resourceToBeAdded) ;
}

public   void   RemoveReferenceFromCollection( object   targetResource,   string   propertyName,   object   resourceToBeRemoved)
{
   
PropertyInfo   pi   =   targetResource.GetType().GetProperty(propertyName) ;
   
if   (pi   ==   null )
       
throw   new   Exception ( "Can't find property" ) ;
   
IList   collection   =   ( IList )pi.GetValue(targetResource,   null ) ;
   
collection.Remove(resourceToBeRemoved) ;
}

public   void   DeleteResource( object   targetResource)
{
   
Scope.Remove(targetResource) ;
}

public   void   SaveChanges()
{
   
Scope.Transaction.Commit() ;
}

public   object   ResolveResource( object   resource)
{
   
Scope.Retrieve(resource) ;
   
return   resource ;
}

public   void   ClearChanges()
{
   
Scope.Transaction.Rollback() ;
}

#endregion

 

 

Yes, this is just what you need to accomplish to have updates pushed back to the server in this example.
To see the service up and running make your web site the startup site and just hit F5. This will start the test web server and the service. Then open a browser and navigate to the service: "http://localhost:<port>/OADataServices/OADataService.svc"
Basically this is all about the server part of the application. In the next post I will guide you how to create a common master-details application that will display the orders and their details with a SIlverLight front-end. The application will be able to insert, update and delete Orders(together with their details) !


About the Author

Sam Basu

Sam Basu is a technologist, author, speaker, Microsoft MVP, gadget-lover and Progress Developer Advocate for Telerik products. With a long developer background, he now spends much of his time advocating modern web/mobile/cloud development platforms on Microsoft/Telerik technology stacks. His spare times call for travel, fast cars, cricket and culinary adventures with the family. You can find him on the internet.