Skip to content

Commit

Permalink
slight refactor to InsertNotExists to return entity
Browse files Browse the repository at this point in the history
  • Loading branch information
garywoodfine committed Dec 20, 2021
1 parent 75052c4 commit cfced34
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/IRepository.cs
Expand Up @@ -35,7 +35,8 @@ public interface IRepository<T> : IReadRepository<T>, IDisposable where T : clas
void Insert(params T[] entities);
void Insert(IEnumerable<T> entities);

void InsertNotExists(Expression<Func<T, bool>> predicate, T entity);
T InsertNotExists(Expression<Func<T, bool>> predicate, T entity);


void Update(T entity);
void Update(params T[] entities);
Expand Down
9 changes: 6 additions & 3 deletions src/Repository.cs
Expand Up @@ -90,11 +90,14 @@ public void Insert(IEnumerable<T> entities)
_dbSet.AddRange(entities);
}

public void InsertNotExists(Expression<Func<T, bool>> predicate, T entity)
public T InsertNotExists(Expression<Func<T, bool>> predicate, T entity)
{
var exists = predicate != null ? _dbSet.Any(predicate) : _dbSet.Any();
if (!exists) _dbSet.Add(entity);
if (_dbSet.Any(predicate)) return _dbSet.SingleOrDefault(predicate);
_dbSet.Add(entity);
return entity;

}


#endregion

Expand Down

0 comments on commit cfced34

Please sign in to comment.