RIA and DTO

来源:互联网 发布:敏感性分析软件 编辑:程序博客网 时间:2024/05/19 17:59

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 ;)

 

原创粉丝点击