Updating data into ADO.NET Data Services which based on LINQ to SQL

We continue to investigate possibilites of use LINQ to SQL as model of data for ADO.NET Data Services. Today we will talk about possibility of change data.

I will remind, that ADO.NET Data Services platform gives us possibility not only to select data through web service, but also to update them using methods POST/PUT/MERGE/DELETE. Within data model it is necessary to implement IQueryable interface (for each entity set) for data selecting, and IUpdatable (for datacontext class) for updating.

Let’s go back to LINQ to SQL. If we will try to create LINQ to SQL data model, give access to them via ADO.NET Data Services and try to update data we will find out, that web service generates an error. At superficial studing of a cause of error it is possible to guess, that the problem consists that LINQ to SQL data model do not implement IUpdatable. The reasons of such outcome are obvious: IUpdatable interface is component of ADO.NET Data Services. LINQ to SQL has been released much earlier, than ADO.NET Data Services. Thus it is possible to make a conclusion – implement yet not ready interface LINQ to SQL team could not.

Nevertheless. this restriction really serious and can essentially affect the final solution. Of course, it is possible to refuse to use LINQ to SQL, but it not our method :) How to solve this problem? It is very simple – implement IUpdatable interface manually :)

However, when we see IUpdatable interface it is possible to become despondent, because there it is necessary to implement 12 method with opaque logic.

partial class NewsDataContext : IUpdatable
{
    #region IUpdatable Members
 
    public void AddReferenceToCollection(object targetResource, string propertyName, object resourceToBeAdded)
    {
        throw new System.NotImplementedException();
    }
 
    public void ClearChanges()
    {
        throw new System.NotImplementedException();
    }
 
    public object CreateResource(string containerName, string fullTypeName)
    {
        throw new System.NotImplementedException();
    }
 
    public void DeleteResource(object targetResource)
    {
        throw new System.NotImplementedException();
    }
 
    public object GetResource(System.Linq.IQueryable query, string fullTypeName)
    {
        throw new System.NotImplementedException();
    }
 
    public object GetValue(object targetResource, string propertyName)
    {
        throw new System.NotImplementedException();
    }
 
    public void RemoveReferenceFromCollection(object targetResource, string propertyName, object resourceToBeRemoved)
    {
        throw new System.NotImplementedException();
    }
 
    public object ResetResource(object resource)
    {
        throw new System.NotImplementedException();
    }
 
    public object ResolveResource(object resource)
    {
        throw new System.NotImplementedException();
    }
 
    public void SaveChanges()
    {
        throw new System.NotImplementedException();
    }
 
    public void SetReference(object targetResource, string propertyName, object propertyValue)
    {
        throw new System.NotImplementedException();
    }
 
    public void SetValue(object targetResource, string propertyName, object propertyValue)
    {
        throw new System.NotImplementedException();
    }
 
    #endregion
}




I will not consider details of implementing of each method. These methods will seem to you simple when you look at them in details. That you could use now already possibility of data updating through web service (based on LINQ to SQL model) I offer you one of variants of implementation of the given interface. You can download it (and use in your projects) by using link at the end of this post.



Important remark. As you see, the interface is implemented in a partial-class. It is essential, because if you implement it in autogenerated file “*.designer.cs” you can lose this code at any data model change.



IUpdatable implementation:


DataContext.Updatable.cs

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>