2020/01/29, ASP.NET Core 3.1, VS2019html
摘要:基於ASP.NET Core 3.1 WebApi搭建後端多層網站架構【8.3-編寫角色業務的增刪改】
編寫最簡單的增刪改業務,涉及到DI依賴注入的使用、AutoMapper的使用、工做單元與倉儲的使用、雪花Id的生成git
文章目錄github
此分支項目代碼web
本章節介紹了編寫最簡單的增刪改查業務,涉及到DI依賴注入的使用、AutoMapper的使用、工做單元與倉儲的使用sql
向MS.Services
類庫添加對MS.Models
項目的引用數據庫
在MS.Services
類庫中添加BaseService.cs
類:後端
using AutoMapper; using MS.Common.IDCode; using MS.DbContexts; using MS.UnitOfWork; namespace MS.Services { public interface IBaseService { } public class BaseService : IBaseService { public readonly IUnitOfWork<MSDbContext> _unitOfWork; public readonly IMapper _mapper; public readonly IdWorker _idWorker; public BaseService(IUnitOfWork<MSDbContext> unitOfWork, IMapper mapper, IdWorker idWorker) { _unitOfWork = unitOfWork; _mapper = mapper; _idWorker = idWorker; } } }
說明:api
在MS.Services
類庫中新建Role文件夾,在該文件夾下新建IRoleService.cs
、RoleService.cs
類,注意我這裏兩個類的命名空間都改成namespace MS.Services
,而不是默認的namespace MS.Services.Role
緩存
修改IRoleService爲public類型的interface接口,繼承自IBaseService,並添加Create、Update、Delete三個方法:架構
using MS.Entities; using MS.Models.ViewModel; using MS.WebCore.Core; using System.Threading.Tasks; namespace MS.Services { public interface IRoleService : IBaseService { Task<ExecuteResult<Role>> Create(RoleViewModel viewModel); Task<ExecuteResult> Update(RoleViewModel viewModel); Task<ExecuteResult> Delete(RoleViewModel viewModel); } }
修改RoleService爲public類型,繼承自BaseService和IRoleService接口,而後使用快速操做生成構造函數和實現接口
接下來開始實現新增Role的業務方法:
public async Task<ExecuteResult<Role>> Create(RoleViewModel viewModel) { ExecuteResult<Role> result = new ExecuteResult<Role>(); //檢查字段 if (viewModel.CheckField(ExecuteType.Create, _unitOfWork) is ExecuteResult checkResult && !checkResult.IsSucceed) { return result.SetFailMessage(checkResult.Message); } using (var tran = _unitOfWork.BeginTransaction())//開啓一個事務 { Role newRow = _mapper.Map<Role>(viewModel); newRow.Id = _idWorker.NextId();//獲取一個雪花Id newRow.Creator = 1219490056771866624;//因爲暫時尚未作登陸,因此拿不到登陸者信息,先隨便寫一個後面再完善 newRow.CreateTime = DateTime.Now; _unitOfWork.GetRepository<Role>().Insert(newRow); await _unitOfWork.SaveChangesAsync(); await tran.CommitAsync();//提交事務 result.SetData(newRow);//添加成功,把新的實體返回回去 } return result; }
說明:
在MS.WebApi
應用程序中,RoleController.cs
類中,添加RoleService接口,並在Post中調用Create方法:
//using MS.Services; //以上代碼添加到using引用 public class RoleController : ControllerBase { private readonly IRoleService _roleService; public RoleController(IRoleService roleService) { _roleService = roleService; } [HttpPost] public async Task<ExecuteResult> Post(RoleViewModel viewModel) { return await _roleService.Create(viewModel); } }
說明:
在MS.WebApi
應用程序中,在Startup.cs
類的ConfigureServices方法中追加:
//using MS.Services; //以上代碼添加到using //註冊IBaseService和IRoleService接口及對應的實現類 services.AddScoped<IBaseService, BaseService>(); services.AddScoped<IRoleService, RoleService>();
完成後啓動項目,打開Postman調試接口:
能夠看到接口調用成功(也能夠看到在控制檯中有EntityFrameworkCore執行的sql語句)
在數據庫中看到新增的記錄:
再重複調用一次接口,則會提示角色名稱已存在:
完整的增刪改角色業務代碼:
using AutoMapper; using MS.Common.IDCode; using MS.DbContexts; using MS.Entities; using MS.Models.ViewModel; using MS.UnitOfWork; using MS.WebCore.Core; using System; using System.Collections.Generic; using System.Text; using System.Threading.Tasks; namespace MS.Services { public class RoleService : BaseService, IRoleService { public RoleService(IUnitOfWork<MSDbContext> unitOfWork, IMapper mapper, IdWorker idWorker) : base(unitOfWork, mapper, idWorker) { } public async Task<ExecuteResult<Role>> Create(RoleViewModel viewModel) { ExecuteResult<Role> result = new ExecuteResult<Role>(); //檢查字段 if (viewModel.CheckField(ExecuteType.Create, _unitOfWork) is ExecuteResult checkResult && !checkResult.IsSucceed) { return result.SetFailMessage(checkResult.Message); } using (var tran = _unitOfWork.BeginTransaction())//開啓一個事務 { Role newRow = _mapper.Map<Role>(viewModel); newRow.Id = _idWorker.NextId();//獲取一個雪花Id newRow.Creator = 1219490056771866624;//因爲暫時尚未作登陸,因此拿不到登陸者信息,先隨便寫一個後面再完善 newRow.CreateTime = DateTime.Now; _unitOfWork.GetRepository<Role>().Insert(newRow); await _unitOfWork.SaveChangesAsync(); await tran.CommitAsync();//提交事務 result.SetData(newRow);//添加成功,把新的實體返回回去 } return result; } public async Task<ExecuteResult> Delete(RoleViewModel viewModel) { ExecuteResult result = new ExecuteResult(); //檢查字段 if (viewModel.CheckField(ExecuteType.Delete, _unitOfWork) is ExecuteResult checkResult && !checkResult.IsSucceed) { return checkResult; } _unitOfWork.GetRepository<Role>().Delete(viewModel.Id); await _unitOfWork.SaveChangesAsync();//提交 return result; } public async Task<ExecuteResult> Update(RoleViewModel viewModel) { ExecuteResult result = new ExecuteResult(); //檢查字段 if (viewModel.CheckField(ExecuteType.Update, _unitOfWork) is ExecuteResult checkResult && !checkResult.IsSucceed) { return checkResult; } //從數據庫中取出該記錄 var row = await _unitOfWork.GetRepository<Role>().FindAsync(viewModel.Id);//在viewModel.CheckField中已經獲取了一次用於檢查,因此此處不會重複再從數據庫取一次,有緩存 //修改對應的值 row.Name = viewModel.Name; row.DisplayName = viewModel.DisplayName; row.Remark = viewModel.Remark; row.Modifier = 1219490056771866624;//因爲暫時尚未作登陸,因此拿不到登陸者信息,先隨便寫一個後面再完善 row.ModifyTime = DateTime.Now; _unitOfWork.GetRepository<Role>().Update(row); await _unitOfWork.SaveChangesAsync();//提交 return result; } } }
完整的接口代碼:
using Microsoft.AspNetCore.Mvc; using MS.Models.ViewModel; using MS.Services; using MS.WebCore.Core; using System.Threading.Tasks; namespace MS.WebApi.Controllers { [Route("[controller]")] [ApiController] public class RoleController : ControllerBase { private readonly IRoleService _roleService; public RoleController(IRoleService roleService) { _roleService = roleService; } [HttpPost] public async Task<ExecuteResult> Post(RoleViewModel viewModel) { return await _roleService.Create(viewModel); } [HttpPut] public async Task<ExecuteResult> Put(RoleViewModel viewModel) { return await _roleService.Update(viewModel); } [HttpDelete] public async Task<ExecuteResult> Delete(long id) { return await _roleService.Delete(new RoleViewModel { Id = id }); } } }
更新接口測試:
刪除接口測試:
項目完成後,以下圖所示