Monday, January 4, 2010

WCF RIA Services – Using inheritance

WCF RIA Services support projecting object hierarchies from server code to client code. That I am aware of this is the only process to change the base class of an entity in the client side.

First create the entities in the server annotated correctly:

   1: [KnownType(typeof(SuperClass2))]
   2: [KnownType(typeof(SuperClass1))]
   3: public class CommonClass
   4: {
   5:     [Key]
   6:     public string SomeString
   7:     {
   8:         get;
   9:         set;
  10:     }
  11: }
  12:  
  13: public class SuperClass1 : CommonClass
  14: {
  15:     public int SomeInt
  16:     {
  17:         get;
  18:         set;
  19:     }
  20: }
  21:  
  22: public class SuperClass2 : CommonClass
  23: {
  24:     public decimal SomeDecimal
  25:     {
  26:         get;
  27:         set;
  28:     }
  29: }

Second create a Domain Service with at least a query method that use the base class:

   1: [EnableClientAccess()]
   2: public class InheritanceDomainService : DomainService
   3: {
   4:     public IQueryable<CommonClass> GetCommons()
   5:     {
   6:         return new List<CommonClass>().AsQueryable();
   7:     }
   8: }

If you look at the generated code the SuperClass1 and SuperClass2 will be available at the client as well even though they are not used in any query method or domain operation.

Have fun,

No comments: