「借鉴学习计划」的核心是:复制一份别人的学习计划到自己的计划中,并同步推送学习任务给自己,并且每个操作都要发送通知给对方。

它们的类图如下:

它们的关系是一对多:

// Schedule
entity.HasOne(x => x.Parent).WithMany(x => x.Children).HasForeignKey(x => x.ParentId).OnDelete(DeleteBehavior.Restrict);
entity.HasIndex(nameof(Schedule.UserId), nameof(Schedule.ParentId)).IsUnique().HasFilter($"[{nameof(Schedule.Deleted)}]=0 and [{nameof(Schedule.ParentId)}] is not null");
// ScheduleItem
entity.HasOne(i => i.Schedule).WithMany(s => s.Items).HasForeignKey(i => i.ScheduleId);
entity.HasOne(i => i.Html).WithOne(h => h.Item).HasForeignKey<ScheduleItemHtml>(h => h.ScheduleItemId);
entity.HasOne(x => x.Parent).WithMany(x => x.Children).HasForeignKey(x => x.ParentId).OnDelete(DeleteBehavior.Restrict);
entity.HasIndex(nameof(ScheduleItem.UserId), nameof(ScheduleItem.ParentId)).IsUnique().HasFilter($"[{nameof(ScheduleItem.Deleted)}]=0 and [{nameof(ScheduleItem.ParentId)}] is not null");

按照 DDD 的思路,业务应该发生在领域层中,事件也是从领域中触发的,整个流程的可读性比较强,下面以借鉴功能为例:

    // Domain.Schedule.cs
/* 借鉴 */
public class Schedule : Entity, IAggregateRoot
{
private Schedule()
{
Items = new List<ScheduleItem>();
Children = new List<Schedule>();
} public Schedule(string title, string description, Guid userId, bool isPrivate = false, long? parentId = null) : this()
{
Title = title;
Description = description;
UserId = userId;
IsPrivate = isPrivate;
if (parentId.HasValue)
{
ParentId = parentId;
}
AddDomainEvent(new ScheduleCreatedEvent(UUID));
}
public Schedule Subscribe(Guid userId)
{
if (userId == UserId)
{
throw new ValidationException("不能借鉴自己的计划");
} if (ParentId > 0)
{
throw new ValidationException("很抱歉,暂时不支持借鉴来的学习计划");
}
var child = Deliver(userId);
Children.Add(child);
FollowingCount += 1; AddDomainEvent(new NewSubscriberEvent(this.UUID, child.UUID)); return child;
}
public Schedule Deliver(Guid userId)
{
var schedule = new Schedule(Title, Description, userId, isPrivate: false, Id);
return schedule;
}
}

阅读Subscribe():首先不能借鉴自己的计划,其次不能借鉴借鉴来的计划,Deliver()生产或者说克隆一个Schedule出来,作为当前计划的孩子,然后把借鉴数+1,触发有新的借鉴者事件NewSubscriberEvent

Application作为领域的消费者,就可以直接消费这个领域了。

        // Application.ScheduleAppService.cs
public async Task<long> SubscribeAsync(long id, Guid userId)
{
var schedule = await _repository.Schedules.FirstOrDefaultAsync(s => s.Id == id);
if (schedule != null)
{
try
{
schedule.Subscribe(userId);
await _repository.UnitOfWork.SaveEntitiesAsync();
}
catch (Exception ex) when (ex.InnerException is SqlException sqlerror)
{
if (sqlerror.Number == 2601)
{
throw new ValidationException("已经借鉴过了");
}
}
}
return 0;
}

最后使用 UnitOfWork 工作单元持久化到数据库,并分发领域中产生的事件。

// Infrastructure.DbContext.cs
public async Task<bool> SaveEntitiesAsync(CancellationToken cancellationToken = default(CancellationToken))
{
//https://stackoverflow.com/questions/45804470/the-dbcontext-of-type-cannot-be-pooled-because-it-does-not-have-a-single-public
var bus = this.GetService<ICapPublisher>();
using (var trans = Database.BeginTransaction())
{
if (await SaveChangesAsync(cancellationToken) > 0)
{
await bus.DispatchDomianEventsAsync(this);
trans.Commit();
}
else
{
trans.Rollback();
return false;
}
}
return true;
}

