Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ildoc committed Nov 3, 2022
1 parent 40a9f7d commit 5a1a9a2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
1 change: 0 additions & 1 deletion src/Infrastructure/Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

<ItemGroup>
<ProjectReference Include="..\Extensions\Extensions.csproj" />
<ProjectReference Include="..\Utils\Utils.csproj" />
</ItemGroup>

</Project>
33 changes: 16 additions & 17 deletions src/Infrastructure/RepositoryPattern/RepositoryBase.cs
Original file line number Diff line number Diff line change
@@ -1,48 +1,47 @@
using Microsoft.EntityFrameworkCore;
using Extensions;
using Microsoft.EntityFrameworkCore;

namespace Infrastructure.RepositoryPattern
{
public class RepositoryBase<T> : IRepository<T> where T : class
{
private readonly DbContext _context;
private DbSet<T> _dbSet { get; }
private DbSet<T> DbSet { get; }

public RepositoryBase(DbContext context)
{
_context = context;
_dbSet = _context.Set<T>();
DbSet = _context.Set<T>();
}

public IQueryable<T> Query(bool trackChanges = true) =>
!trackChanges ?
_dbSet.AsNoTracking() :
_dbSet;
DbSet.AsNoTrackingIf(!trackChanges);

public virtual T GetById<TId>(TId id, bool track = true)
{
var entity = _dbSet.Find(id);
var entity = DbSet.Find(id);
if (!track)
_context.Entry(entity).State = EntityState.Detached;
return entity;
}

public virtual async Task<T> GetByIdAsync<TId>(TId id, bool track = true)
{
var entity = await _dbSet.FindAsync(id);
var entity = await DbSet.FindAsync(id);
if (!track)
_context.Entry(entity).State = EntityState.Detached;
return entity;
}

public void Create(T entity) => _dbSet.Add(entity);
public void Create(T entity) => DbSet.Add(entity);

public void Update(T entity) => _dbSet.Update(entity);
public void UpdateRange(params T[] entities) => _dbSet.UpdateRange(entities);
public void UpdateRange(IEnumerable<T> entities) => _dbSet.UpdateRange(entities);
public void Update(T entity) => DbSet.Update(entity);
public void UpdateRange(params T[] entities) => DbSet.UpdateRange(entities);
public void UpdateRange(IEnumerable<T> entities) => DbSet.UpdateRange(entities);

public void Remove(T entity) => _dbSet.Remove(entity);
public void RemoveRange(params T[] entities) => _dbSet.RemoveRange(entities);
public void RemoveRange(IEnumerable<T> entities) => _dbSet.RemoveRange(entities);
public void Remove(T entity) => DbSet.Remove(entity);
public void RemoveRange(params T[] entities) => DbSet.RemoveRange(entities);
public void RemoveRange(IEnumerable<T> entities) => DbSet.RemoveRange(entities);

public virtual async void RemoveByIdAsync<TId>(TId id)
{
Expand All @@ -54,7 +53,7 @@ public virtual async void RemoveByIdAsync<TId>(TId id)
if (entityToDelete == null)
throw new Exception("Entity not found");

_dbSet.Remove(entityToDelete);
DbSet.Remove(entityToDelete);
}

public virtual void RemoveById<TId>(TId id)
Expand All @@ -67,7 +66,7 @@ public virtual void RemoveById<TId>(TId id)
if (entityToDelete == null)
throw new Exception("Entity not found");

_dbSet.Remove(entityToDelete);
DbSet.Remove(entityToDelete);
}
}
}

0 comments on commit 5a1a9a2

Please sign in to comment.