Sau khi tạo dự án từ mẫu chính thức tại aspnetboilerplate.com/Templates, bạn nên mở giải pháp bằng Visual Studio 2019 (phiên bản 2017 không hỗ trợ .NET Core 3.0 trở lên). Cấu trúc dự án tiêu chuẩn gồm các lớp: Core, EntityFramework, và Application.
1. Thêm lớp thực thể mới trong dự án Core
Tạo hai thực thể LookupValue và ValueMapping để biểu diễn dữ liệu tra cứu và quan hệ ánh xạ:
public class LookupValue : Entity<int>
{
public string Code { get; set; }
public string Description { get; set; }
public bool IsActive { get; set; }
public DateTime CreatedAt { get; set; }
}
public class ValueMapping : Entity<long>
{
public int SourceId { get; set; }
public int TargetId { get; set; }
public string Context { get; set; }
}
2. Định nghĩa giao diện kho lưu trữ trong Core
Thêm các interface kế thừa từ IRepository<TEntity, TKey>:
public interface ILookupValueRepository : IRepository<LookupValue, int>
{
List<LookupValue> GetActiveValues();
}
public interface IValueMappingRepository : IRepository<ValueMapping, long>
{
IQueryable<ValueMapping> GetByContext(string context);
}
3. Triển khai lớp kho lưu trữ trong EntityFramework
Tạo các lớp triển khai cụ thể và đăng ký trong WuMingDbContext:
public class LookupValueRepository : WuMingRepositoryBase<LookupValue, int>, ILookupValueRepository
{
public LookupValueRepository(IDbContextProvider<WuMingDbContext> dbContextProvider)
: base(dbContextProvider) { }
public List<LookupValue> GetActiveValues() =>
GetAll().Where(x => x.IsActive).ToList();
}
public class ValueMappingRepository : WuMingRepositoryBase<ValueMapping, long>, IValueMappingRepository
{
public ValueMappingRepository(IDbContextProvider<WuMingDbContext> dbContextProvider)
: base(dbContextProvider) { }
public IQueryable<ValueMapping> GetByContext(string context) =>
GetAll().Where(x => x.Context == context);
}
Đảm bảo thêm thuộc tính DbSet tương ứng trong WuMingDbContext:
public virtual DbSet<LookupValue> LookupValues { get; set; }
public virtual DbSet<ValueMapping> ValueMappings { get; set; }
4. Tạo dịch vụ ứng dụng trong Application
Định nghĩa interface ILookupValueAppService với các thao tác CRUD cơ bản:
public interface ILookupValueAppService : IApplicationService
{
Task<List<LookupValueDto>> GetAllAsync();
Task<LookupValueDto> GetByIdAsync(int id);
Task CreateAsync(CreateLookupValueDto input);
Task UpdateAsync(UpdateLookupValueDto input);
Task DeleteAsync(int id);
}
Cài đặt lớp dịch vụ với dependency injection rõ ràng và xử lý ngoại lệ có kiểm soát:
[AbpAuthorize]
public class LookupValueAppService : WuMingAppServiceBase, ILookupValueAppService
{
private readonly ILookupValueRepository _valueRepo;
private readonly IValueMappingRepository _mappingRepo;
public LookupValueAppService(
ILookupValueRepository valueRepo,
IValueMappingRepository mappingRepo)
{
_valueRepo = valueRepo;
_mappingRepo = mappingRepo;
}
public async Task<List<LookupValueDto>> GetAllAsync()
{
var values = await _valueRepo.GetAllListAsync();
return ObjectMapper.Map<List<LookupValueDto>>(values);
}
public async Task<LookupValueDto> GetByIdAsync(int id)
{
var entity = await _valueRepo.GetAsync(id);
return ObjectMapper.Map<LookupValueDto>(entity);
}
public async Task CreateAsync(CreateLookupValueDto input)
{
var entity = ObjectMapper.Map<LookupValue>(input);
entity.CreatedAt = Clock.Now;
await _valueRepo.InsertAsync(entity);
}
public async Task UpdateAsync(UpdateLookupValueDto input)
{
var entity = await _valueRepo.GetAsync(input.Id);
ObjectMapper.Map(input, entity);
await _valueRepo.UpdateAsync(entity);
}
public async Task DeleteAsync(int id)
{
await _valueRepo.DeleteAsync(id);
}
}
5. Kiểm tra API qua Swagger
Sau khi hoàn tất, thiết lập dự án Web.Host làm startup project, nhấn F5 để chạy ứng dụng. Truy cập https://localhost:XXXX/swagger để xem tài liệu API tự sinh — các endpoint mới sẽ xuất hiện dưới nhóm LookupValue, kèm mô tả tham số và mã phản hồi.