RIA and DTO associated objects

I am currently working on a Silverlight application and we are using RIA and MVVM.  We don’t use the default implementation where we consume Entity Framework or Linq2Sql objects, as we already have a DTO object model that we use to move all our data around.  Unfortunately a lot of the RIA samples and documentation use Entity Framework objects, which already have a lot of “magic” built in.  Our approach is good in the sense that we don’t carry the excess baggage around, but at the same time it can be challenging when trying to do some “simple” things like exposing an object that has properties of another one of my types. 

So the scenario is that I have a Customer object with a MailingAddress property of type Address, something like this:

[Serializable]

public class Customer

{

    [Key]

    public Guid Id { get; set; }

    public string Name { get; set; }

    public Address MailingAddress { get; set; }

    public string Phone { get; set; }

    public string Fax { get; set; }

}

[Serializable]

public class Address

{

    [Key]

    public Guid Id { get; set; }

    public string Address1 { get; set; }

    public string Address2 { get; set; }

    public string City { get; set; }

    public string State { get; set; }

    public string Zip { get; set; }

}

Our DomainService exposes Customer, but when I take a look at the generated code (the “g.cs” file) on the Silverlight project, the MailingAddress property is missing.  In order to tell RIA that those two objects are related, you have to use the AssociationAttribute on the Customer class, and tell it what property should be used to associate both types.  You must also use the IncludeAttribute to tell RIA to include the association.  I also had to add a MailingAddressId property to my Customer type.  So Customer now looks like this:

[Serializable]

public class Customer

{

    [Key]

    public Guid Id { get; set; }

    public string Name { get; set; }

    [Include]

    [Association("MailingAddress", "MailingAddressId", "Id")]        

    public Address MailingAddress { get; set; }

    public Guid MailingAddressId { get; set; }

    public string Phone { get; set; }

    public string Fax { get; set; }

}

The Association attribute takes 3 parameters: name, thisKey, otherKey.  The first one is just an arbitrary string, #2 and #3 are the important ones.  thisKey specifies the property from the parent object, and otherKey specifies the property from the child.  RIA then uses its magic to tie them both together.  The only other changes was that I obviously had to set my new MailingAddressId property when i load my DTO.

I could not find a lot of information on this out on the web, but this article pointed me in the right direction.

About esteban

Esteban is the Founder and Chief Technologist at Nebbia Technology, an ALM consulting and Azure-powered technology company. He is a software developer with a passion for ALM, TFS, Azure, and software development best practices. Esteban is a Microsoft Visual Studio ALM MVP and ALM Ranger, Pluralsight author, and the president of ONETUG (Orlando .NET User Group).

One thought on “How to turn on Silverlight Debugging

  1. Naveen Raina

    This was helpful. Thank you.


Leave a Reply

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

Are you human? *