将静态类型对象扩展为动态对象
我在视图模型中使用动态对象,因为我发现使用 Automapper 之类的东西不必要的开销,并且发现这种方法更加灵活和轻量。我正在使用 impromptu-interface 中的构建器,如下所示:
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;
}
有几种情况其中“扩展”实际对象会比逐一重新映射所有属性更好,类似于在 JavaScript 中使用 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;
}
这应该可以使用来实现ExpandoObject
结合反射并迭代所有成员,但我想知道是否有更干净/整洁的解决方案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终像这样实现它:
和测试:
这使用
Impromptu.InvokeGet()
而不是PropertyInfo.GetValue()
因为Impromptu.InvokeGet( )
使用 DLR,因此根据我的测试,比使用反射快大约 2.5 倍。总体而言,该方法运行得相当快,并且最多 10,000 个对象的开销几乎不存在。我应该注意到,这对于扩展其他 ExpandoObject 或类似对象不起作用,但这实际上并不是必要的。
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.您可以创建组合两个或多个对象的动态对象:
并像这样使用它:
当您动态获取或设置属性时,它首先尝试在第一个对象上执行此操作,然后在第二个对象上执行此操作,依此类推,直到成功为止。
这样做(至少)有一个奇怪的行为:例如,如果
Product
具有int
类型的属性Id
,则代码 < code>viewModel.Id = "42"; 会成功。但它会设置ExpandoObject
上的属性。因此,如果您在此之后尝试检索viewModel.Id
,它将从product.Id
返回未修改的int
。You could create dynamic object that combines two or more objects:
And use it like this:
When you get or set a property dynamically, it first tries to do that on the first object, then on the second etc., until it succeeds.
Doing it like this has (at least) one weird behavior: If, for example,
Product
had propertyId
of typeint
, the codeviewModel.Id = "42";
would succeed. But it would set the property on theExpandoObject
. So if you tried to retrieveviewModel.Id
after that, it would return theint
fromproduct.Id
, which wasn't modified.