Dynamic DTO and Expando Objects

613 views
Skip to first unread message

ririck

unread,
Jun 8, 2011, 1:04:06 PM6/8/11
to servic...@googlegroups.com
I'd like to return a dynamic DTO based on the url route to fill a grid similar the one generated natively by servicestack.

So if I call something like mydomain/col1-col2-col4-col6 instead of returning the full collection with all the columns and hiding the non interesting data on the client I'd like to build a dynamic DTO and return a collection of dynamic objects.

Everything works if you return a dynamic object with already known properties build as an anonymous object.

  dynamic obj = new  {id = i,col1="testcol1",col2="testcol2",col4="testcol3"};

If I want to choose witch property to add the only way I found was this :
 dynamic obj=new ExpandoObject();
                obj.id = i;
                foreach (var col in columns)
                {
                    switch (col)
                    {
                        case "col1":
                            obj.col1 = "testcol1";
                            break;
                        case "col2":
                            obj.col2 = "col 2";
                            break;
                        default:
                            break;

                    }
The result that appears when serialized si very different (check the pictures).

Is there a clean way to dynamically add proprerties to a DTO to return?
Capturedyn01.PNG
Capturedyn02.PNG

Demis Bellot

unread,
Jun 8, 2011, 1:12:52 PM6/8/11
to servic...@googlegroups.com
Hi Riccardo,

Why don't you just use a Dictionary<string,string>?

Honestly I'm not to fond of dynamic DTO's if it's a property you want to add, I don't see why it can't be defined on a formal DTO type.
I've vented my concerns about using dynamic DTOs in a previous discussion captured here:

I should be able to support serializing anonymous types stored/returned on an object, but it's not deserializable since I don't have the type information to serialize it back into.

Cheers,

ririck

unread,
Jun 8, 2011, 1:24:15 PM6/8/11
to servic...@googlegroups.com
Denis, my base object for my returned collection  is like a row of an excel file with different types for every column and I have like more than 300 clolumns.

What would the most efficient way to let the user choose the columns to return without all the columns and hiding them on the client?

Demis Bellot

unread,
Jun 8, 2011, 1:38:15 PM6/8/11
to servic...@googlegroups.com
You could just use a List<Dictionary<string,string>>() ?

Note: a Dictionary would serialize the same way as if you did have a POCO with 300 properties (i.e. key/value pairs with the nulls excluded).

Otherwise you could do something more compact where you don't repeat the column name like:

List<string> Headers
List<List<string>> RowValues

Row doesn't even need to be a List<string> you could make it a POCO with 20 columns or so on it where the headers holds the names of each column

List<Row> RowValues:

class Row 
  string Row1
  string Row2
  ...

Basically just the standard ways of dealing with dynamic data, personally I like using .NET collections myself.

Cheers,


On Wed, Jun 8, 2011 at 6:24 PM, ririck <riccardo...@gmail.com> wrote:
Denis, my base object for my returned collection  is like a row of an excel file with different types for every column and I have like more than 300 clolumns.

What would the most efficient way to let the user choose the columns to return without all the columns and hiding them on the client?



Reply all
Reply to author
Forward
0 new messages