I am using dynamic
objects for my view models, as I find the overhead from using something like Automapper unnecessary and find this approach a lot more flexible and lightweight. I am using the builder from impromptu-interface like this:
private dynamic New = Builder.New();
private dynamic GetViewModel(Product p)
{
var viewModel = New.Product( id : p.Id, name : p.Name );
viewModel.AdditionalProperty = "some additional data";
return viewModel;
}
There are a few scenarios where "expanding" the actual object would be better then remapping all the properties one by one, similar to how you would do in JavaScript using jQuery.extend()
private dynamic GetViewModel(Product p)
{
var viewModel = //create base dynamic object, that has all the members of p.
viewModel.AdditionalProperty = "some additional data";
return viewModel;
}
This should be achievable using ExpandoObject
combined with reflection and iterating through all the members, but I would like to know if there's a cleaner/neater solution.
I ended up implementing it like this:
and the tests:
This makes use of
Impromptu.InvokeGet()
instead ofPropertyInfo.GetValue()
becauseImpromptu.InvokeGet()
uses the DLR and as such about 2.5x faster than using than reflection from my tests. Overall this works reasonably fast and the overhead for upto 10,000 objects is almost nonexistant.I should note that this won't work to expand other
ExpandoObject
or similar, but this should not really be necessary anyway.