feat(Repositories): Added Compiled Queries
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Shop.Core.SharedKernel;
|
||||
@@ -15,6 +16,13 @@ internal abstract class BaseWriteOnlyRepository<TEntity, Tkey>(WriteDbContext co
|
||||
where TEntity : class, IEntity<Tkey>
|
||||
where Tkey : IEquatable<Tkey>
|
||||
{
|
||||
private static readonly Func<WriteDbContext, Tkey, Task<TEntity>> GetByIdCompiledAsync =
|
||||
EF.CompileAsyncQuery((WriteDbContext context, Tkey id) =>
|
||||
context
|
||||
.Set<TEntity>()
|
||||
.AsNoTrackingWithIdentityResolution()
|
||||
.FirstOrDefault(entity => entity.Id.Equals(id)));
|
||||
|
||||
private readonly DbSet<TEntity> _dbSet = context.Set<TEntity>();
|
||||
protected readonly WriteDbContext Context = context;
|
||||
|
||||
@@ -28,7 +36,7 @@ internal abstract class BaseWriteOnlyRepository<TEntity, Tkey>(WriteDbContext co
|
||||
_dbSet.Remove(entity);
|
||||
|
||||
public async Task<TEntity> GetByIdAsync(Tkey id) =>
|
||||
await _dbSet.AsNoTrackingWithIdentityResolution().FirstOrDefaultAsync(entity => entity.Id.Equals(id));
|
||||
await GetByIdCompiledAsync(Context, id);
|
||||
|
||||
#region IDisposable
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Shop.Domain.Entities.CustomerAggregate;
|
||||
@@ -11,13 +12,23 @@ namespace Shop.Infrastructure.Data.Repositories;
|
||||
internal class CustomerWriteOnlyRepository(WriteDbContext context)
|
||||
: BaseWriteOnlyRepository<Customer, Guid>(context), ICustomerWriteOnlyRepository
|
||||
{
|
||||
private static readonly Func<WriteDbContext, string, Task<bool>> ExistsByEmailCompiledAsync =
|
||||
EF.CompileAsyncQuery((WriteDbContext context, string email) =>
|
||||
context
|
||||
.Customers
|
||||
.AsNoTracking()
|
||||
.Any(customer => customer.Email.Address == email));
|
||||
|
||||
private static readonly Func<WriteDbContext, string, Guid, Task<bool>> ExistsByEmailAndIdCompiledAsync =
|
||||
EF.CompileAsyncQuery((WriteDbContext context, string email, Guid currentId) =>
|
||||
context
|
||||
.Customers
|
||||
.AsNoTracking()
|
||||
.Any(customer => customer.Email.Address == email && customer.Id != currentId));
|
||||
|
||||
public async Task<bool> ExistsByEmailAsync(Email email) =>
|
||||
await Context.Customers
|
||||
.AsNoTracking()
|
||||
.AnyAsync(customer => customer.Email.Address == email.Address);
|
||||
await ExistsByEmailCompiledAsync(Context, email.Address);
|
||||
|
||||
public async Task<bool> ExistsByEmailAsync(Email email, Guid currentId) =>
|
||||
await Context.Customers
|
||||
.AsNoTracking()
|
||||
.AnyAsync(customer => customer.Email.Address == email.Address && customer.Id != currentId);
|
||||
await ExistsByEmailAndIdCompiledAsync(Context, email.Address, currentId);
|
||||
}
|
||||
Reference in New Issue
Block a user