DDD 友好的 ASP.NET MVC 模型绑定器?
我正在考虑自定义模型绑定程序的值,它可以实例化在我的域层中定义的不可变值对象。然后我可以将它们传递到堆栈并将它们设置在适当的实体上。有人尝试过吗?有运气吗?认为这是一个愚蠢的想法吗?
I'm considering the value of a custom model binder that can instatiate immutable value objects defined in my domain layer. Then I can just pass them through the stack and set them on the appropriate entity. Has anyone tried? Had any luck? Think its a silly idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果“值对象”是指只能通过将值传递给构造函数而不是通过绑定到字段来创建的对象,我认为您有以下解决方案:
编写自定义绑定器 - 尽管我现在不知道如何一次访问其中的多个字段。
传递视图模型(允许绑定到其字段),然后将其转换为值对象。使用反射编写一个简单的转换器(几行)。您必须按名称或类型将视图模型属性和构造函数参数关联起来。您可以让视图模型定义相应的值类型,并在操作过滤器/OnActionExecuting 中自动调用转换器。这是一种半自动模型绑定。
将 FormCollection 之类的内容传递给操作,然后调用您的反射方法,例如 var value = BindValue(formCollection)。
If by "value objects" you mean objects that can only be created by passing values to constructor, not by binding to fields, I think you have these solutions:
Write a custom binder - though I can't tell now how to access several fields at once in there.
Pass view model (that allows to bind to its fields) and then convert it to value object. Write a simple converter using reflection (couple of lines). You'll have to relate view model properties and constructor parameters either by name or by type. You can have your view model define what is the corresponding value type, and in action filter/OnActionExecuting call you converter - automatically. That's kind of semi-automatic model binding.
Pass something like FormCollection to action and then call your reflection method like var value = BindValue<ValueType>(formCollection).