- 倉儲(Repository)
倉儲模式封裝對資料來源的訪問邏輯,包括CRUD操作。以下是一個簡單的倉儲介面和實現示例:
public interface IRepository
{
Task<IEnumerable
Task
Task AddAsync(T entity);
Task UpdateAsync(T entity);
Task DeleteAsync(int id);
}
public class Repository
{
private readonly DbContext _context;
private readonly DbSet
public Repository(DbContext context)
{
_context = context;
_dbSet = context.Set<T>();
}
public async Task<IEnumerable<T>> GetAllAsync() => await _dbSet.ToListAsync();
public async Task<T> GetByIdAsync(int id) => await _dbSet.FindAsync(id);
public async Task AddAsync(T entity) => await _dbSet.AddAsync(entity);
public async Task UpdateAsync(T entity) => _dbSet.Update(entity);
public async Task DeleteAsync(int id)
{
var entity = await GetByIdAsync(id);
if (entity != null)
{
_dbSet.Remove(entity);
}
}
}
CopyInsert
2. 工作單元(Unit of Work)
工作單元職責是協調多個倉儲之間的操作,並統一提交事務。以下是一個工作單元的介面和實現示例:
public interface IUnitOfWork : IDisposable
{
IRepository
Task
}
public class UnitOfWork : IUnitOfWork
{
private readonly DbContext _context;
public UnitOfWork(DbContext context)
{
_context = context;
}
public IRepository<TEntity> Repository<TEntity>() where TEntity : class
{
return new Repository<TEntity>(_context);
}
public async Task<int> SaveChangesAsync() => await _context.SaveChangesAsync();
public void Dispose()
{
_context.Dispose();
}
}
CopyInsert
3. 服務層(Service Layer)
服務層處理業務邏輯,並呼叫倉儲和工作單元。以下是服務層的示例:
public interface IMyService
{
Task<IEnumerable
Task
Task AddEntityAsync(MyEntity entity);
Task UpdateEntityAsync(MyEntity entity);
Task DeleteEntityAsync(int id);
}
public class MyService : IMyService
{
private readonly IUnitOfWork _unitOfWork;
public MyService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async Task<IEnumerable<MyEntity>> GetAllEntitiesAsync()
{
return await _unitOfWork.Repository<MyEntity>().GetAllAsync();
}
public async Task<MyEntity> GetEntityByIdAsync(int id)
{
return await _unitOfWork.Repository<MyEntity>().GetByIdAsync(id);
}
public async Task AddEntityAsync(MyEntity entity)
{
await _unitOfWork.Repository<MyEntity>().AddAsync(entity);
await _unitOfWork.SaveChangesAsync();
}
public async Task UpdateEntityAsync(MyEntity entity)
{
await _unitOfWork.Repository<MyEntity>().UpdateAsync(entity);
await _unitOfWork.SaveChangesAsync();
}
public async Task DeleteEntityAsync(int id)
{
await _unitOfWork.Repository<MyEntity>().DeleteAsync(id);
await _unitOfWork.SaveChangesAsync();
}
}
CopyInsert
4. 控制器(Controller)
控制器接收使用者請求並呼叫服務層。以下是控制器的示例:
[ApiController]
[Route("[controller]")]
public class MyEntityController : ControllerBase
{
private readonly IMyService _myService;
public MyEntityController(IMyService myService)
{
_myService = myService;
}
[HttpGet]
public async Task<IActionResult> GetAll()
{
var entities = await _myService.GetAllEntitiesAsync();
return Ok(entities);
}
[HttpGet("{id}")]
public async Task<IActionResult> Get(int id)
{
var entity = await _myService.GetEntityByIdAsync(id);
if (entity == null) return NotFound();
return Ok(entity);
}
[HttpPost]
public async Task<IActionResult> Post([FromBody] MyEntity entity)
{
await _myService.AddEntityAsync(entity);
return CreatedAtAction(nameof(Get), new { id = entity.Id }, entity);
}
[HttpPut("{id}")]
public async Task<IActionResult> Put(int id, [FromBody] MyEntity entity)
{
if (id != entity.Id) return BadRequest();
await _myService.UpdateEntityAsync(entity);
return NoContent();
}
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
await _myService.DeleteEntityAsync(id);
return NoContent();
}
}
CopyInsert
總結
透過結合使用倉儲、工作單元、服務層和控制器,可以構建一個高內聚、低耦合的分層架構。這種設計有助於增強程式碼的可讀性、可維護性和可擴充套件性。在實際開發中,可以根據團隊和專案的需求進行適當的調整和最佳化。