关于具体需求,请看前面的博文:DDD领域驱动设计实践篇之如何提取模型,下面是具体的实体、聚合、值对象的代码,不想多说什么是实体、聚合等概念,相信理论的东西大家已经知晓了。本人对DDD表示好奇,没有在真正项目实践过,甚至也没有看过真正的DDD实践的项目源码,处于极度纠结状态,甚至无法自拔,所以告诫DDD爱好者们,如果要在项目里面实践DDD,除非你对实体建模和领域职责非常了解(很多时候会纠结一些逻辑放哪里好,属于设计问题)以及你的团队水平都比较高认同DDD,否则请慎重。。。勿喷!

代码在后,请先看DEMO结果图

1、聚合的基类,注意,几乎属性都是拼音首字母命名,勿喷哈,不要跑题!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DDD.Infrastructure;
using DDD.Infrastructure.Domain; namespace DDD.Domain
{
/// <summary>
/// 项目基类
/// </summary>
public abstract class ProjectBase : EntityBase, IAggregateRoot
{
protected ProjectBase()
{
this.ND = DateTime.Now.Year;
this.CJSJ = DateTime.Now;
this.WH = new DocumentNumber();
} /// <summary>
/// 安排批次
/// </summary>
public int APPC { get; set; }
/// <summary>
/// 项目名称
/// </summary>
public string XMMC { get; set; }
/// <summary>
/// 项目编号
/// </summary>
public string XMBH { get; internal set; }
/// <summary>
/// 年度
/// </summary>
public int ND { get; set; }
/// <summary>
/// 文号
/// </summary>
public DocumentNumber WH { get; set; }
/// <summary>
/// 创建时间
/// </summary>
public DateTime CJSJ { get; set; }
/// <summary>
/// 下发行政区名称
/// </summary>
public string XFXZQMC { get; set; }
/// <summary>
/// 下发行政区代码
/// </summary>
public string XFXZQDM { get; set; }
/// <summary>
/// 行政区名称
/// </summary>
public string XZQMC { get; set; }
/// <summary>
/// 行政区代码
/// </summary>
public string XZQDM { get; set; }
/// <summary>
/// 备注
/// </summary>
public string BZ { get; set; }
/// <summary>
/// 指标级别
/// </summary>
public IndicatorGrade ZBJB { get; set; }
/// <summary>
/// 附件id
/// </summary>
public decimal ATTACHID { get; set; }
/// <summary>
/// 项目状态
/// </summary>
public ProjectStauts Status { get; set; } /// <summary>
/// 业务代码
/// </summary>
protected abstract string BussinessCode { get; } /// <summary>
/// 登记
/// </summary>
/// <param name="seq"></param>
public virtual void Register()
{
this.XMBH = this.BussinessCode + SeqGeneratr.Generate();
} /// <summary>
/// 是否可以更新或者删除
/// </summary>
/// <returns></returns>
public virtual bool CanUpdate()
{
return this.ZBJB == IndicatorGrade.Country || this.XFXZQDM == this.XZQDM || this.Status == ProjectStauts.Default;
} public void Send()
{
this.Status = ProjectStauts.Sent;
}
}
}

2、聚合1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DDD.Infrastructure;
using DDD.Infrastructure.Domain; namespace DDD.Domain.Indicator
{
/// <summary>
/// 计划指标
/// </summary>
public class PlanIndicator : ProjectBase
{
public PlanIndicator()
{
IndicatorArea = new IndicatorArea();
} protected override string BussinessCode
{
get { return "103101"; }
} /// <summary>
/// 指标面积
/// </summary>
public IndicatorArea IndicatorArea
{
get;
set;
} public override IEnumerable<BusinessRule> Validate()
{
if (this.IndicatorArea.GD > this.IndicatorArea.NYD)
{
yield return new BusinessRule("IndicatorArea.GD", "耕地面积不能大于农用地面积");
}
} public override void Register()
{
if (this.ZBJB == IndicatorGrade.Country)
{
this.Send();
}
base.Register();
}
}
}

3、聚合2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DDD.Infrastructure;
using DDD.Infrastructure.Domain; namespace DDD.Domain.Arrange
{
/// <summary>
/// 计划安排
/// </summary>
public class PlanArrange : ProjectBase
{
public PlanArrange()
{
JHSY = new IndicatorArea();
SJSY = new IndicatorArea();
} protected override string BussinessCode
{
get { return "103102"; }
} /// <summary>
/// 计划使用面积
/// </summary>
public IndicatorArea JHSY
{
get;
set;
} /// <summary>
/// 实际使用面积
/// </summary>
public IndicatorArea SJSY
{
get;
set;
} /// <summary>
/// 剩余面积
/// </summary>
public IndicatorArea SY
{
get
{
return JHSY - SJSY;
}
} /// <summary>
/// 用地类别
/// </summary>
public string XMYDLB { get; set; } public override IEnumerable<BusinessRule> Validate()
{
if (this.JHSY.GD > this.JHSY.NYD)
{
yield return new BusinessRule("JHSY.GD", "计划使用中,耕地面积不能大于农用地面积");
}
if (this.SJSY.GD > this.SJSY.NYD)
{
yield return new BusinessRule("SJSY.GD", "实际使用中,耕地面积不能大于农用地面积");
}
if (string.IsNullOrEmpty(this.XMYDLB))
{
yield return new BusinessRule("XMYDLB", "项目用地类别不允许为空");
}
}
}
}