通过 EF Core 的上下文实现了 IUnitOfWork 接口,通过事务保证一致性。这里使用 DotNetCore.CAP 这个优秀的开源产品帮助我们分发事件消息,处理最终一致性。

    public static class CapPublisherExtensions
{
public static async Task<int> DispatchDomianEventsAsync(this ICapPublisher bus, AcademyContext ctx)
{
var domainEntities = ctx.ChangeTracker
.Entries<BaseEntity>()
.Where(x => x.Entity.DomainEvents != null && x.Entity.DomainEvents.Any()); if (domainEntities == null || domainEntities.Count() < 1)
{
return 0;
} var domainEvents = domainEntities
.SelectMany(x => x.Entity.DomainEvents)
.ToList(); domainEntities.ToList()
.ForEach(entity => entity.Entity.ClearDomainEvents()); var tasks = domainEvents
.Select(domainEvent => bus.PublishAsync(domainEvent.GetEventName(), domainEvent)); await Task.WhenAll(tasks);
return domainEvents.Count;
}
}

这里参考了eShopContainer的实现。在触发事件的时候一直都有一个疑问,我们的实体的主键是自增长类型的,只有持久化到数据库之后才知道 Id 的值是多少,但是我们在领域事件中却经常需要这个 Id作为消息的一部分。我解决这个问题的方案,给实体增加一个 GUID 类型的字段UUID,作为唯一身份标识,这样我们就不需要关心最终的Id是多少了,用UUID就可以定位到这个实体了。

事件消息分发出去后,关心这个事件消息的领域就能通过订阅去消费这个事件消息了。

当有新的借鉴者的时候,“消息中心”这个领域关心这个事件,它的MsgService通过DotNetCore.CAP订阅事件消息:

// Msg.AppService.cs
[CapSubscribe(EventConst.NewSubscriber, Group = MsgAppConst.MessageGroup)]
public async Task HandleNewSubscriberEvent(NewSubscriberEvent e)
{
// Notify schedule author
var child = await _repository.FindByUUID<Schedule>(e.ChildScheduleUuid).Include(x => x.Parent).FirstOrDefaultAsync();
if (child == null) return;
var auth = await _uCenter.GetUser(x => x.UserId, child.Parent.UserId);
if (auth == null) return;
var subscriber = await _uCenter.GetUser(x => x.UserId, child.UserId);
if (subscriber == null) return;
var msg = new Notification
{
RecipientId = auth.SpaceUserId,
Title = $"有用户借鉴了您的「{child.Parent.Title}」",
Content = $@"<p>亲爱的 {auth.DisplayName} 同学:</p>
<p>
<b>
<a href='{AppConst.DomainAddress}/schedules/u/{subscriber.Alias}/{child.Id}'>
{subscriber.DisplayName}</a>
</b>
借鉴了您的学习计划
<a href='{AppConst.DomainAddress}/schedules/u/{auth.Alias}/{child.ParentId}'>
「{child.Parent.Title}」
</a>
</p>"
};
await _msgSvc.NotifyAsync(msg);
}

“消息中心”的业务是要给作者发送通知,它负责生产出通知Notification,因为我们团队已经有了基础服务——MsgService,已经实现发送通知的功能,所以只需要调用即可,如果没有的话我们就要自己来实现通过邮件或者短信进行通知。

源代码已托管在 github 上了

