The Elephant and the Silverlight

Thoughts and analysis of Silverlight for business applications

Page List

    Getting to know the EntityCollection

    Meet the EntityCollection

    Other than the DomainClient, the EntityCollection is probably the least known part of RIA Services. This is a problem as the EntityCollection is probably the most used class other than the Entity itself. The primary job of the EnityCollection is to represent one to many relationships between entities. It does this in two different ways.

    The Detached EntityCollection

    When your entity is detached, meaning it isn’t tracked by an EntityList, the EntityCollection acts as a container of related entities. You can create an entire hierarchy of objects and it will all be held together by the EntityCollections. When you finally add your entity to an EntityList all of the related entities from the EntityCollections are also added to their respective EntityLists. This begins the EntityCollection’s other mode.

    The Attached EntityCollection

    For entities tracked by EntityLists, the EntityCollection takes on some of the attributes of a CollectionView where the EntityList is now the underlying container of entities. The EntityCollection listens for changes in the EntityList and uses a filter to determine what entities should be viewed, much like a CollectionView. However, unlike a CollectionView the EntityCollection does not have a Refresh method. This means that the filter method has to be static, only changes to the EntityList will change the contents of the EntityCollection.

    Adding and Removing objects through the EntityCollection

    The EntityCollection has Add and Remove methods and while dealing with detached entities these methods work fine. However, using those methods with attached entities can be confusing. The EntityCollection’s add and remove do not actually delete the entity, it just clears the values defined in the AssociationAttribute. This means that you have set the ParentID in the child table to Null or 0. This is probably not what you meant to do. In general, I would suggest not using the Add and Remove method of the EntityCollection when dealing with attached entities. Instead, deal directly with the EntityLists instead.

    Continue to Advanced EntityCollection


    Permalink | Comments (5) | Post RSSRSS comment feed

    Comments

    topsy.com

    Tuesday, September 01, 2009 12:04 AM

    pingback

    Pingback from topsy.com

    Twitter Trackbacks for
            
            The Elephant and the Silverlight | Getting to know the EntityCollection
            [riaservicesblog.com]
            on Topsy.com

    swo Austria

    Wednesday, September 02, 2009 8:33 AM

    swo

    I would suggest not using the Add and Remove method of the EntityCollection when dealing with attached entities......

    Does anybody knows why it has this behaver?
    Dealing sometimes with the EntityList and sometime with the EntityCollection can confuse.

    ColinBlair

    Wednesday, September 02, 2009 9:18 AM

    ColinBlair

    @swo if you read the Advanced EntityCollection post I show the actual Add and Remove code which explains why it works this way.

    swo Austria

    Friday, September 04, 2009 6:04 AM

    swo

    Hi Colin
    I red the Advanced EntityCollection post. But what i want to figure aut is why it was coded in this way. There must be a good design decision to work in this way. If the EntityCollection.Remove function didn't work as supposed why is it public? What is it good for?

    ColinBlair United States

    Sunday, September 06, 2009 10:37 AM

    ColinBlair

    swo, please read my newest entry on RIA Services and parent child terminology. I hope that helps.
    www.riaservicesblog.net/.../...-have-children.aspx

    Add comment


    (Will show your Gravatar icon)

      Country flag

    Click to change captcha
    biuquote
    • Comment
    • Preview
    Loading