จัดระเบียบ Code ใหม่ และเพิ่ม Extension Method

implement GenericRepository Class ใหม่
This commit is contained in:
Suphonchai Phoonsawat 2023-06-26 10:15:50 +07:00
parent e49c6a4aca
commit 89de09d213
21 changed files with 1439 additions and 63 deletions

View file

@ -26,6 +26,8 @@ namespace BMA.EHR.Application.Common.Interfaces
#endregion
DbSet<T> Set<T>() where T : class;
Task<int> SaveChangesAsync();
}
}

View file

@ -1,10 +1,11 @@
using BMA.EHR.Domain.Common;
using Microsoft.AspNetCore.Http;
namespace BMA.EHR.Application.Common.Interfaces
{
public interface IGenericRepository<S,T> where T : class
{
Task<T?> GetByIdAsync(S id);
public interface IGenericRepository<S, T> where T : class
{
Task<T?> GetByIdAsync(S id);
Task<IReadOnlyList<T>> GetAllAsync();
}

View file

@ -1,26 +1,28 @@
using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Domain.Entities.MetaData.BloodGroup;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Http;
namespace BMA.EHR.Application.Repositories.BloodGroup
{
public class BloodGroupRepository : IGenericRepository<Guid, BloodGroupEntity>
public class BloodGroupRepository : GenericRepository<Guid, BloodGroupEntity>
{
private readonly IApplicationDBContext _dbContext;
#region " Fields "
public BloodGroupRepository(IApplicationDBContext dbContext)
private readonly IApplicationDBContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor;
#endregion
#region " Constructor and Destructor "
public BloodGroupRepository(IApplicationDBContext dbContext,
IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
}
public async Task<IReadOnlyList<BloodGroupEntity>> GetAllAsync()
{
return await _dbContext.MD_BloodGroups.ToListAsync();
}
#endregion
public async Task<BloodGroupEntity?> GetByIdAsync(Guid id)
{
return await _dbContext.MD_BloodGroups.FirstOrDefaultAsync(x => x.Id == id);
}
}
}
}

View file

@ -0,0 +1,52 @@
using BMA.EHR.Application.Common.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System.Security.Claims;
namespace BMA.EHR.Application.Repositories
{
public class GenericRepository<S, T> : IGenericRepository<S, T> where T : class
{
#region " Field "
private readonly IApplicationDBContext _dbContext;
private readonly DbSet<T> _dbSet;
private readonly IHttpContextAccessor _httpContextAccessor;
#endregion
#region " Constructor and Destructor "
public GenericRepository(IApplicationDBContext dbContext,
IHttpContextAccessor httpContextAccessor)
{
_dbContext = dbContext;
_dbSet = _dbContext.Set<T>();
_httpContextAccessor = httpContextAccessor;
}
#endregion
#region " Properties "
private string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
private string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
#endregion
#region " Methods "
public async Task<IReadOnlyList<T>> GetAllAsync()
{
return await _dbSet.ToListAsync();
}
public async Task<T?> GetByIdAsync(S id)
{
return await _dbSet.FindAsync(id);
}
#endregion
}
}

View file

@ -1,26 +1,28 @@
using BMA.EHR.Application.Common.Interfaces;
using BMA.EHR.Domain.Entities.MetaData.Prefix;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Http;
namespace BMA.EHR.Application.Repositories.Prefix
{
public class PrefixRepository : IGenericRepository<Guid, PrefixEntity>
public class PrefixRepository : GenericRepository<Guid, PrefixEntity>
{
private readonly IApplicationDBContext _dbContext;
#region " Fields "
public PrefixRepository(IApplicationDBContext dbContext)
private readonly IApplicationDBContext _dbContext;
private readonly IHttpContextAccessor _httpContextAccessor;
#endregion
#region " Constructor and Destructor "
public PrefixRepository(IApplicationDBContext dbContext,
IHttpContextAccessor httpContextAccessor) : base(dbContext, httpContextAccessor)
{
_dbContext = dbContext;
_httpContextAccessor = httpContextAccessor;
}
public async Task<IReadOnlyList<PrefixEntity>> GetAllAsync()
{
return await _dbContext.MD_Prefixes.ToListAsync();
}
#endregion
public async Task<PrefixEntity?> GetByIdAsync(Guid id)
{
return await _dbContext.MD_Prefixes.FirstOrDefaultAsync(x => x.Id == id);
}
}
}