DDD 实战记录——实现「借鉴学习计划」的更多相关文章

  1. 【Maven实战技巧】「插件使用专题」Maven-Archetype插件创建自定义maven项目骨架

    技术推荐 自定义Archetype Maven骨架/以当前项目为模板创建maven骨架,可以参考http://maven.apache.org/archetype/maven-archetype-pl ...

  2. 【Maven实战技巧】「插件使用专题」Maven-Assembly插件实现自定义打包

    前提概要 最近我们项目越来越多了,然后我就在想如何才能把基础服务的打包方式统一起来,并且可以实现按照我们的要求来生成,通过研究,我们通过使用maven的assembly插件完美的实现了该需求,爽爆了有 ...

  3. 「快速学习系列」我熬夜整理了Vue3.x响应性API

    前言 Vue3.x正式版发布已经快半年了,相信大家也多多少少也用Vue3.x开发过项目.那么,我们今天就整理下Vue3.x中的响应性API.响应性APIreactive 作用: 创建一个响应式数据. ...

  4. Python(三)基础篇之「模块&面向对象编程」

    [笔记]Python(三)基础篇之「模块&面向对象编程」 2016-12-07 ZOE    编程之魅  Python Notes: ★ 如果你是第一次阅读,推荐先浏览:[重要公告]文章更新. ...

  5. 实战java虚拟机的学习计划图(看懂java虚拟机)

    啥也不说了,实战java虚拟机,好好学习,天天向上!针对自己的软肋制定学习计划. 一部分内容看完,自己做的学习笔记和感想. 学java很简单,但懂java会有难度,如果你的工资还没超过1W,那是时候深 ...

  6. DDD实战课--学习笔记

    目录 学好了DDD,你能做什么? 领域驱动设计:微服务设计为什么要选择DDD? 领域.子域.核心域.通用域和支撑域:傻傻分不清? 限界上下文:定义领域边界的利器 实体和值对象:从领域模型的基础单元看系 ...

  7. [译]聊聊C#中的泛型的使用(新手勿入) Seaching TreeVIew WPF 可编辑树Ztree的使用(包括对后台数据库的增删改查) 字段和属性的区别 C# 遍历Dictionary并修改其中的Value 学习笔记——异步 程序员常说的「哈希表」是个什么鬼?

    [译]聊聊C#中的泛型的使用(新手勿入)   写在前面 今天忙里偷闲在浏览外文的时候看到一篇讲C#中泛型的使用的文章,因此加上本人的理解以及四级没过的英语水平斗胆给大伙进行了翻译,当然在翻译的过程中发 ...

  8. 重学 Java 设计模式:实战备忘录模式「模拟互联网系统上线过程中,配置文件回滚场景」

    作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! 一.前言 实现不了是研发的借口? 实现不了,有时候是功能复杂度较高难以实 ...

  9. 重学 Java 设计模式:实战状态模式「模拟系统营销活动,状态流程审核发布上线场景」

    作者:小傅哥 博客:https://bugstack.cn - 原创系列专题文章 沉淀.分享.成长,让自己和他人都能有所收获! @ 目录 一.前言 二.开发环境 三.状态模式介绍 四.案例场景模拟 1 ...

随机推荐

  1. P1055 ISBN号码

    题目描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括99位数字.11位识别码和33位分隔符,其规定格式如x-xxx-xxxxx-x,其中符号-就是分隔符(键盘上的减号),最后一位 ...

  2. NSSearchPathForDirectoriesInDomains用法

    iPhone会为每一个应用程序生成一个私有目录,这个目录位于: /Users/sundfsun2009/Library/Application Support/iPhone Simulator/Use ...

  3. js的模糊查询

    在项目中会用到模糊查询,之前在首页是用的element的tree显示的目录,会有用到搜索,但tree里边会有自带的模糊查询,用filter-node-method方法使用 但上次的项目中 又涉及到不试 ...

  4. KubeSphere 日志备份与恢复实践

    为什么需要日志备份 KubeSphere 日志系统使用 Fluent Bit + ElasticSearch 的日志采集存储方案,并通过 Curator 实现对 Index 的生命周期管理,定期清理久 ...

  5. oracle监听查看、启动和停止

    oracle监听查看.启动和停止 查看监听lsnrctl status 停止监听lsnrctl stop 启动监听lsnrctl start

  6. Create an Embedded Framework in Xcode with Swift

    转自:http://zappdesigntemplates.com/create-an-embedded-framework-in-xcode-with-swift/ Post Series: Cre ...

  7. OA项目之Mybatis多表链接查询

    xml文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC & ...

  8. Spring源码学习笔记之基于ClassPathXmlApplicationContext进行bean标签解析

    bean 标签在spring的配置文件中, 是非常重要的一个标签, 即便现在boot项目比较流行, 但是还是有必要理解bean标签的解析流程,有助于我们进行 基于注解配置, 也知道各个标签的作用,以及 ...

  9. UVA-1608

    We were afraid of making this problem statement too boring, so we decided to keep it short. A sequen ...

  10. vs code:sync setting 插件

    sync setting 是同步设置插件 第一步:A机器上下载插件 第二步:通过git生成 token user(个人中心) --> Settings --> Developer sett ...