值对象和视图模型属性
我正在开发一个使用 DDD 进行架构的解决方案。我的 ViewModel 中有一个指向 ValueObject 的属性,视图模型还实现了 INotifyPropertyChanged 接口。当用户在前端输入数据时,ValueObject 的值将发生变化。我遇到的问题是值对象应该是不可变的。我该如何解决这个问题?先感谢您。
I am working on a solution that used DDD for architecture. I have a property in my ViewModel which points to a ValueObject, the view model also implements INotifyPropertyChanged interface. The value of the ValueObject will change as a user enters data on the front end. The problem I am running into is the value object is suppose to be immutable. How can I work around this issue? Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您可以编辑某些内容,那么必须有一个用于不可变值的可变容器。因此,您的视图模型应该作用于可变容器,而不是直接作用于不可变值。
整数就是此类不可变值对象的一个示例:
Int32
类型没有任何允许您更改对象状态的成员。您只能替换一个整数,而不能更改它。因此,整数的视图模型将如下所示:其中
MutableInteger
就是这样:我在这里省略了错误处理和更改通知,但希望您能明白这一点。
另请注意,此示例与具有
FirstName
和LastName
的Customer
类的典型示例并没有太大区别。字符串也是不可变的,因此我们再次为不可变值提供了一个可变容器。If you can edit something, then there must be a mutable container for the immutable value. Therefore, your viewmodel should act on the mutable container rather than on the immutable value directly.
An integer is an example of such an immutable value object: the
Int32
type does not have any members that allow you to change the state of the object. You can only replace an integer, not change it. So a view model for an integer would look like this:Where
MutableInteger
is just this:I've omitted error handling and change notification here, but hopefully you get the idea.
Also note this example is not really different from the typical example of a
Customer
class with aFirstName
and aLastName
. Strings are also immutable, so again we have a mutable container for immutable values.