.NET Core應用框架AA介紹(二)
阿新 • • 發佈:2019-12-05
AA的開源地址
https://github.com/ChengLab/AAFrameWork
AA框架是一個基礎應用框架,是建立在眾多大家熟知的流行工具之上並與之整合。比如:ASP.NET Core、Automapper、Dapper、Dapper-FluentMap、RabbitMQ、Redis、MassTransit、Log4net等等
大家可以很方便的去使用,學習成本很低,也易於擴充套件。目標能做成一個大家都能吼得住、可以自己改進的框架。
AA這個名字來源於AA制,一起貢獻於社群才能從社群獲取果實。
基於AA建立一個示例demo
示例demo 很簡單,建立一個任務管理的模組,包含增刪改查的功能。
示例專案架構圖
示例專案截圖
準備工作
資料庫指令碼:
CREATE TABLE [dbo].[QuartzJobdetail]( [Id] [int] IDENTITY(1,1) NOT NULL, [JobGroup] [varchar](100) NULL, [JobName] [varchar](100) NULL, [RunStatus] [int] NULL, [Cron] [varchar](100) NULL, [StartTime] [datetime] NULL, [EndTime] [datetime] NULL, [Description] [varchar](100) NULL, [GmtCreateTime] [datetime] NULL, [ApiUrl] [varchar](100) NULL, [Status] [int] NULL, CONSTRAINT [PK_QUARTZJOBDETAIL] PRIMARY KEY CLUSTERED ( [Id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO
基礎設施層
配置資料庫連線 和指定資料庫型別
public AADapperContent() : base(new NameValueCollection() { ["aa.dataSource.AaBase.connectionString"] = "Data Source =.; Initial Catalog = QuartzAA-Job;User ID = sa; Password = 123;", ["aa.dataSource.AaBase.provider"] = "SqlServer" }) { }
配置領域實體和資料庫表對映
public class QuartzJobdetailMap:DommelEntityMap<QuartzJobdetail> { public QuartzJobdetailMap() { ToTable("QuartzJobdetail"); Map(x=>x.Id).IsKey().IsIdentity(); } }
實現倉儲層
public class QuartzJobdetailRepository:DapperRepository<QuartzJobdetail>, IQuartzJobdetailRepository { public IPage<QuartzJobdetailDto> GetListReturnOrder(GetListQuartzJobDetailInput input) { object sqlParam = null; var sql = new StringBuilder(); sql.Append("select * from QuartzJobdetail "); sql.Append(" where 1=1"); var result = DapperContext.Current.DataBase.GetPage<QuartzJobdetailDto>(new PageRequest { PageIndex = input.PageIndex, PageSize = input.PageSize, SqlText = sql.ToString(), SqlParam = sqlParam, OrderFiled = " Id desc ", }); return result; } }
DapperRepository 實現了IDapperRepository介面,IDapperRepository介面程式碼如下
public virtual bool Delete(TEntity entity); public virtual Task<bool> DeleteAsync(TEntity entity); public virtual bool DeleteMultiple(Expression<Func<TEntity, bool>> predicate); public virtual Task<bool> DeleteMultipleAsync(Expression<Func<TEntity, bool>> predicate); public virtual TEntity FirstOrDefault(Expression<Func<TEntity, bool>> predicate); public virtual Task<TEntity> FirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate); public virtual TEntity Get(object id); public virtual IEnumerable<TEntity> GetAll(); public virtual Task<IEnumerable<TEntity>> GetAllAsync(); public virtual Task<TEntity> GetAsync(object id); public virtual object Insert(TEntity entity); public virtual Task<object> InsertAsync(TEntity entity); public virtual IEnumerable<TEntity> Select(Expression<Func<TEntity, bool>> predicate); public virtual Task<IEnumerable<TEntity>> SelectAsync(Expression<Func<TEntity, bool>> predicate); public virtual bool Update(TEntity entity); public virtual Task<bool> UpdateAsync(TEntity entity);
領域層
示例專案,比較簡單,並沒有完全按照DDD去實現領域服務,領域事件等等,這裡主要演示如何基於AA基礎框架建立一個專案。領域模型(貧血)程式碼如下
public class QuartzJobdetail { /// <summary> /// 編號 /// </summary> public int Id { get; set; } /// <summary> /// 任務組 /// </summary> public string JobGroup { get; set; } /// <summary> /// 任務名稱 /// </summary> public string JobName { get; set; } /// <summary> /// 執行狀態 /// </summary> public int RunStatus { get; set; } /// <summary> /// cron表示式 /// </summary> public string Cron { get; set; } /// <summary> /// 開始日期 /// </summary> public DateTime StartTime { get; set; } /// <summary> /// 結束日期 /// </summary> public DateTime EndTime { get; set; } /// <summary> /// 描述 /// </summary> public string Description { get; set; } /// <summary> /// 建立日期 /// </summary> public DateTime GmtCreateTime { get; set; } /// <summary> /// api地址 /// </summary> public string ApiUrl { get; set; } /// <summary> /// 狀態 /// </summary> public int Status { get; set; } }
倉儲介面程式碼如下
public interface IQuartzJobdetailRepository: IDapperRepository<QuartzJobdetail> { IPage<QuartzJobdetailDto> GetListReturnOrder(GetListQuartzJobDetailInput input); }
應用層
服務介面和實現程式碼如下
public interface IQuartzJobdetailService { void Save(SaveQuartzJobdetailInput input); void Update(UpdateQuartzJobdetailInput input); void Remove(RemoveQuartzJobdetailInput input); QuartzJobdetailDto GetQuartzJobdetail(GetQuartzJobdetailInput input); PagedResultDto<QuartzJobdetailDto> GetList(GetListQuartzJobDetailInput input); } public class QuartzJobdetailService : IQuartzJobdetailService { #region filed private readonly IQuartzJobdetailRepository _quartzJobdetailRepository; #endregion #region actor public QuartzJobdetailService() { var dapperContent = new AADapperContent(); _quartzJobdetailRepository = new QuartzJobdetailRepository(); } #endregion public void Save(SaveQuartzJobdetailInput input) { var obj = _quartzJobdetailRepository.Insert(new QuartzJobdetail { JobGroup = input.JobGroup, JobName = input.JobName, RunStatus = input.RunStatus, Cron = input.Cron, StartTime = input.StartTime, EndTime = input.EndTime, Description = input.Description, GmtCreateTime = DateTime.Now, ApiUrl = input.ApiUrl, Status = input.Status, }); } public void Update(UpdateQuartzJobdetailInput input) { var model = _quartzJobdetailRepository.Get(input.Id); model.JobGroup = input.JobGroup; model.JobName = input.JobName; model.Cron = input.Cron; model.StartTime = input.StartTime; model.EndTime = input.EndTime; model.Description = input.Description; model.ApiUrl = input.ApiUrl; _quartzJobdetailRepository.Update(model); } public void Remove(RemoveQuartzJobdetailInput input) { var model = _quartzJobdetailRepository.Get(input.Id); _quartzJobdetailRepository.Delete(model); } public QuartzJobdetailDto GetQuartzJobdetail(GetQuartzJobdetailInput input) { var model = _quartzJobdetailRepository.FirstOrDefault(p => p.Description.Contains(input.Description)); return new QuartzJobdetailDto() { JobGroup = model.JobGroup, JobName = model.JobName, RunStatus = model.RunStatus, Cron = model.Cron, StartTime = model.StartTime, EndTime = model.EndTime, Description = model.Description, ApiUrl = model.ApiUrl, }; } public PagedResultDto<QuartzJobdetailDto> GetList(GetListQuartzJobDetailInput input) { var result = _quartzJobdetailRepository.GetListReturnOrder(input); return new PagedResultDto<QuartzJobdetailDto> { TotalCount = result.Count, Items = result.Data.ToList() }; }
表現層
控制器的程式碼
public IActionResult Index() { return View(); } /// <summary> /// job列表 /// </summary> /// <param name="limit">每頁顯示條數</param> /// <param name="start"></param> /// <param name="page">頁碼</param> /// <param name="draw"></param> /// <returns></returns> public IActionResult GetListQuartzJobdetail(int limit, int start, int page, int draw) { var result = _quartzJobdetailService.GetList(new GetListQuartzJobDetailInput() { PageIndex = page, PageSize = limit, }); var vm = new PageResponse<QuartzJobdetailViewModel> { draw = draw, recordsTotal = result.TotalCount, recordsFiltered = result.TotalCount, data = result.Items.MapTo<List<QuartzJobdetailViewModel>>() }; return Json(vm); } /// <summary> /// 新增 /// </summary> /// <param name="vm"></param> /// <returns></returns> public IActionResult Save(QuartzJobdetailVm vm) { _quartzJobdetailService.Save(vm.MapTo<SaveQuartzJobdetailInput>()); return Json(Result.Success("操作成功")); } /// <summary> /// 編輯 /// </summary> /// <param name="vm"></param> /// <returns></returns> public IActionResult Update(QuartzJobdetailVm vm) { _quartzJobdetailService.Update(vm.MapTo<UpdateQuartzJobdetailInput>()); return Json(Result.Success("修改成功")); } /// <summary> /// 刪除 /// </summary> /// <param name="id"></param> /// <returns></returns> [HttpPost] public IActionResult Remove(int id) { _quartzJobdetailService.Remove(new RemoveQuartzJobdetailInput { Id = id }); return Json(Result.Success("刪除成功")); }
最後執行,新增一條記錄如圖:
文中展示了部分程式碼,整個示例專案的程式碼會上傳到github上,這個示例專案不會是最終版本,還會繼續。.NET Core 3.1這個長期版本快釋出了,期待ing。。。。。