We have a few repository classes in our application with the methods like the following:
public override void Add(Template template)
{
CheckDuplicateDescription(template);
base.Add(template);
}
public override void Update(Template template)
{
CheckDuplicateDescription(template);
base.Update(template);
}
private void CheckDuplicateDescription(Template template)
{
if( _dbSet.Any(x => x.Descrip.Equals(template.Descrip.Trim()) && x.TemplateId != template.TemplateId))
{
throw new DuplicatePropertyException("Description",
string.Format(Messages.alreadyExistsWithValue, template.Descrip.Trim()));
}
}
I'm trying to figure out if there is a way to make this method to be generic and implement in the base repository class (so we can probably provide a bool property to tell we need to validate, perhaps also the name of the column to check and the name of Pk column). I am not sure how to write such a code in a generic way assuming Entity entity in the update method.