The Elephant and the Silverlight

Thoughts and analysis of Silverlight for business applications

Page List

    Interlude – Entities do not have children

    Since this is the first interlude, let me explain what an interlude is. I have the main series of articles for this blog planned out, but every so often I will feel the need to jump ahead and give a small sample of what I will be posting about in the future. This can be triggered by a tweet, a post in the message boards, or a passing thoughts catches my fancy.

    This particular interlude is about terminology. In RIA Services, every entity should be considered a top level entity. There is no such thing as a parent child relationship in RIA Services; all entities are equal to each other. The hows and whys of this I will cover in the EntityContainer/EntityList series. The point is, the correct terminology should be Entity and Associated Entities, not Parent and Children.

    Why is this important?

    When we talk about a parent/child releationship there are certain preconceptions we bring to the table. For example, we would expect that the child cannot exist without the parent. We would call such an object an “orphan”. In RIA Services, as there are no children, there are no orphans. If you delete the “parent” the “children” are still tracked in their respective EntityList, all that happened is that the key value that uses to be shared with the deleted entity was cleared severing the association. Granted, you may or may not fail a foreign key check when you get back to the database but that is a matter of correct programming.

    This goes back to swo’s question back on the EntityCollection where he asked why the Add and Remove methods were designed in the way that they were. The answer is here in the question of naming. If you use the words parent and child then removing the child from the parent would seem to imply that the child should be deleted. However, if you use the words Entity and Associated Entity that removing the Associated Entity from the Entity would indicate that you are simply severing the association. As are most things are in programming, it is all about finding the proper names for things so that we can correctly understand concepts.

    Ok, one thing on why RIA Services is designed this way

    RIA Services is actually designed to work a lot like a database does. If you created a SQL Database where all of the foreign keys were nullable and you had cascading turned on you would have something that would act a lot like RIA Services does. That database design would have the virtue of being able to model and other database design that you can come up with in SQL Server. Just something to think about.

    (Do not actually go and design a database that way, what works really well for a data model works horribly for an actual database.)


    Permalink | Comments (6) | Post RSSRSS comment feed

    Comments

    Nikhil Kothari United States

    Monday, September 07, 2009 2:33 PM

    Nikhil Kothari

    Colin,
    As usual insightful.

    Yes, indeed you've hit upon a key distinction - we look at entities as a flat set of lists, with associations that can be used to navigate, rather than define ownership or lifetime.

    However the parent-child relationship is also useful... its not supported in what is public today, but is an intended feature, to support compositional scenarios. The classic example here is Order - OrderDetail.

    ColinBlair United States

    Tuesday, September 08, 2009 10:15 AM

    ColinBlair

    This additional parent-child relationship feature you are talking about for the future, is it going to be similar to the EF complex types? Yeah, I can see how you could implement that. I assume that in that scenario only the parent is the actual Entity and it wouldn't be possible to "lazy load" the children directly as they are not, themselves, entities?

    My only concern would be the LoadState and ExtractState methods. Recreating those two methods myself as extension methods without actually copying your code wasn't too difficult since the code is so straight forward. Recreating them with the ability to extract and import child code would be much harder. If you could either include the needed code in a sample or actually include public versions of the LoadState and ExtractState that work like mine then that would be great.

    Nikhil Kothari United States

    Wednesday, September 09, 2009 10:00 AM

    Nikhil Kothari

    Its not exactly the same as EF complex types. In the example, both Order and OrderDetail would be entities. There are lots of facilities on entity - change tracking, original state etc. that we want to preserve.

    The primary thing we're enabling is tracking changes at the root of the composition level, i.e. if you change the children, the parent gets dirty. If any of the children or the parent are part of the submit, the entire parent-child composition/hierarchy gets sent to the server.

    The parent/children are still stored in the same EntityContainer, and still allow navigation using EntityRef/EntityCollection. Removing a parent from EntityList will also remove children from their EntityList.

    So I don't think it needs a new implementation of LoadState/ExtractState, given a composition is mostly an additional qualification on the existing association concept that affects how changes are submitted, and dirty state is aggregated.

    Also means that child entities should be able to lazily loaded if you want. Example, only fetch the Orders, and fill in the details later.

    ColinBlair United States

    Wednesday, September 09, 2009 10:17 AM

    ColinBlair

    Excellent, I look forward to revising my EntityCollection and EntityRef articles with the new abilities.

    Kasimier Buchcik (Casimodo72) Germany

    Friday, September 11, 2009 11:12 PM

    Kasimier Buchcik (Casimodo72)

    If one would like more on terminology, then one could also mention Aggregation vs. Composition.

    See
    tcle.wordpress.com/.../

    Theo Albers Netherlands

    Saturday, September 26, 2009 9:45 AM

    Theo Albers

    Hi Nikhil, I do hope you're considering support for sending entity trees to the server via a service operation as well. There is more than validation and persistence.

    Add comment


    (Will show your Gravatar icon)

      Country flag

    Click to change captcha
    biuquote
    • Comment
    • Preview
    Loading