4、值对象1

using System;
using DDD.Infrastructure.Domain; namespace DDD.Domain
{
/// <summary>
/// 文号
/// </summary>
public class DocumentNumber : ValueObject<DocumentNumber>, ICloneable
{
public static readonly string LeftYearChar = "〔";
public static readonly string RightYearChar = "〕"; public DocumentNumber()
{ } public DocumentNumber(string wh)
{
try
{
this.Code = wh.Substring(0, wh.IndexOf(LeftYearChar));
this.Year = wh.Substring(wh.IndexOf(LeftYearChar), wh.IndexOf(RightYearChar) - this.Code.Length + 1);
this.Order = wh.Replace(this.Code + this.Year, "");
this.Year = this.Year.Replace(LeftYearChar, "").Replace(RightYearChar, "");
}
catch(Exception ex)
{
throw new InvalidCastException("文号格式不正确", ex);
}
} /// <summary>
/// 发文机关代字
/// </summary>
public string Code { get; set; }
/// <summary>
/// 年份
/// </summary>
public string Year { get; set; }
private string order;
/// <summary>
/// 顺序号
/// </summary>
public string Order
{
get
{
if (!string.IsNullOrEmpty(order) && !order.Contains("号"))
{
order += "号";
}
return order;
}
set
{
order = value;
}
} public override string ToString()
{
if (string.IsNullOrEmpty(Code) && string.IsNullOrEmpty(Year) && string.IsNullOrEmpty(order))
{
return string.Empty;
}
return this.Code + LeftYearChar + Year + RightYearChar + Order;
} public static implicit operator DocumentNumber(string wh)
{
return new DocumentNumber(wh);
} public object Clone()
{
return this.MemberwiseClone();
}
}
}

5、值对象2

using DDD.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DDD.Infrastructure.Domain; namespace DDD.Domain
{
/// <summary>
/// 指标面积
/// </summary>
public class IndicatorArea : ValueObject<IndicatorArea>
{
/// <summary>
/// 新增建设用地
/// </summary>
public decimal XZJSYD
{
get
{
return NYD + WLYD;
}
}
/// <summary>
/// 农用地
/// </summary>
public decimal NYD { get; set; }
/// <summary>
/// 耕地
/// </summary>
public decimal GD { get; set; }
/// <summary>
/// 未利用地
/// </summary>
public decimal WLYD { get; set; } /// <summary>
/// 将公顷转换成亩
/// </summary>
/// <returns></returns>
public IndicatorArea HectareToMu()
{
return new IndicatorArea
{
GD = this.GD * 15,
NYD = this.NYD * 15,
WLYD = this.WLYD * 15,
};
} /// <summary>
/// 重载加法运算符
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static IndicatorArea operator +(IndicatorArea a, IndicatorArea b)
{
return new IndicatorArea
{
GD = a.GD + b.GD,
NYD = a.NYD + b.NYD,
WLYD = a.WLYD + b.WLYD,
};
} /// <summary>
/// 重载减法运算符
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static IndicatorArea operator -(IndicatorArea a, IndicatorArea b)
{
return new IndicatorArea
{
GD = a.GD - b.GD,
NYD = a.NYD - b.NYD,
WLYD = a.WLYD - b.WLYD,
};
} public static IndicatorArea Sum(IEnumerable<IndicatorArea> query)
{
return new IndicatorArea
{
GD = query.Sum(t => t.GD),
NYD = query.Sum(t => t.NYD),
WLYD = query.Sum(t => t.WLYD),
};
}
}
}

  

6、枚举

using System.ComponentModel;

namespace DDD.Domain
{
/// <summary>
/// 指标级别
/// </summary>
public enum IndicatorGrade
{
/// <summary>
/// 国家
/// </summary>
[Description("国家")]
Country,
/// <summary>
/// 省级
/// </summary>
[Description("省级")]
Province,
/// <summary>
/// 市级
/// </summary>
[Description("市级")]
City,
/// <summary>
/// 县级
/// </summary>
[Description("县级")]
County,
}
}
using System.ComponentModel;

namespace DDD.Domain
{
/// <summary>
/// 项目状态
/// </summary>
public enum ProjectStauts
{
/// <summary>
/// 默认状态,已登记
/// </summary>
Default,
/// <summary>
/// 已下发
/// </summary>
Sent,
}
}

  

  

