WCF RIA Services and DTO with association

This post will be short, I notice that some people are asking about how to send an object graph which will include an association to other objects. First of all, be careful with the distribution of an object graph, wrong design can affect performance in a way that to much data is passed over the wire.

Here is an simple Order class, this class has OrderRows:

public class Order
{
    [Key]
    public int OrderID { get; set; }

    public IEnumerable<OrderRow> Rows { get; set; }
}


public class OrderRow
{
    public int OrderID { get; set; }

    [Key]
    public int OrderRowID { get; set; }

    public string Name { get; set; }
}


Here is my DomainService where I have specified a query method for the Orders:

[EnableClientAccess()]
public class DomainService1 : DomainService
{
        public IEnumerable<Order> GetOrders()
        {
            return new List<Order>()
                        {
                            new Order()
                            {
                                OrderID = 0,
                                Rows = new List<OrderRow>()
                                        {
                                            new OrderRow() { OrderID = 0, Name = "Row 0", OrderRowID = 0 },
                                            new OrderRow() { OrderID = 0, Name = "Row 1", OrderRowID = 1 }
                                        }
                            },
                                                        new Order()
                            {
                                OrderID = 1,
                                Rows = new List<OrderRow>()
                                        {
                                            new OrderRow() { OrderID = 1, Name = "Row 0", OrderRowID = 0 },
                                            new OrderRow() { OrderID = 1, Name = "Row 1", OrderRowID = 1 }
                                        }
                            }

                        };
        }
}

Note: I just fake the loading of data in the GetOrder query method, normally I should of course use Entity Framework 4.0, nHibernate or other ORM.

If we now load the Orders from the client, the Order object we will get will not include the Rows property of the order. So the association will not be passed with the Order object. To include an association we need to add the IncludeAttribute, and also the AssociationAttribute:

public class Order
{
        [Key]
        public int OrderID { get; set; }

        [Include]
        [Association("Order_OrderRows", "OrderID", "OrderID")]
        public IEnumerable<OrderRow> Rows { get; set; }
}


The AssociationAttribute  is needed to specify the association between the Order and the OrderRows. The first argument is only a name of the association, the second is the main key, in this case the Order’s OrderID key, the last argument is the associated objects key which holds the value of the Order’s OrderID, in this case the OrderRow’s OrderID. The Include attributes make sure the Rows property will be included when the object is passed to the client. I hope some of you find this post useful.

If you want to know when I publish some new posts, or just follow my life, you can find med on twitter: http://www.twitter.com/fredrikn

Published Friday, November 20, 2009 10:46 AM by Fredrik N

Comments

# Twitter Trackbacks for WCF RIA Services and DTO with association - Fredrik Norm??n [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 WCF RIA Services and DTO with association - Fredrik Norm??n         [asp.net]        on Topsy.com

# re: WCF RIA Services and DTO with association

Friday, November 20, 2009 7:43 AM by Niclas Pehrsson

I think it's scary when the framework forces us to have get; set; on every property, we can't protect the data inside our objects.

And for example having get; set; on collections could lead to bugs that could be hard to find.

I think this is a big problem for silverlight and a big step back there we handle data as they were datarows in a datatable, when we really wants to use encapsulation on our objects to be able to work with a more object oriented approach.

# Dew Drop &#8211; November 20, 2009 | Alvin Ashcraft&#039;s Morning Dew

Pingback from  Dew Drop &#8211; November 20, 2009 | Alvin Ashcraft&#039;s Morning Dew

# re: WCF RIA Services and DTO with association

Friday, November 20, 2009 8:36 AM by Fredrik N

@Niclas Pehrsson:

You can do that now, or sort of doing it.. I will short publish a blog post about it.

# WCF RIA Services – What you need to know when creating DTO/Presentation Model

Friday, November 20, 2009 11:37 AM by Fredrik Normén

Note: This is based on the WCF RIA Services Beta, so the code in this post can change in a future release

# WCF RIA Services – What you need to know when creating DTO/Presentation Model

Friday, November 20, 2009 11:41 AM by Cornerstones utvecklarblogg

Note: This is based on the WCF RIA Services Beta, so the code in this post can change in a future release

# re: WCF RIA Services and DTO with association

Saturday, November 21, 2009 7:41 AM by Steve

Since the move to WCF, are associations required - or can just datacontract/datamember attributes be used ?

# WCF RIA Services and a guide to use DTO/”Presentation Model”

Saturday, November 28, 2009 9:10 AM by Fredrik Normén

NOTE: Examples in this blog post is based on the VS 2010 Beta 2, WCF RIA Services PDC Beta, some changes

# WCF RIA Services and a guide to use DTO/”Presentation Model”

Saturday, November 28, 2009 9:15 AM by ASPInsiders

NOTE: Examples in this blog post is based on the VS 2010 Beta 2, WCF RIA Services PDC Beta, some changes

# WCF RIA Services and a guide to use DTO/”Presentation Model”

Saturday, November 28, 2009 9:26 AM by Cornerstones utvecklarblogg

NOTE: Examples in this blog post is based on the VS 2010 Beta 2, WCF RIA Services PDC Beta, some changes

# re: WCF RIA Services and DTO with association

Friday, January 29, 2010 7:42 PM by NW

Something that I've really been struggling with when trying to use a presentation model is how do you insert multiple new objects that are associated with each other if you're not using GUID identifiers? I've got a presentation model on top of a LINQ to SQL data access layer. When the new objects arrive on the server, they are holding references to each other in the appropriate properties, but the foreign-key ID values are meaningless because they can't be set on the client (my DB uses int identifiers). Each object gets its own Insert call, but you can't just convert/project the one object passed in to the L2S class and add it to the L2S context, because then the two objects no longer have references to each other. Trying to insert both objects in one Insert is not scalable to more complex structures, and you have to deal with the fact that the other inserts still get called so you have to "ignore" them. Is this possible to do?

Leave a Comment

(required) 
(required) 
(optional)
(required)