Search code examples
silverlightwcf-ria-servicesbatch-insert

Send custom complex objects to Silverlight RIA services


I've created a complex object which itself include a list of another object.

Example:

public class MyClass1
{
    private List<MyClass2> myClass2List

    [Key]
    public long Id { get; set; }

    public string Name { get; set; }

    [Include]
    [Association("FK_MyClass1_MyClass2", "Id", "MyClass1Id")]
    public List<MyClass2> MyClass2List
    {
        get
        {
            if (this.myClass2List == null)
            {
                this.myClass2List = new List<MyClass2>();
            }

            return this.myClass2List;
        }
        set { this.myClass2List = value; }
    }
}

public class MyClass2
{
    [Key]
    public long Id { get; set; }

    public string Value { get; set; }

    public long MyClass1Id { get; set; }
}

Now I want to send an instance of MyClass1 (which its MyClass2List is full) to the RIA services.

My first try was creating a get method with MyClass1 return type (to expose MyClass1) and also creating an Insert method for MyClass1 and MyClass2. It works but the MyClass2 Insert method is useless! I insert all the information in MyClass1 method. Also the get method is useless!

This is the domain service that works:

[EnableClientAccess]
public class MyDomainService : DomainService
{
    public MyClass1 GetMyClass1()
    {
        return null;
    }

    [Insert]
    public void Insert(MyClass1 myClass1)
    {
        ... (Main code)
    }

    [Insert]
    public void Insert(MyClass2 myClass2)
    {
        // I leave this method empty because I do the job in MyClass1 Insert method.
        // If I don't add this method the Add operation on MyClass2List will not work!
    }
}

As you see it doesn't have a clear implementation. Although I could use the traditional WCF Silverlight-Enabled services and simply use a single method to insert the MyClass1 instance.

Note that I tried the [Invoke] method (A simple invoke method which gives the MyClass1 as an input parameter) but then "MyClass1.MyClass2List" is empty!!!

How can I send a complex object to RIA services without having such useless methods and dirty implementation? I need to do all my works just in a single method. I don't need tracking and etc here. Is it better to use the traditional WCF Services besides the RIA or is there another way about RIA?

Thanks in advance


Solution