How to check a property of a custom object in a data binding script (i.e. a BindingList)?

When you set a property value, you can either check before or after updating the internal value.

If you check earlier, you can throw an exception if the new value is invalid. Then the object is always in a valid state.

If the check is performed after, you need to cancel (i.e. via IEditableObject ), so the user can cancel editing at any time. We also have the opportunity to either throw an exception here or throw errors through IDataErrorInfo .

I do not think that IDataErrorInfo makes sense if checked before typing . Some may also argue that throwing exceptions is not justified in validation scripts.

Validation after extensive work in scenarios where the user object is contained in the BindingList and specified as the data source for the grid.

Check before performing operations with grids, too, but you need to throw an exception to signal that the property value did not fall into the data grid (without a lot of additional code).

I also find it inconvenient to use domain objects that implement IEditableObject and IDataErrorInfo , INotifyPropertyChanged , etc. It leaves the domain object cluttered with additional problems. But this seems inevitable if you want to put something nice with data binding. I could create a wrapper, possibly a DTO, but I'm not too crazy due to the fact that I have to write mostly dummy extra code just to support these bits of data.

How do you validate objects (preferably in the context of data binding and user interface)?

+3
source share
1 answer

-, : , ( ) .

IDataErrorInfo (, , IEditableObject INotifyPropertyChanged), .NET- (Windows Forms, WPF, ASP.NET...) . ( ) - -: ( ), ( , ).

, ( , , ). , , . ( ), , , .

factory ( ): Salamanca.

+1

Source: https://habr.com/ru/post/1698000/


All Articles