DDD领域驱动设计之运用层代码

1、DDD领域驱动设计实践篇之如何提取模型

2、DDD领域驱动设计之聚合、实体、值对象

3、DDD领域驱动设计之领域基础设施层

4、DDD领域驱动设计之领域服务

5、整体DEMO代码

什么是运用层,说白了就是以前三层的BLL,没有什么特别,只是调用的不是以前的DAL了,而是领域层+基础设施层,运用层接口基本是可以根据原型来做的,也就是UI需要什么数据就定义什么接口,难点就在于怎么去调用领域层了,这个如果是分开来开发的话,难度会很大,为什么是很难呢,原因就在于区分职责,需要有很详细的领域层说明以及实时的参与领域建模。

运用层代码1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using DDD.Domain;
using DDD.Domain.Arrange;

namespace DDD.Services
{
    public class PlanArrangeService : NDJHService<PlanArrange>, IPlanArrangeService
    {
        private readonly IPlanArrangeRepository repository;
        private readonly ArrangeService service;

        protected PlanArrangeService(IPlanArrangeRepository repository, ArrangeService service)
            : base(repository)
        {
            this.repository = repository;
            this.service = service;
        }

        /// <summary>
        /// 查找可下发的批次列表
        /// </summary>
        /// <param name="xzqdm"></param>
        /// <returns></returns>
        public override IList<BatchModel> FindAllowSendBatches(string xzqdm)
        {
            if (string.IsNullOrEmpty(xzqdm)) throw new ArgumentNullException("xzqdm");
            if (!xzqdm.EndsWith("00")) //如果是县级用户,则不需要判断t.XFXZQDM != t.XZQDM这个条件
            {
                var list = FindBy(0, 0, xzqdm, ProjectStauts.Default).OrderBy(a => a.APPC).ToList();
                var query = from t in list
                            group t by new { t.APPC, t.ND }
                                into temp
                                select new BatchModel
                                {
                                    Batch = temp.Key.APPC,
                                    IsSent = false,
                                    Year = temp.Key.ND
                                };
                return query.ToList();
              
            }
            else
            {
                return base.FindAllowSendBatches(xzqdm);
            }
           
        }

        /// <summary>
        /// 查找计划安排列表
        /// </summary>
        /// <param name="year">年度</param>
        /// <param name="grade">指标级别</param>
        /// <param name="xzqdm">行政区代码</param>
        /// <returns></returns>
        public IList<PlanArrange> FindByDescendants(int year, string xzqdm, IndicatorGrade? grade = null)
        {
            if (string.IsNullOrEmpty(xzqdm)) throw new ArgumentNullException("xzqdm");
            var query = this.repository
                .Where(GetDistrictCodeExpr(xzqdm))
                .Where(grade.HasValue, a => a.ZBJB == grade.Value)
                .Where(year != 0, a => a.ND == year);
            if (xzqdm.EndsWith("0000"))
            {
                //省级用户可以查看国家安排、省级安排、市县级已下发的安排
                if (grade == IndicatorGrade.City || grade == IndicatorGrade.County)
                {
                    query = query.Where(t=>t.Status == ProjectStauts.Sent);
                }
            }
            else if (xzqdm.EndsWith("00"))
            {
                //市级用户可以查看国家、省、县已下发的安排、市级安排
                if (grade != IndicatorGrade.City)
                {
                    query = query.Where(t => t.Status == ProjectStauts.Sent);
                }
            }
            else
            {
                //县级用户可以查看国家、省、市已下发的安排、县级项目
                if (grade != IndicatorGrade.County)
                {
                    query = query.Where(t => t.Status == ProjectStauts.Sent);
                }
            }
            return query.OrderBy(t => t.APPC).ToList();
        }

        /// <summary>
        /// 行政区代码表达式
        /// </summary>
        /// <param name="xzqdm">一般为参数传递的值</param>
        /// <returns></returns>
        private Expression<Func<PlanArrange, bool>> GetDistrictCodeExpr(string xzqdm)
        {
            if (string.IsNullOrEmpty(xzqdm)) throw new ArgumentNullException("xzqdm");
            if (xzqdm.EndsWith("0000"))
            {
                var sub = xzqdm.Substring(0, 2);
                return d => d.XFXZQDM.Contains(sub);
            }
            else if (xzqdm.EndsWith("00"))
            {
                var sub = xzqdm.Substring(0, 4);
                return d => d.XFXZQDM.Contains(sub);
            }
            else
            {
                return d => d.XFXZQDM.Contains(xzqdm);
            }
        }

        public override void Update(PlanArrange entity)
        {
            service.CheckUpdate(entity);
            base.Update(entity);
        }

        public override void Register(PlanArrange entity, IList<PlanArrange> entities)
        {
            if (entity == null) throw new ArgumentNullException("entity");
            Validate(entity);
            Validate(entities);
            service.Register(entity, entities);
            repository.Add(entity);
            foreach (var item in entities)
            {
                repository.Add(item);
            }
        }
    }
}

运用层代码2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using DDD.Domain;
using DDD.Domain.Indicator;

