Inheritance in DTO

1,216 views
Skip to first unread message

Pierluca P.

unread,
Jan 22, 2013, 2:49:08 PM1/22/13
to servic...@googlegroups.com
There are limitations in the inheritance in DTO?

Demis Bellot

unread,
Jan 22, 2013, 3:12:58 PM1/22/13
to servic...@googlegroups.com
Even though Inheritance in DTOs are a bad idea (that IMO should be avoided when possible), they're still generally supported, although most serializers require implementation-specific extensions in order to support it.



On Tue, Jan 22, 2013 at 2:49 PM, Pierluca P. <ppetr...@gmail.com> wrote:
There are limitations in the inheritance in DTO?



Pierluca P.

unread,
Jan 22, 2013, 3:53:38 PM1/22/13
to servic...@googlegroups.com
Hi, consider this example:

public class MyControl
{
    public int Id { get; set; }
}

public class MyWebControl : MyControl
{
    public string Name { get; set; }
}

public class Panel : MyWebControl
{
    public IList<MyWebControl> Controls {get;set;}
    public Panel()
    {
      Controls = new List<MyWebControl>();
    }
}

//Dto response in ServiceStack
var ctr1 = new MyWebControl { Id = 1, Name = "control1"};
var ctr2 = new MyWebControl { Id = 2, Name = "control2"};

var panel1 = new Panel { Id= 3, Name = "container1"};
panel1.Controls.Add( ctr1);
panel1.Controls.Add( ctr2);

var panel2 = new Panel { Id= 4, Name = "container2"};
panel2.Controls.Add( panel1);

return panel2;

ServiceStack's JsonSerializer do not serialize the controls ctr1 e ctr2.
so the only solution would be to work with interface or abstract class?

Thanks

Demis Bellot

unread,
Jan 22, 2013, 4:05:35 PM1/22/13
to servic...@googlegroups.com
Right, you can use an interface or abstract class or alternatively you can specify that which types should always emit __type info (i.e. the metadata required to enable inheritance) by specifying it in the config:

    JsConfig<MyControl>.IncludeTypeInfo = true;

Note: this isn't a support forum, please ask future support questions on StackOverflow.

Thanks,
Reply all
Reply to author
Forward
0 new messages