RIA and DTO

董法
2023-12-01

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

 

 类似资料:

相关阅读

相关文章

相关问答