- 定義實體
首先定義一個實體,例如 Product:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
CopyInsert
2. 建立 DbContext
建立一個 DbContext 類:
public class AppDbContext : DbContext
{
public DbSet
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
}
CopyInsert
3. 定義倉庫介面和實現
定義一個通用倉庫介面:
public interface IRepository
{
Task<IEnumerable
Task
Task AddAsync(T entity);
Task UpdateAsync(T entity);
Task DeleteAsync(int id);
}
CopyInsert
實現該介面:
public class Repository
{
private readonly AppDbContext _context;
private readonly DbSet
public Repository(AppDbContext 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);
_dbSet.Remove(entity);
}
}
CopyInsert
4. 定義工作單元介面和實現
定義工作單元介面:
public interface IUnitOfWork
{
Task
}
CopyInsert
實現工作單元:
public class UnitOfWork : IUnitOfWork
{
private readonly AppDbContext _context;
public UnitOfWork(AppDbContext context)
{
_context = context;
}
public async Task<int> SaveChangesAsync() => await _context.SaveChangesAsync();
}
CopyInsert
5. 建立服務層
建立服務層來處理業務邏輯:
public interface IProductService
{
Task<IEnumerable
Task
Task AddProductAsync(Product product);
Task UpdateProductAsync(Product product);
Task DeleteProductAsync(int id);
}
public class ProductService : IProductService
{
private readonly IRepository
private readonly IUnitOfWork _unitOfWork;
public ProductService(IRepository<Product> productRepository, IUnitOfWork unitOfWork)
{
_productRepository = productRepository;
_unitOfWork = unitOfWork;
}
public async Task<IEnumerable<Product>> GetAllProductsAsync() => await _productRepository.GetAllAsync();
public async Task<Product> GetProductByIdAsync(int id) => await _productRepository.GetByIdAsync(id);
public async Task AddProductAsync(Product product)
{
await _productRepository.AddAsync(product);
await _unitOfWork.SaveChangesAsync();
}
public async Task UpdateProductAsync(Product product)
{
await _productRepository.UpdateAsync(product);
await _unitOfWork.SaveChangesAsync();
}
public async Task DeleteProductAsync(int id)
{
await _productRepository.DeleteAsync(id);
await _unitOfWork.SaveChangesAsync();
}
}
CopyInsert
6. 建立控制器
最後,建立一個控制器來處理 HTTP 請求:
[ApiController]
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private readonly IProductService _productService;
public ProductsController(IProductService productService)
{
_productService = productService;
}
[HttpGet]
public async Task<IActionResult> GetAll() => Ok(await _productService.GetAllProductsAsync());
[HttpGet("{id}")]
public async Task<IActionResult> GetById(int id) => Ok(await _productService.GetProductByIdAsync(id));
[HttpPost]
public async Task<IActionResult> Create(Product product)
{
await _productService.AddProductAsync(product);
return CreatedAtAction(nameof(GetById), new { id = product.Id }, product);
}
[HttpPut("{id}")]
public async Task<IActionResult> Update(int id, Product product)
{
if (id != product.Id)
{
return BadRequest();
}
await _productService.UpdateProductAsync(product);
return NoContent();
}
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
await _productService.DeleteProductAsync(id);
return NoContent();
}
}
CopyInsert
7. 在 Startup.cs 中配置服務
最後,在 Startup.cs(或 Program.cs)中配置服務:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext
services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
services.AddScoped<IUnitOfWork, UnitOfWork>();
services.AddScoped<IProductService, ProductService>();
services.AddControllers();
}
CopyInsert
透過以上步驟,你將可以在 ASP.NET Core 應用程式中實現一個基本的倉庫和工作單元模式,以及服務層和控制器的分離。這樣有助於保持程式碼的清晰和可維護性。