Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db context made protected #6

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public class KirelGenericEntityFrameworkRepository<TKey, TEntity, TDbContext> :
where TKey : IComparable, IComparable<TKey>, IEquatable<TKey>
where TDbContext : DbContext
{
private readonly TDbContext _dbContext;
/// <summary>
/// Db context
/// </summary>
protected readonly TDbContext DbContext;
/// <summary>
/// Reader field for read access
/// </summary>
Expand All @@ -30,15 +33,15 @@ protected virtual IQueryable<TEntity> Reader
/// </summary>
protected virtual DbSet<TEntity> Writer
{
get => _dbContext.Set<TEntity>();
get => DbContext.Set<TEntity>();
}
/// <summary>
/// GenericRepository constructor
/// </summary>
/// <param name="context">DbContext</param>
public KirelGenericEntityFrameworkRepository(TDbContext context)
{
_dbContext = context;
DbContext = context;
}
/// <summary>
/// Add new Entity
Expand All @@ -48,8 +51,8 @@ public KirelGenericEntityFrameworkRepository(TDbContext context)
public async Task<TEntity> Insert(TEntity entity)
{
Writer.Add(entity);
_dbContext.Entry(entity).State = EntityState.Added;
await _dbContext.SaveChangesAsync();
DbContext.Entry(entity).State = EntityState.Added;
await DbContext.SaveChangesAsync();
return entity;
}
/// <summary>
Expand All @@ -74,7 +77,7 @@ public async Task Delete(TKey id)
if (entity == null)
throw new KeyNotFoundException($"Entity with specified id {id} was not found");
Writer.Remove(entity);
await _dbContext.SaveChangesAsync();
await DbContext.SaveChangesAsync();
}
/// <summary>
/// Updating entity
Expand All @@ -84,8 +87,8 @@ public async Task Delete(TKey id)
public async Task<TEntity> Update(TEntity entity)
{
Writer.Attach(entity);
_dbContext.Entry(entity).State = EntityState.Modified;
await _dbContext.SaveChangesAsync();
DbContext.Entry(entity).State = EntityState.Modified;
await DbContext.SaveChangesAsync();
return entity;
}
/// <summary>
Expand Down
Loading