1- using Microsoft . EntityFrameworkCore ;
1+ using Extensions ;
2+ using Microsoft . EntityFrameworkCore ;
23
34namespace Infrastructure . RepositoryPattern
45{
56 public class RepositoryBase < T > : IRepository < T > where T : class
67 {
78 private readonly DbContext _context ;
8- private DbSet < T > _dbSet { get ; }
9+ private DbSet < T > DbSet { get ; }
910
1011 public RepositoryBase ( DbContext context )
1112 {
1213 _context = context ;
13- _dbSet = _context . Set < T > ( ) ;
14+ DbSet = _context . Set < T > ( ) ;
1415 }
1516
1617 public IQueryable < T > Query ( bool trackChanges = true ) =>
17- ! trackChanges ?
18- _dbSet . AsNoTracking ( ) :
19- _dbSet ;
18+ DbSet . AsNoTrackingIf ( ! trackChanges ) ;
2019
2120 public virtual T GetById < TId > ( TId id , bool track = true )
2221 {
23- var entity = _dbSet . Find ( id ) ;
22+ var entity = DbSet . Find ( id ) ;
2423 if ( ! track )
2524 _context . Entry ( entity ) . State = EntityState . Detached ;
2625 return entity ;
2726 }
2827
2928 public virtual async Task < T > GetByIdAsync < TId > ( TId id , bool track = true )
3029 {
31- var entity = await _dbSet . FindAsync ( id ) ;
30+ var entity = await DbSet . FindAsync ( id ) ;
3231 if ( ! track )
3332 _context . Entry ( entity ) . State = EntityState . Detached ;
3433 return entity ;
3534 }
3635
37- public void Create ( T entity ) => _dbSet . Add ( entity ) ;
36+ public void Create ( T entity ) => DbSet . Add ( entity ) ;
3837
39- public void Update ( T entity ) => _dbSet . Update ( entity ) ;
40- public void UpdateRange ( params T [ ] entities ) => _dbSet . UpdateRange ( entities ) ;
41- public void UpdateRange ( IEnumerable < T > entities ) => _dbSet . UpdateRange ( entities ) ;
38+ public void Update ( T entity ) => DbSet . Update ( entity ) ;
39+ public void UpdateRange ( params T [ ] entities ) => DbSet . UpdateRange ( entities ) ;
40+ public void UpdateRange ( IEnumerable < T > entities ) => DbSet . UpdateRange ( entities ) ;
4241
43- public void Remove ( T entity ) => _dbSet . Remove ( entity ) ;
44- public void RemoveRange ( params T [ ] entities ) => _dbSet . RemoveRange ( entities ) ;
45- public void RemoveRange ( IEnumerable < T > entities ) => _dbSet . RemoveRange ( entities ) ;
42+ public void Remove ( T entity ) => DbSet . Remove ( entity ) ;
43+ public void RemoveRange ( params T [ ] entities ) => DbSet . RemoveRange ( entities ) ;
44+ public void RemoveRange ( IEnumerable < T > entities ) => DbSet . RemoveRange ( entities ) ;
4645
4746 public virtual async void RemoveByIdAsync < TId > ( TId id )
4847 {
@@ -54,7 +53,7 @@ public virtual async void RemoveByIdAsync<TId>(TId id)
5453 if ( entityToDelete == null )
5554 throw new Exception ( "Entity not found" ) ;
5655
57- _dbSet . Remove ( entityToDelete ) ;
56+ DbSet . Remove ( entityToDelete ) ;
5857 }
5958
6059 public virtual void RemoveById < TId > ( TId id )
@@ -67,7 +66,7 @@ public virtual void RemoveById<TId>(TId id)
6766 if ( entityToDelete == null )
6867 throw new Exception ( "Entity not found" ) ;
6968
70- _dbSet . Remove ( entityToDelete ) ;
69+ DbSet . Remove ( entityToDelete ) ;
7170 }
7271 }
7372}
0 commit comments