The Elephant and the Silverlight

Thoughts and analysis of Silverlight for business applications

Page List

    ApplyState and ExtractState with RIA Services Contrib

    UPDATE: There is a new version of the ApplyState and ExtractState at http://www.riaservicesblog.net/Blog/post/Updated-ApplyStateExtractState-Library.aspx

    Tomorrow Soon  Some day I am going to updating RIA Services Contrib with some new features. The existing T4 templates, as fun as they are, are going to left alone for now. I am going to look at them again after the PDC version of RIA Services is released. In the meantime, I am going to release some new utility classes that I have been putting together. The release is attached to this blog post and Codeplex will be updated tomorrow. 

    This new contrib release adds ApplyState and ExtractState methods to Enity objects, an Import method to EntityList<T>, and an Export method to all IEnumerable<T> where T : Entity collections. To use the new code simply reference the RIAServicesContrib.dll and import the RiaServicesContrib.Extensions namespace.

    Here are some usage examples:

    Entity cloning

    Person newPerson = newPerson();
    newPerson.ApplyState(Nothing, existingPerson.ExtractState(ExtractType.ModifiedState);
    newPerson.PersonId = Guid.NewGuid();
    context.Persons.Add(newPerson);

    Partial Save (i.e. save only a single change instead of the whole DomainContext)

    PersonDomainContext tempContext = new PersonDomainContext();
    Person savePerson = newPerson();
    tempContext.Persons.Add(savePerson);
    savePerson.ApplyState(originalPerson.ExtractState(ExtractType.OriginalState, ExtractType.ModifiedState);
    tempContext.SubmitChanges();

    Export EntityList and save to Isolated Storage

    using IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()
    {
        using IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.OpenOrCreate, isf)
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(List<EntityStateSet>));
            serializer.WriteObject(isfs, context.Persons.Export());
            isfs.Close();
        }
    }

    Import information from Isolated Storage into EntityList

    using IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()
    {
        using IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Open, isf)
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(List<EntityStateSet>));
            context.Persons.Import(serializer.ReadObject(isfs));
            isfs.Close();
        }
    }

    Some things which might not be obvious is that the Export functionality can also be done against a LINQ query, allowing partial exports of entities. When I update CodePlex I am going to include better documentation of the API.

    RiaServicesContrib.zip (12.97 kb)


    Permalink | Comments (4) | Post RSSRSS comment feed

    Comments

    Alex Y United States

    Friday, August 21, 2009 1:15 PM

    Alex Y

    Hi Colin,

    Thanks a lot for your EF utility library.
    It definitely helps to understand some important moments from  RIA Services/ EF interconnections.

    Looking forward for new updates.

    Eric United States

    Friday, September 11, 2009 1:23 PM

    Eric

    Excellent stuff Smile

    I'm very pleased with your breadth and depth of knowledge which you so willingly share with the greater Microsoft developer community.  I'm a long time MS/Java developer and truly enjoy your retrospective scruntinzation of current RIA services offering.  Your examination and in depth analysis of the RIA Services "DomainService" class was particularly illuminating.  

    The usage of Proxy -> Context -> Implemenation is a very common pattern in MS API's but its exceedingly relevant to point that out so we do not loose the meaning of the conversation and get mired down in semantics.

    I'm presently looking into the pros and cons of using LinqSQL or the entity framework or both.  If you would offer some enticing tid-bits from your prospective in a future blog entry or response it would be most appreciated.  

    Thank you again for the fantastic work Smile

    Eric Cotter

    ColinBlair United States

    Monday, September 14, 2009 1:29 PM

    ColinBlair

    @Eric - In the long term, EntityFramework can/will do everything that L2S can do. Short term, L2S is a little easier for those who want something that Just Works Right Now. Personally, I think if you are just starting out then using L2S is a mistake as there isn't any future to L2S. That fact that we have the two seperate frameworks is a historical artifact. L2S was invented by the LINQ team as they needed something to show how you could connect to the database using LINQ. In retrospect, it was probably a mistake that L2S was ever included as part of the framework.

    Alexander Russia

    Friday, April 02, 2010 4:15 PM

    Alexander

    Very thanks! It really helped me

    Add comment


    (Will show your Gravatar icon)

      Country flag

    Click to change captcha
    biuquote
    • Comment
    • Preview
    Loading