forked from anjoy8/ChristDDD
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
v-anzha
committed
Dec 4, 2018
1 parent
79a5809
commit c4c406d
Showing
15 changed files
with
305 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System; | ||
using MediatR; | ||
|
||
namespace Christ3D.Domain.Core.Events | ||
{ | ||
/// <summary> | ||
/// 事件模型 抽象基类,继承 INotification | ||
/// 也就是说,拥有中介者模式中的 发布/订阅模式 | ||
/// </summary> | ||
public abstract class Event : INotification | ||
{ | ||
// 时间戳 | ||
public DateTime Timestamp { get; private set; } | ||
|
||
// 每一个事件都是有状态的 | ||
protected Event() | ||
{ | ||
Timestamp = DateTime.Now; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using Christ3D.Domain.Core.Events; | ||
|
||
namespace Christ3D.Domain.Core.Notifications | ||
{ | ||
/// <summary> | ||
/// 领域通知模型,用来获取当前总线中出现的通知信息 | ||
/// 继承自领域事件和 INotification(也就意味着可以拥有中介的发布/订阅模式) | ||
/// </summary> | ||
public class DomainNotification : Event | ||
{ | ||
// 标识 | ||
public Guid DomainNotificationId { get; private set; } | ||
// 键(可以根据这个key,获取当前key下的全部通知信息) | ||
// 这个我们在事件源和事件回溯的时候会用到,伏笔 | ||
public string Key { get; private set; } | ||
// 值(与key对应) | ||
public string Value { get; private set; } | ||
// 版本信息 | ||
public int Version { get; private set; } | ||
|
||
public DomainNotification(string key, string value) | ||
{ | ||
DomainNotificationId = Guid.NewGuid(); | ||
Version = 1; | ||
Key = key; | ||
Value = value; | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
Christ3D.Domain.Core/Notifications/DomainNotificationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using MediatR; | ||
|
||
namespace Christ3D.Domain.Core.Notifications | ||
{ | ||
/// <summary> | ||
/// 领域通知处理程序,把所有的通知信息放到事件总线中 | ||
/// 继承 INotificationHandler<T> | ||
/// </summary> | ||
public class DomainNotificationHandler : INotificationHandler<DomainNotification> | ||
{ | ||
// 通知信息列表 | ||
private List<DomainNotification> _notifications; | ||
|
||
// 每次访问该处理程序的时候,实例化一个空集合 | ||
public DomainNotificationHandler() | ||
{ | ||
_notifications = new List<DomainNotification>(); | ||
} | ||
|
||
// 处理方法,把全部的通知信息,添加到内存里 | ||
public Task Handle(DomainNotification message, CancellationToken cancellationToken) | ||
{ | ||
_notifications.Add(message); | ||
return Task.CompletedTask; | ||
} | ||
|
||
// 获取当前生命周期内的全部通知信息 | ||
public virtual List<DomainNotification> GetNotifications() | ||
{ | ||
return _notifications; | ||
} | ||
|
||
// 判断在当前总线对象周期中,是否存在通知信息 | ||
public virtual bool HasNotifications() | ||
{ | ||
return GetNotifications().Any(); | ||
} | ||
|
||
// 手动回收(清空通知) | ||
public void Dispose() | ||
{ | ||
_notifications = new List<DomainNotification>(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Christ3D.Domain.Events; | ||
using MediatR; | ||
|
||
namespace Christ3D.Domain.EventHandlers | ||
{ | ||
public class StudentEventHandler : | ||
INotificationHandler<StudentRegisteredEvent>, | ||
INotificationHandler<StudentUpdatedEvent>, | ||
INotificationHandler<StudentRemovedEvent> | ||
{ | ||
// 学习被注册成功后的事件处理方法 | ||
public Task Handle(StudentRegisteredEvent message, CancellationToken cancellationToken) | ||
{ | ||
// 恭喜您,注册成功,欢迎加入我们。 | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
// 学生被修改成功后的事件处理方法 | ||
public Task Handle(StudentUpdatedEvent message, CancellationToken cancellationToken) | ||
{ | ||
// 恭喜您,更新成功,请牢记修改后的信息。 | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
// 学习被删除后的事件处理方法 | ||
public Task Handle(StudentRemovedEvent message, CancellationToken cancellationToken) | ||
{ | ||
// 您已经删除成功啦,记得以后常来看看。 | ||
|
||
return Task.CompletedTask; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using Christ3D.Domain.Core.Events; | ||
|
||
namespace Christ3D.Domain.Events | ||
{ | ||
public class StudentRegisteredEvent : Event | ||
{ | ||
public StudentRegisteredEvent(Guid id, string name, string email, DateTime birthDate, string phone) | ||
{ | ||
Id = id; | ||
Name = name; | ||
Email = email; | ||
BirthDate = birthDate; | ||
Phone = phone; | ||
} | ||
public Guid Id { get; set; } | ||
|
||
public string Name { get; private set; } | ||
|
||
public string Email { get; private set; } | ||
|
||
public DateTime BirthDate { get; private set; } | ||
|
||
public string Phone { get; private set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using Christ3D.Domain.Core.Events; | ||
|
||
namespace Christ3D.Domain.Events | ||
{ | ||
public class StudentRemovedEvent : Event | ||
{ | ||
public StudentRemovedEvent(Guid id) | ||
{ | ||
Id = id; | ||
} | ||
|
||
public Guid Id { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using Christ3D.Domain.Core.Events; | ||
|
||
namespace Christ3D.Domain.Events | ||
{ | ||
public class StudentUpdatedEvent : Event | ||
{ | ||
public StudentUpdatedEvent(Guid id, string name, string email, DateTime birthDate,string phone) | ||
{ | ||
Id = id; | ||
Name = name; | ||
Email = email; | ||
BirthDate = birthDate; | ||
Phone = phone; | ||
} | ||
public Guid Id { get; set; } | ||
|
||
public string Name { get; private set; } | ||
|
||
public string Email { get; private set; } | ||
|
||
public DateTime BirthDate { get; private set; } | ||
|
||
public string Phone { get; private set; } | ||
} | ||
} |
Oops, something went wrong.