I have a service to Insert TagName
, I dont want to insert duplicate tags
I used this :
public void Insert(string tagName)
{
tagName = tagName.Trim();
if (IsExists(tagName))
throw new ArgumentException("tagName is Exists");
Tag model = new Tag { Name=tagName};
_uow.MarkAsAdded(model);
}
public bool IsExists(string tagName)
{
return _tags.Any(row=>row.Name==tagName.Trim());
}
and another way is change If
statement like below :
public void Insert(string tagName)
{
tagName = tagName.Trim();
if (!IsExists(tagName))
{
Tag model = new Tag { Name = tagName };
_uow.MarkAsAdded(model);
}
}
I decided to use second one because I add another method Like this :
public void Insert(string[] tags)
{
foreach (var item in tags)
{
Insert(item);
}
}
If I throw exception , Insert(string[] tags)
will not completed .
Updated:
private readonly IDbSet<Tag> _tags;
#region ctor
public TagService(IUnitOfWork uow)
{
_uow = uow;
_tags = _uow.Set<Tag>();
}
#endregion