@@ -56,7 +56,7 @@ public async Task<IList<User>> GetAllAsync(CancellationToken cancellationToken)
5656## Dependency Injection
5757``` CSharp
5858builder .Service .AddScoped <IUserRepository , UserRepository >();
59- builder .Service .AddScoped <IUnitOfWork >(cfr => cfr .GetRequiredService <ApplicationDbContext >());
59+ builder .Services .AddScoped <IUnitOfWork >(srv => srv .GetRequiredService <ApplicationDbContext >());
6060```
6161
6262## Methods
@@ -91,158 +91,4 @@ public interface IRepository<TEntity>
9191 void DeleteRange (ICollection <TEntity > entities );
9292
9393}
94- ```
95-
96- ``` Csharp
97- public class Repository <TEntity , TContext > : IRepository <TEntity >
98- where TEntity : class
99- where TContext : DbContext
100- {
101- private readonly TContext _context ;
102- private DbSet <TEntity > Entity ;
103-
104- public Repository (TContext context )
105- {
106- _context = context ;
107- Entity = _context .Set <TEntity >();
108- }
109-
110- public void Add (TEntity entity )
111- {
112- Entity .Add (entity );
113- }
114-
115- public async Task AddAsync (TEntity entity , CancellationToken cancellationToken = default )
116- {
117- await Entity .AddAsync (entity , cancellationToken );
118- }
119-
120- public async Task AddRangeAsync (ICollection <TEntity > entities , CancellationToken cancellationToken = default )
121- {
122- await Entity .AddRangeAsync (entities , cancellationToken );
123- }
124-
125- public bool Any (Expression <Func <TEntity , bool >> expression )
126- {
127- return Entity .Any (expression );
128- }
129-
130- public async Task <bool > AnyAsync (Expression <Func <TEntity , bool >> expression , CancellationToken cancellationToken = default )
131- {
132- return await Entity .AnyAsync (expression , cancellationToken );
133- }
134-
135- public void Delete (TEntity entity )
136- {
137- Entity .Remove (entity );
138- }
139-
140- public async Task DeleteByExpressionAsync (Expression <Func <TEntity , bool >> expression , CancellationToken cancellationToken = default )
141- {
142- TEntity entity = await Entity .Where (expression ).AsNoTracking ().FirstOrDefaultAsync (cancellationToken );
143- Entity .Remove (entity );
144- }
145-
146- public async Task DeleteByIdAsync (string id )
147- {
148- TEntity entity = await Entity .FindAsync (id );
149- Entity .Remove (entity );
150- }
151-
152- public void DeleteRange (ICollection <TEntity > entities )
153- {
154- Entity .RemoveRange (entities );
155- }
156-
157- public IQueryable <TEntity > GetAll ()
158- {
159- return Entity .AsNoTracking ().AsQueryable ();
160- }
161-
162- public IQueryable <TEntity > GetAllWithTracking ()
163- {
164- return Entity .AsQueryable ();
165- }
166-
167- public async Task <TEntity > FirstOrDefaultAsync (Expression <Func <TEntity , bool >> expression , CancellationToken cancellationToken = default , bool isTrackingActive = true )
168- {
169- TEntity entity ;
170- if (isTrackingActive )
171- {
172- entity = await Entity .Where (expression ).FirstOrDefaultAsync (cancellationToken );
173- }
174- else
175- {
176- entity = await Entity .Where (expression ).AsNoTracking ().FirstOrDefaultAsync (cancellationToken );
177- }
178-
179- return entity ;
180- }
181-
182- public TEntity GetByExpression (Expression <Func <TEntity , bool >> expression )
183- {
184- TEntity entity = Entity .Where (expression ).AsNoTracking ().FirstOrDefault ();
185- return entity ;
186- }
187-
188- public async Task <TEntity > GetByExpressionAsync (Expression <Func <TEntity , bool >> expression , CancellationToken cancellationToken = default )
189- {
190- TEntity entity = await Entity .Where (expression ).AsNoTracking ().FirstOrDefaultAsync (cancellationToken );
191- return entity ;
192- }
193-
194- public TEntity GetByExpressionWithTracking (Expression <Func <TEntity , bool >> expression )
195- {
196- TEntity entity = Entity .Where (expression ).FirstOrDefault ();
197- return entity ;
198- }
199-
200- public async Task <TEntity > GetByExpressionWithTrackingAsync (Expression <Func <TEntity , bool >> expression , CancellationToken cancellationToken = default )
201- {
202- TEntity entity = await Entity .Where (expression ).FirstOrDefaultAsync (cancellationToken );
203- return entity ;
204- }
205-
206- public TEntity GetFirst ()
207- {
208- TEntity entity = Entity .AsNoTracking ().FirstOrDefault ();
209- return entity ;
210- }
211-
212- public async Task <TEntity > GetFirstAsync (CancellationToken cancellationToken = default )
213- {
214- TEntity entity = await Entity .AsNoTracking ().FirstOrDefaultAsync (cancellationToken );
215- return entity ;
216- }
217-
218- public IQueryable <TEntity > Where (Expression <Func <TEntity , bool >> expression )
219- {
220- return Entity .AsNoTracking ().Where (expression ).AsQueryable ();
221- }
222-
223- public IQueryable <TEntity > WhereWithTracking (Expression <Func <TEntity , bool >> expression )
224- {
225- return Entity .Where (expression ).AsQueryable ();
226- }
227-
228- public void Update (TEntity entity )
229- {
230- Entity .Update (entity );
231- }
232-
233- public void UpdateRange (ICollection <TEntity > entities )
234- {
235- Entity .UpdateRange (entities );
236- }
237- }
238-
239-
240- ```
241-
242- ``` Csharp
243- public interface IUnitOfWork
244- {
245- Task <int > SaveChangesAsync (CancellationToken cancellationToken = default );
246- int SaveChanges ();
247- }
248- ```
94+ ```
0 commit comments