namespace DDD.Services
{
    public class PlanIndicatorService : NDJHService<PlanIndicator>, IPlanIndicatorService
    {
        private readonly IPlanIndicatorRepository repository;
        private readonly IndicatorService service;

        protected PlanIndicatorService(IPlanIndicatorRepository repository, IndicatorService service)
            : base(repository)
        {
            this.repository = repository;
            this.service = service;
        }

        /// <summary>
        /// 查找指标列表(国家下发,省级发,市级下发)
        /// </summary>
        /// <param name="year">年度</param>
        /// <param name="xzqdm">下发行政区代码</param>
        /// <param name="grade">指标级别</param>
        /// <returns></returns>
        public IList<PlanIndicator> FindReceive(int year, string xzqdm)
        {
            if (string.IsNullOrEmpty(xzqdm)) throw new ArgumentNullException("xzqdm");
            return this.repository
                .Where(t => t.XFXZQDM != t.XZQDM)
                .Where(t => t.XFXZQDM == xzqdm)
                .Where(t => t.Status == ProjectStauts.Sent)
                .Where(year != 0, a => a.ND == year)
                .OrderBy(a => a.APPC).ToList();
        }

        /// <summary>
        /// 查找预留指标列表(省级预留,市级预留)
        /// </summary>
        /// <param name="year">年度</param>
        /// <param name="xzqdm">行政区代码</param>
        /// <returns></returns>
        public IList<PlanIndicator> FindReserve(int year, string xzqdm)
        {
            if (string.IsNullOrEmpty(xzqdm)) throw new ArgumentNullException("xzqdm");
            return this.repository
                .Where(a => a.XFXZQDM == a.XZQDM)
                .Where(a => a.XZQDM == xzqdm)
                .Where(year != 0, a => a.ND == year)
                .OrderBy(a => a.APPC).ToList();
        }

        /// <summary>
        /// 查找下发指标列表(下发市级,下发县级)
        /// </summary>
        /// <param name="year">年度</param>
        /// <param name="xzqdm">行政区代码</param>
        /// <returns></returns>
        public IList<PlanIndicator> FindSendTo(int year, string xzqdm)
        {
            if (string.IsNullOrEmpty(xzqdm)) throw new ArgumentNullException("xzqdm");
            return this.repository
                .Where(a => a.XFXZQDM != a.XZQDM)
                .Where(a => a.XZQDM == xzqdm)
                .Where(year != 0, a => a.ND == year)
                .OrderBy(a => a.APPC).ToList();
        }

        /// <summary>
        /// 查找指定行政区下发的指标(预留+下发)
        /// </summary>
        /// <param name="status">状态</param>
        /// <param name="xzqdm">行政区代码</param>
        /// <returns></returns>
        public IList<PlanIndicator> FindSend(string xzqdm, ProjectStauts status)
        {
            if (string.IsNullOrEmpty(xzqdm)) throw new ArgumentNullException("xzqdm");
            return this.repository
                .Where(a => a.XZQDM == xzqdm)
                .Where(t => t.Status == status)
                .OrderBy(a => a.APPC).ToList();
        }

        /// <summary>
        /// 统计预留指标
        /// </summary>
        /// <param name="year"></param>
        /// <param name="xzqdm"></param>
        /// <returns></returns>
        public IndicatorArea TotalReserveIndicator(int year, string xzqdm)
        {
            if (string.IsNullOrEmpty(xzqdm)) throw new ArgumentNullException("xzqdm");
            return repository.TotalReserveIndicator(year, xzqdm);
        }

        /// <summary>
        /// 统计上级下发给下级指标(省-国家下发,市-省级下发,县-市级下发)
        /// </summary>
        /// <param name="year"></param>
        /// <param name="xzqdm"></param>
        /// <returns></returns>
        public IndicatorArea TotalReceiveIndicator(int year, string xzqdm)
        {
            if (string.IsNullOrEmpty(xzqdm)) throw new ArgumentNullException("xzqdm");
            return repository.TotalReceiveIndicator(year, xzqdm);
        }

        /// <summary>
        /// 统计下发给下级的指标(省-市级下发,市-县级下发)
        /// </summary>
        /// <param name="year"></param>
        /// <param name="xzqdm"></param>
        /// <returns></returns>
        public IndicatorArea TotalSendToIndicator(int year, string xzqdm)
        {
            if (string.IsNullOrEmpty(xzqdm)) throw new ArgumentNullException("xzqdm");
            return repository.TotalSendToIndicator(year, xzqdm);
        }

        public override void Update(PlanIndicator entity)
        {
            service.CheckUpdate(entity);
            base.Update(entity);
        }

        public override void Register(PlanIndicator entity, IList<PlanIndicator> entities)
        {
            if (entity == null) throw new ArgumentNullException("entity");
            Validate(entity);
            Validate(entities);
            service.Register(entity, entities);
            repository.Add(entity);
            foreach (var item in entities)
            {
                repository.Add(item);
            }
        }
    }
}

 

你可能感兴趣的:(代码)