I have scenario in project where we need to fetch all active book records from database and hold in observablecollection. This observale Collection is bound to grid control in WPF application where user can add remove books. We need to persist all added, removed and modified records in collection till user hit the save to database. Our contraint is book name and IBN number of book must be unique. If user add new book with duplicate book name that already present in colle开发者_JAVA百科ction then we need to invalidate that object, so that it will not allow user to save in database till all collection object are not valid. I know it can be achieved with different ways, I want to know about best practices to do this. Any solution with IDataErrorInfo or Enterprise Validation Library is appreciated.
You can very well use IDataErrorInfo like this
public class Book : IDataErrorInfo
{
public string BookName { get; set; }
public string IBN { get; set; }
public string Error
{
get { throw new NotImplementedException(); }
}
public string this[string columnName]
{
get
{
string result = null;
switch(columnName)
{
case "BookName":
result = IsDuplicate(BookName) ? "Already Present" : null;
break;
case "IBN":
result = IsDuplicate(IBN) ? "Already Present" : null;
break;
default:
break;
}
return result;
}
}
}
精彩评论