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{ public QuartzJobdetailMap() { ToTable("QuartzJobdetail"); Map(x=>x.Id).IsKey().IsIdentity(); } }
实现仓储层
public class QuartzJobdetailRepository:DapperRepository, IQuartzJobdetailRepository { public IPage 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 (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(Expressionbool>> predicate); public virtual Task<bool> DeleteMultipleAsync(Expression bool>> predicate); public virtual TEntity FirstOrDefault(Expression bool>> predicate); public virtual Task FirstOrDefaultAsync(Expression bool>> predicate); public virtual TEntity Get(object id); public virtual IEnumerable GetAll(); public virtual Task > GetAllAsync(); public virtual Task GetAsync(object id); public virtual object Insert(TEntity entity); public virtual Task<object> InsertAsync(TEntity entity); public virtual IEnumerable Select(Expression bool>> predicate); public virtual Task > SelectAsync(Expression bool>> predicate); public virtual bool Update(TEntity entity); public virtual Task<bool> UpdateAsync(TEntity entity);
领域层
示例项目,比较简单,并没有完全按照DDD去实现领域服务,领域事件等等,这里主要演示如何基于AA基础框架创建一个项目。领域模型(贫血)代码如下
public class QuartzJobdetail { ////// 编号 /// public int Id { get; set; } /// /// 任务组 /// public string JobGroup { get; set; } /// /// 任务名称 /// public string JobName { get; set; } /// /// 运行状态 /// public int RunStatus { get; set; } /// /// cron表达式 /// public string Cron { get; set; } /// /// 开始日期 /// public DateTime StartTime { get; set; } /// /// 结束日期 /// public DateTime EndTime { get; set; } /// /// 描述 /// public string Description { get; set; } /// /// 创建日期 /// public DateTime GmtCreateTime { get; set; } /// /// api地址 /// public string ApiUrl { get; set; } /// /// 状态 /// public int Status { get; set; } }
仓储接口代码如下
public interface IQuartzJobdetailRepository: IDapperRepository{ IPage GetListReturnOrder(GetListQuartzJobDetailInput input); }
应用层
服务接口和实现代码如下
public interface IQuartzJobdetailService { void Save(SaveQuartzJobdetailInput input); void Update(UpdateQuartzJobdetailInput input); void Remove(RemoveQuartzJobdetailInput input); QuartzJobdetailDto GetQuartzJobdetail(GetQuartzJobdetailInput input); PagedResultDtoGetList(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 GetList(GetListQuartzJobDetailInput input) { var result = _quartzJobdetailRepository.GetListReturnOrder(input); return new PagedResultDto { TotalCount = result.Count, Items = result.Data.ToList() }; }
表现层
控制器的代码
public IActionResult Index() { return View(); } ////// job列表 /// /// 每页显示条数 /// /// 页码 /// /// 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 { draw = draw, recordsTotal = result.TotalCount, recordsFiltered = result.TotalCount, data = result.Items.MapTo >() }; return Json(vm); } ///
/// 添加 /// /// /// public IActionResult Save(QuartzJobdetailVm vm) { _quartzJobdetailService.Save(vm.MapTo ()); return Json(Result.Success("操作成功")); } /// /// 编辑 /// /// /// public IActionResult Update(QuartzJobdetailVm vm) { _quartzJobdetailService.Update(vm.MapTo ()); return Json(Result.Success("修改成功")); } /// /// 删除 /// /// /// [HttpPost] public IActionResult Remove(int id) { _quartzJobdetailService.Remove(new RemoveQuartzJobdetailInput { Id = id }); return Json(Result.Success("删除成功")); }
最后运行,新增一条记录如图:
文中展示了部分代码,整个示例项目的代码会上传到github上,这个示例项目不会是最终版本,还会继续。.NET Core 3.1这个长期版本快发布了,期待ing。。。。。