Wednesday, March 31, 2010

RIA and DTO

At the moment (RC bits) WCF RIA doesn’t support custom DTO as parameters for domain service operations. If you define

Code Snippet
[DataContract]
public class MyCustomDTO
{
    [DataMember]
    public int MyPayload { get; set; }
    [DataMember]
    public string MyOtherPayload { get; set; }
}

You won't be able to do this

Code Snippet
[Invoke]
public void MyOperation(MyCustomDTO data)

Compiler will greet you with this encouraging message:

Operation named 'MyOperation' does not conform to the required signature. Parameter types must be an entity type or one of the predefined serializable types.

Well, don’t be scared, there is workaround (as usual) to make our custom DTO “RIA friendly” (or Entity type in RIA terms).

You need to do 2 simple but not obvious things

1. Make your class identifiable by providing RIA with identity property(ies)

Code Snippet
public class MyCustomDTO
{
    [Key]
    public int MyDummyKey { get; set; }

    public int MyPayload { get; set; }
    public string MyOtherPayload { get; set; }
}

Note that we don’t need WCF data contract attributes anymore – RIA code generator takes care of it

2. Expose our “entity” via domain service dummy Query

Code Snippet
[Query]
public IQueryable<MyCustomDTO> GetMyCustomDTODummy() { return null; }

Now you can successfully compile your project and use your custom operation with custom DTO client side

Code Snippet
var ctx = (OrderContext)orderDomainDataSource.DomainContext;
var data = new MyCustomDTO
{
    MyPayload = 13,
    MyOtherPayload = "Test"
};
ctx.MyOperation(data);

but what about more complex DTOs with sub lists (data graphs)? well, just wait for the next post ;)

3 comments:

  1. How it is possible to create function
    like that

    [Invoke]
    public void DoSometing(MyCustomDTO[] dtos)
    {
    }

    ReplyDelete
  2. I have the same problem!!!

    [Invoke]
    public void MySave(DTOX dtox, List dtosy)
    {

    }

    ReplyDelete
  3. Hello ...

    Some how I manage to make this work on the RIA:

    [Invoke] public IEnumerable GetContacts(string name = "")
    {
    }

    I make some presentation to my boss and everything work nice. He ask me for some changes and now it's not working and I'm getting the error:
    "Operation 'GetContracts' named does not conform to the required signature. Return types must be an entity, collection of entities, or one of the predefined serializable types"

    there is something that I've done that I want to do again!

    My RIA Service it's implemented by hand using the DomainService inherit because this GetContracts access a WCF service.

    The major problem with this it this, on my client I only get the GetContractsQuery method that I don't know how to use to get my data.
    can you help me?


    regards
    Paulo Aboim Pinto
    Odivelas - Portugal

    ReplyDelete