AA的开源地址

https://github.com/ChengLab/AAFrameWork 

AA框架是一个基础应用框架,是建立在众多大家熟知的流行工具之上并与之集成。比如:ASP.NET Core、Automapper、Dapper、Dapper-FluentMap、RabbitMQ、Redis、MassTransit、Log4net等等

大家可以很方便的去使用,学习成本很低,也易于扩展。目标能做成一个大家都能吼得住、可以自己改进的框架。

AA这个名字来源于AA制,一起贡献于社区才能从社区获取果实。

基于AA创建一个示例demo

示例demo 很简单,创建一个任务管理的模块,包含增删改查的功能。

示例项目架构图 

.NET Core应用框架AA介绍(二)-LMLPHP

示例项目截图

.NET Core应用框架AA介绍(二)-LMLPHP

准备工作

数据库脚本:

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("删除成功"));
        }

最后运行,新增一条记录如图:

.NET Core应用框架AA介绍(二)-LMLPHP

.NET Core应用框架AA介绍(二)-LMLPHP

 文中展示了部分代码,整个示例项目的代码会上传到github上,这个示例项目不会是最终版本,还会继续。.NET Core 3.1这个长期版本快发布了,期待ing。。。。。

12-05 21:28