csharp 事件源聚合策略样本
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 事件源聚合策略样本相关的知识,希望对你有一定的参考价值。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace mergePolicy {
internal class PropertyValueChangedEvent {
public string Guid;
public string EventType= "";
public string PropertyName="";
public string PropertyValue="";
}
class Program {
static void Main(string[] args) {
var events = new List<PropertyValueChangedEvent> {
new PropertyValueChangedEvent{ Guid = "Guid1", EventType = "PropertyValueChanged", PropertyName = "Title", PropertyValue = "Titile1"},
new PropertyValueChangedEvent{ Guid = "Guid1", EventType = "PropertyValueChanged", PropertyName = "Text", PropertyValue = "Text1"},
new PropertyValueChangedEvent{ Guid = "Guid1", EventType = "PropertyValueChanged", PropertyName = "Description", PropertyValue = "Description1"},
new PropertyValueChangedEvent{ Guid = "Guid1", EventType = "Commit"},
new PropertyValueChangedEvent{ Guid = "Guid2", EventType = "PropertyValueChanged", PropertyName = "Title", PropertyValue = "Title2"},
new PropertyValueChangedEvent{ Guid = "Guid2", EventType = "PropertyValueChanged", PropertyName = "Text", PropertyValue = "Text2"},
new PropertyValueChangedEvent{ Guid = "Guid2", EventType = "Commit"},
};
Print("LastCommitSampleAggregation result", LastCommitSampleAggregation(events));
Print("FirstCommitSampleAggregation result", FirstCommitSampleAggregation(events));
Print("MergeCommitSampleAggregation result", MergeCommitSampleAggregation(events));
Console.ReadKey();
}
private static Dictionary<string, string> LastCommitSampleAggregation(IEnumerable<PropertyValueChangedEvent> events) {
var aggregationResults = new Dictionary<string, Dictionary<string, string>>();
var resultGuid = "";
foreach (var valueChangedEvent in events) {
if (!aggregationResults.ContainsKey(valueChangedEvent.Guid))
aggregationResults.Add(valueChangedEvent.Guid, new Dictionary<string, string>());
if (valueChangedEvent.EventType == "PropertyValueChanged")
aggregationResults[valueChangedEvent.Guid][valueChangedEvent.PropertyName] = valueChangedEvent.PropertyValue;
if (valueChangedEvent.EventType == "Commit") {
resultGuid = valueChangedEvent.Guid;
}
}
return aggregationResults[resultGuid];
}
private static Dictionary<string, string> FirstCommitSampleAggregation(IEnumerable<PropertyValueChangedEvent> events) {
var aggregationResults = new Dictionary<string, Dictionary<string, string>>();
var resultGuid = "";
foreach (var valueChangedEvent in events) {
if (!aggregationResults.ContainsKey(valueChangedEvent.Guid))
aggregationResults.Add(valueChangedEvent.Guid, new Dictionary<string, string>());
if (valueChangedEvent.EventType == "PropertyValueChanged")
aggregationResults[valueChangedEvent.Guid][valueChangedEvent.PropertyName] = valueChangedEvent.PropertyValue;
if (valueChangedEvent.EventType == "Commit" && resultGuid == "") {
resultGuid = valueChangedEvent.Guid;
}
}
return aggregationResults[resultGuid];
}
private static Dictionary<string, string> MergeCommitSampleAggregation(IEnumerable<PropertyValueChangedEvent> events) {
var aggregationResults = new Dictionary<string, Dictionary<string, string>>();
var aggregationResult = new Dictionary<string, string>();
foreach (var valueChangedEvent in events) {
if (!aggregationResults.ContainsKey(valueChangedEvent.Guid))
aggregationResults.Add(valueChangedEvent.Guid, new Dictionary<string, string>());
if (valueChangedEvent.EventType == "PropertyValueChanged") {
aggregationResults[valueChangedEvent.Guid][valueChangedEvent.PropertyName] = valueChangedEvent.PropertyValue;
if (aggregationResult.Count > 0) {
aggregationResult[valueChangedEvent.PropertyName] = valueChangedEvent.PropertyValue;
}
}
if (valueChangedEvent.EventType == "Commit" && aggregationResult.Count == 0) {
aggregationResult = aggregationResults[valueChangedEvent.Guid];
}
}
return aggregationResult;
}
public static void Print(string message, IDictionary<string, string> dict) {
Console.WriteLine(message);
foreach (var kv in dict) {
Console.WriteLine("{0}: {1}", kv.Key, kv.Value);
}
Console.WriteLine("=====================\r\n");
}
}
}
以上是关于csharp 事件源聚合策略样本的主要内容,如果未能解决你的问题,请参考以下文章
可以使用 Apache Kafka“无限保留策略”作为具有 CQRS 的事件源系统的基础吗?
004-spring-data-elasticsearch 3.0.0.0使用-spring-data之定义方法创建repository实例从聚合根发布事件