DDD领域驱动设计之聚合、实体、值对象的更多相关文章

  1. DDD领域驱动设计之领域服务

    1.DDD领域驱动设计实践篇之如何提取模型 2.DDD领域驱动设计之聚合.实体.值对象 3.DDD领域驱动设计之领域基础设施层 什么是领域服务,DDD书中是说,有些类或者方法,放实体A也不好,放实体B ...

  2. DDD领域驱动设计之领域基础设施层

    1.DDD领域驱动设计实践篇之如何提取模型 2.DDD领域驱动设计之聚合.实体.值对象 其实这里说的基础设施层只是领域层的一些接口和基类而已,没有其他的如日子工具等代码,仅仅是为了说明领域层的一些基础 ...

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

    1.DDD领域驱动设计实践篇之如何提取模型 2.DDD领域驱动设计之聚合.实体.值对象 3.DDD领域驱动设计之领域基础设施层 4.DDD领域驱动设计之领域服务 5.整体DEMO代码 什么是运用层,说 ...

  4. DDD 领域驱动设计-三个问题思考实体和值对象(续)

    上一篇:DDD 领域驱动设计-三个问题思考实体和值对象 说实话,整理现在这一篇博文的想法,在上一篇发布出来的时候就有了,但到现在才动起笔来,而且写之前又反复读了上一篇博文的内容及评论,然后去收集资料, ...

  5. DDD 领域驱动设计-三个问题思考实体和值对象

    消息场景:用户 A 发送一个消息给用户 B,用户 B 回复一个消息给用户 A... 现有设计:消息设计为实体并为聚合根,发件人.收件人设计为值对象. 三个问题: 实体最重要的特性是什么? Messag ...

  6. DDD 领域驱动设计-“臆想”中的实体和值对象

    其他博文: DDD 领域驱动设计-三个问题思考实体和值对象 DDD 领域驱动设计-三个问题思考实体和值对象(续) 以下内容属于博主"臆想",如有不当,请别当真. 扯淡开始: 诺兰的 ...

  7. C#进阶系列——DDD领域驱动设计初探(一):聚合

    前言:又有差不多半个月没写点什么了,感觉这样很对不起自己似的.今天看到一篇博文里面写道:越是忙人越有时间写博客.呵呵,似乎有点道理,博主为了证明自己也是忙人,这不就来学习下DDD这么一个听上去高大上的 ...

  8. DDD领域驱动设计初探(一):聚合

    前言:又有差不多半个月没写点什么了,感觉这样很对不起自己似的.今天看到一篇博文里面写道:越是忙人越有时间写博客.呵呵,似乎有点道理,博主为了证明自己也是忙人,这不就来学习下DDD这么一个听上去高大上的 ...

  9. DDD 领域驱动设计-两个实体的碰撞火花

    上一篇:<DDD 领域驱动设计-领域模型中的用户设计?> 开源地址:https://github.com/yuezhongxin/CNBlogs.Apply.Sample(代码已更新) 在 ...

随机推荐

  1. Spring.Scheduling.Quartz 作业的应用(定时任务和循环触发任务)

    .定时任务的实现,比如有个任务是要晚上2点10分的时候要去触发的,先定义这个任务类RskBookFilterInitDiningService.cs,这里其实有两种实现,一种是需要继承QuartzJo ...

  2. photoshopcc基础教程

    web项目中,除了最基础的用java存取数据外,还有重要的h5+css排版以及图片的ps,排版多多看网上人家的好看的界面设计,至于图片,只能自己上手了,设计最终的目的是好看,好看,好看. 接下来,做个 ...

  3. python day1 变量的命名和赋值

    变量 一.变量的命名 1.不能以数字进行开头 2.不能包含特殊字符 3.不能是python内部的某些关键字 a = 123print(a)123 --------------------------- ...

  4. Android学习 之 ColorStateList按钮文字变色

    首先添加一个ColorStateList资源XML文件,XML文件保存在res/color/button_text.xml: <?xml version="1.0" enco ...

  5. iOS基础之Xcode 8相关

    1.屏蔽日志输出 2.注释相关 注释不能使用:命令运行:  sudo /usr/libexec/xpccachectl  VVDocument方式注释快捷键:option + command + /

  6. 计算机网路之动态NAT配置

    配置路由端口的ip地址与打开(省略) 配置路由协议 router eigrp 100 network 211.1.1.0(网络号) 0.0.0.255(通配子掩) network 192.168.1. ...

  7. AOP实现原理

    Spring 为解耦而生,其中AOP(面向切面编程)是很浓重的一笔. 本文来探讨一下AOP实现的原理. 一. 概述 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负 ...

  8. DOS tasklist 命令(转)

    Dos命令之Tasklist用法及参数函义 2012-10-24 14:44:34|  分类: Windows |字号 订阅   TASKLIST [/S system [/U username [/ ...

  9. WWDC2016的一点个人想法

    看了游戏审核的新闻和WWDC,感觉个人游戏开发者会很难混下去了,WWDC里面iMessage 透露出来的信息,我感觉微信有竞争者了,假如苹果把iMessage 打造成微信那种模式(聊天+第三方接入), ...

  10. 更改Xampp-sql的默认密码-配置appche运行环境

    用php编写的web应用程序,需运行在php的web容器中,其中apache server是一个针对php web容器,它是apache下的开源项目.通常要运行一个web程序,我们还需要安装数据库软件 ...