In my previous 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"

 

image

 

Basically this is all about the server part of the application. In the next post I will guide you how to create extremely simple SilverLight test application that interacts with the provided service. Also I will try to implement the whole sample application in VB.NET as there is strong demand for that. Enjoy!


About the Author

Dimitar Kapitanov

Dimitar Kapitanov is Team Lead in Telerik Platform Team

Comments

Comments are disabled in preview mode.