2
\$\begingroup\$

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
t3chb0t
44.6k9 gold badges84 silver badges190 bronze badges
asked Jul 12, 2017 at 9:56
\$\endgroup\$
0

1 Answer 1

3
\$\begingroup\$

You make this harder then it needs to be.

Since IDbSet implements the IEnumerable<TEntity> interface

 public interface IDbSet<TEntity> 
 : IQueryable<TEntity>, IEnumerable<TEntity>, IQueryable, IEnumerable

you can simply use the Contains extension optionally with your own comparer that can implement such things as Trim etc.

answered Jul 12, 2017 at 10:16
\$\endgroup\$
5
  • \$\begingroup\$ I used Trim() beacse I don't want to save Tag name with spaces around it , Like this C# or ` MVC ` \$\endgroup\$ Commented Jul 12, 2017 at 10:20
  • 1
    \$\begingroup\$ @workquestion sure, I'm not saying that it's wrong just that you could put it in the comparer and call it again just before adding a new tag. \$\endgroup\$ Commented Jul 12, 2017 at 10:22
  • 2
    \$\begingroup\$ @t3chb0t - I don't think a comparer is the right choice for encoding important domain rules. Here, the OP is saying Tagname must never have surrounding whitespace. I think Trimming incoming values is a better choice (but that's up for debate!) \$\endgroup\$ Commented Jul 12, 2017 at 10:47
  • \$\begingroup\$ @RobH true, I always say there should be multiple sting types for this, case-insensitive and trimmed ones becasue this two are causing too many bugs. \$\endgroup\$ Commented Jul 12, 2017 at 10:59
  • \$\begingroup\$ @t3chb0t - I know what you mean... Especially with SQL's handling of equality see here. It's easy to have stuff in the DB returning true and stuff in C# returning false if you don't trim the strings somewhere. \$\endgroup\$ Commented Jul 12, 2017 at 12:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.