When you a set a property's value, you can either validate before or after updating the internal value.
设置属性值时,可以在更新内部值之前或之后进行验证。
If validating before, you could throw an exception if the new value is invalid. The object is then always in a valid state.
如果之前验证,则可以在新值无效时抛出异常。然后该对象始终处于有效状态。
If validating after, undo is desired (i.e. via IEditableObject), so the user can cancel the edit any time. We also have the option of either throwing an exception here or expose errors via IDataErrorInfo.
如果在之后验证,则需要撤消(即通过IEditableObject),因此用户可以随时取消编辑。我们还可以选择在此处抛出异常或通过IDataErrorInfo暴露错误。
I don't think IDataErrorInfo makes sense if validating before the set. Some also may argue throwing an Exception is not warranted in validation scenarios.
如果在集合之前验证,我认为IDataErrorInfo没有意义。有些人也可能认为抛出异常在验证方案中是不合理的。
Validating after works great in scenarios where the custom object is contained in BindingList and set as datasource to a grid.
在自定义对象包含在BindingList中并将其设置为网格的数据源的情况下进行优化后进行验证。
Validating before works ok with grids too but you kind of have to throw an exception in order to signal the setting of the property value failed to the data grid (without a lot of extra code)
在使用网格工作之前验证也是好的,但你必须抛出一个异常,以便向数据网格发信号通知属性值的设置(没有很多额外的代码)
I am also not comfortable with my domain objects implementing IEditableObject and IDataErrorInfo, INotifyPropertyChanged, etc. It leaves the domain object cluttered with extra concerns. But it seems unavoidable if you want to place nice with databinding. I could create a wrapper, DTO perhaps, but I'm not too crazy about having to write mostly dummy extra code just to support these databinding bits.
我也不熟悉我的域对象实现IEditableObject和IDataErrorInfo,INotifyPropertyChanged等。它使域对象混乱了额外的问题。但是如果你想用数据绑定来放置它似乎是不可避免的。我可以创建一个包装器,也许是DTO,但我不是太疯狂为了支持这些数据绑定位而必须编写大多数虚拟额外代码。
How do you validate objects (preferrably in the context of databinding and UI)?
如何验证对象(最好是在数据绑定和UI的上下文中)?
1 个解决方案
#1
1
See my answer to Business Objects, Validation And Exceptions : I think Paul Stovell's ideas about validation (summed up in this article) are incredibly powerful.
请参阅我对Business Objects,Validation和Exceptions的回答:我认为Paul Stovell关于验证的想法(在本文中总结)非常强大。
By implementing IDataErrorInfo
in your domain entities (and possibly IEditableObject
and INotifyPropertyChanged
), you give them the ability to get databound on many .NET presentation technologies (Windows Forms, WPF, ASP .NET...) without much code. Or you could use them in scripts or batches (ie non UI processes) and still have the possibility to validate them against business rules : either smoothly (query the current entity state) or the hard way (throw an exception on saving if invalid).
通过在域实体中实现IDataErrorInfo(可能还有IEditableObject和INotifyPropertyChanged),您可以在没有太多代码的情况下在许多.NET表示技术(Windows窗体,WPF,ASP .NET ......)上获取数据绑定。或者您可以在脚本或批处理中使用它们(即非UI进程),并且仍然可以根据业务规则验证它们:顺利地(查询当前实体状态)或硬路径(如果无效则抛出异常保存)。
The main thing is that with this pattern, your domain entities are responsible for their own validation (which is good, and certainly does not appear to me like extra concerns). They enforce it by throwing meaningful exceptions on saving if they are in an invalid state. And they can play nicely with your code (UI or not), by letting you know if they are valid or not if you care to ask first.
主要的是,使用这种模式,您的域实体负责自己的验证(这是好的,当然在我看来并不像额外的问题)。如果它们处于无效状态,它们会通过在保存时抛出有意义的异常来强制执行它。他们可以很好地使用您的代码(UI或不是UI),如果您想先询问,可以告诉您它们是否有效。
I even apply these principles beyond the domain model in my software factory project (still in early stages) : Salamanca.
我甚至在我的软件工厂项目(仍处于早期阶段)中将这些原则应用于领域模型之外:萨拉曼卡。
#1
1
See my answer to Business Objects, Validation And Exceptions : I think Paul Stovell's ideas about validation (summed up in this article) are incredibly powerful.
请参阅我对Business Objects,Validation和Exceptions的回答:我认为Paul Stovell关于验证的想法(在本文中总结)非常强大。
By implementing IDataErrorInfo
in your domain entities (and possibly IEditableObject
and INotifyPropertyChanged
), you give them the ability to get databound on many .NET presentation technologies (Windows Forms, WPF, ASP .NET...) without much code. Or you could use them in scripts or batches (ie non UI processes) and still have the possibility to validate them against business rules : either smoothly (query the current entity state) or the hard way (throw an exception on saving if invalid).
通过在域实体中实现IDataErrorInfo(可能还有IEditableObject和INotifyPropertyChanged),您可以在没有太多代码的情况下在许多.NET表示技术(Windows窗体,WPF,ASP .NET ......)上获取数据绑定。或者您可以在脚本或批处理中使用它们(即非UI进程),并且仍然可以根据业务规则验证它们:顺利地(查询当前实体状态)或硬路径(如果无效则抛出异常保存)。
The main thing is that with this pattern, your domain entities are responsible for their own validation (which is good, and certainly does not appear to me like extra concerns). They enforce it by throwing meaningful exceptions on saving if they are in an invalid state. And they can play nicely with your code (UI or not), by letting you know if they are valid or not if you care to ask first.
主要的是,使用这种模式,您的域实体负责自己的验证(这是好的,当然在我看来并不像额外的问题)。如果它们处于无效状态,它们会通过在保存时抛出有意义的异常来强制执行它。他们可以很好地使用您的代码(UI或不是UI),如果您想先询问,可以告诉您它们是否有效。
I even apply these principles beyond the domain model in my software factory project (still in early stages) : Salamanca.
我甚至在我的软件工厂项目(仍处于早期阶段)中将这些原则应用于领域模型之外:萨拉曼卡。