Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
refactor(shriek): 移除AggregateRoot类
Browse files Browse the repository at this point in the history
  • Loading branch information
ElderJames committed Feb 16, 2018
1 parent 633a232 commit 9e25765
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/Shriek.EventStorage.Redis/RedisEventStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public void Save(Event @event)
}

public void SaveAggregateRoot<TAggregateRoot>(TAggregateRoot aggregate)
where TAggregateRoot : AggregateRoot
where TAggregateRoot : IAggregateRoot
{
var uncommittedChanges = aggregate.GetUncommittedChanges();
var version = aggregate.Version;
Expand All @@ -112,7 +112,7 @@ public void SaveAggregateRoot<TAggregateRoot>(TAggregateRoot aggregate)
}

public TAggregateRoot Source<TAggregateRoot, TKey>(TKey aggregateId)
where TAggregateRoot : AggregateRoot, new()
where TAggregateRoot : IAggregateRoot<TKey>, new()
where TKey : IEquatable<TKey>
{
IEnumerable<Event> events;
Expand Down
18 changes: 9 additions & 9 deletions src/Shriek/Commands/DefaultCommandContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Shriek.Commands
{
public class DefaultCommandContext : ICommandContext, ICommandContextSave
{
private readonly ConcurrentQueue<AggregateRoot> aggregates;
private readonly ConcurrentQueue<IAggregateRoot> aggregates;
private static readonly object Lock = new object();
private readonly IEventBus eventBus;
private readonly IEventStorage eventStorage;
Expand All @@ -20,7 +20,7 @@ public DefaultCommandContext(IServiceProvider container)
{
eventStorage = container.GetService<IEventStorage>();
eventBus = container.GetService<IEventBus>();
aggregates = new ConcurrentQueue<AggregateRoot>();
aggregates = new ConcurrentQueue<IAggregateRoot>();
}

public IDictionary<string, object> Items => new Dictionary<string, object>();
Expand All @@ -34,10 +34,10 @@ public DefaultCommandContext(IServiceProvider container)
/// <param name="initFromRepository"></param>
/// <returns></returns>
public TAggregateRoot GetAggregateRoot<TKey, TAggregateRoot>(TKey key, Func<TAggregateRoot> initFromRepository)
where TAggregateRoot : AggregateRoot, new()
where TAggregateRoot : IAggregateRoot<TKey>, new()
where TKey : IEquatable<TKey>
{
var obj = GetById<TAggregateRoot, TKey>(key);
var obj = GetById<TKey, TAggregateRoot>(key);
if (obj == null)
obj = initFromRepository();

Expand All @@ -47,8 +47,8 @@ public TAggregateRoot GetAggregateRoot<TKey, TAggregateRoot>(TKey key, Func<TAgg
return obj;
}

private TAggregateRoot GetById<TAggregateRoot, TKey>(TKey id)
where TAggregateRoot : AggregateRoot, new()
private TAggregateRoot GetById<TKey, TAggregateRoot>(TKey id)
where TAggregateRoot : IAggregateRoot<TKey>, new()
where TKey : IEquatable<TKey>
{
return eventStorage.Source<TAggregateRoot, TKey>(id);
Expand All @@ -66,7 +66,7 @@ public void Save()
}

public void SaveAggregateRoot<TAggregateRoot>(TAggregateRoot aggregate)
where TAggregateRoot : AggregateRoot
where TAggregateRoot : IAggregateRoot
{
if (aggregate.GetUncommittedChanges().Any())
{
Expand Down Expand Up @@ -95,10 +95,10 @@ public void SaveAggregateRoot<TAggregateRoot>(TAggregateRoot aggregate)
}

public TAggregateRoot GetAggregateRoot<TKey, TAggregateRoot>(TKey key)
where TAggregateRoot : AggregateRoot, new()
where TAggregateRoot : IAggregateRoot<TKey>, new()
where TKey : IEquatable<TKey>
{
var obj = GetById<TAggregateRoot, TKey>(key);
var obj = GetById<TKey, TAggregateRoot>(key);
if (obj != null)
aggregates.Enqueue(obj);

Expand Down
4 changes: 2 additions & 2 deletions src/Shriek/Commands/ICommandContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ public interface ICommandContext
IDictionary<string, object> Items { get; }

TAggregateRoot GetAggregateRoot<TKey, TAggregateRoot>(TKey key, Func<TAggregateRoot> initFromRepository)
where TAggregateRoot : AggregateRoot, new()
where TAggregateRoot : IAggregateRoot<TKey>, new()
where TKey : IEquatable<TKey>;

TAggregateRoot GetAggregateRoot<TKey, TAggregateRoot>(TKey key)
where TAggregateRoot : AggregateRoot, new()
where TAggregateRoot : IAggregateRoot<TKey>, new()
where TKey : IEquatable<TKey>;
}
}
80 changes: 58 additions & 22 deletions src/Shriek/Domains/AggregateRoot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@

namespace Shriek.Domains
{
public abstract class AggregateRoot<TKey> : AggregateRoot, IAggregateRoot<TKey> where TKey : IEquatable<TKey>
public abstract class AggregateRoot<TKey> : IAggregateRoot<TKey>
where TKey : IEquatable<TKey>
{
[Key]
public int Id { get; protected set; }

public int Version { get; protected set; } = -1;

protected List<Event> Changes { get; }


public TKey AggregateId { get; protected set; }

protected AggregateRoot() : this(default(TKey))
Expand All @@ -19,6 +28,7 @@ protected AggregateRoot() : this(default(TKey))

protected AggregateRoot(TKey aggregateId)
{
this.Changes = new List<Event>();
AggregateId = aggregateId;
}

Expand Down Expand Up @@ -58,7 +68,7 @@ public override string ToString()
return GetType().Name + " [Id=" + AggregateId + "]";
}

public override void LoadsFromHistory(IEnumerable<Event> history)
public void LoadsFromHistory(IEnumerable<Event> history)
{
foreach (var e in history)
{
Expand All @@ -82,29 +92,10 @@ protected void ApplyChange(Event @event, bool isNew)
}
}

public override Memento GetMemento()
public Memento GetMemento()
{
return new Memento() { AggregateId = AggregateId.ToString(), Data = JsonConvert.SerializeObject(this), Version = 0 };
}
}

public abstract class AggregateRoot : IOriginator, IEventProvider
{
[Key]
public int Id { get; protected set; }

public int Version { get; protected set; } = -1;

protected List<Event> Changes { get; }

protected AggregateRoot()
{
this.Changes = new List<Event>();
}

public abstract Memento GetMemento();

public abstract void LoadsFromHistory(IEnumerable<Event> history);

public IEnumerable<Event> GetUncommittedChanges()
{
Expand Down Expand Up @@ -132,4 +123,49 @@ public void SetMemento(Memento memento)

public bool CanCommit => this.Changes.Any();
}

//public abstract class AggregateRoot : IOriginator, IEventProvider
//{
// [Key]
// public int Id { get; protected set; }

// public int Version { get; protected set; } = -1;

// protected List<Event> Changes { get; }

// protected AggregateRoot()
// {
// this.Changes = new List<Event>();
// }

// public abstract Memento GetMemento();

// public abstract void LoadsFromHistory(IEnumerable<Event> history);

// public IEnumerable<Event> GetUncommittedChanges()
// {
// return Changes;
// }

// public void MarkChangesAsCommitted()
// {
// Changes.Clear();
// }

// public void SetMemento(Memento memento)
// {
// var data = JObject.Parse(memento.Data);
// foreach (var t in data)
// {
// var prop = GetType().GetProperty(t.Key);
// if (prop == null || !prop.CanWrite)
// continue;

// var value = t.Value.ToObject(prop.PropertyType);
// prop.SetValue(this, value);
// }
// }

// public bool CanCommit => this.Changes.Any();
//}
}
14 changes: 10 additions & 4 deletions src/Shriek/Domains/IAggregateRoot.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
using System;
using Shriek.Storage.Mementos;
using System;

namespace Shriek.Domains
{
public interface IAggregateRoot<out TKey> where TKey : IEquatable<TKey>
public interface IAggregateRoot<out TKey> : IAggregateRoot
where TKey : IEquatable<TKey>
{
TKey AggregateId { get; }
}

int Version { get; }

public interface IAggregateRoot:IOriginator, IEventProvider
{
bool CanCommit { get; }


int Version { get; }
}
}
4 changes: 2 additions & 2 deletions src/Shriek/Storage/IEventStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IEventStorage
{
IEnumerable<Event> GetEvents<TKey>(TKey aggregateId, int afterVersion = 0) where TKey : IEquatable<TKey>;

void SaveAggregateRoot<TAggregateRoot>(TAggregateRoot aggregate) where TAggregateRoot : AggregateRoot;
void SaveAggregateRoot<TAggregateRoot>(TAggregateRoot aggregate) where TAggregateRoot : IAggregateRoot;

/// <summary>
/// 事件溯源,获取聚合根
Expand All @@ -18,7 +18,7 @@ public interface IEventStorage
/// /// <typeparam name="TKey">聚合根Id类型</typeparam>
/// <param name="aggregateId">唯一标识</param>
TAggregateRoot Source<TAggregateRoot, TKey>(TKey aggregateId)
where TAggregateRoot : AggregateRoot, new()
where TAggregateRoot : IAggregateRoot<TKey>, new()
where TKey : IEquatable<TKey>;

IEvent<TKey> GetLastEvent<TKey>(TKey aggregateId) where TKey : IEquatable<TKey>;
Expand Down
5 changes: 3 additions & 2 deletions src/Shriek/Storage/InMemoryEventStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public IEvent<TKey> GetLastEvent<TKey>(TKey aggregateId) where TKey : IEquatable
.OrderBy(e => e.Version).LastOrDefault();
}

public void SaveAggregateRoot<TAggregateRoot>(TAggregateRoot aggregate) where TAggregateRoot : AggregateRoot
public void SaveAggregateRoot<TAggregateRoot>(TAggregateRoot aggregate)
where TAggregateRoot : IAggregateRoot
{
var uncommittedChanges = aggregate.GetUncommittedChanges();
var version = aggregate.Version;
Expand Down Expand Up @@ -76,7 +77,7 @@ public Memento GetMemento<TKey>(TKey aggregateId) where TKey : IEquatable<TKey>
}

public TAggregateRoot Source<TAggregateRoot, TKey>(TKey aggregateId)
where TAggregateRoot : AggregateRoot, new()
where TAggregateRoot : IAggregateRoot<TKey>, new()
where TKey : IEquatable<TKey>
{
//获取该记录的所有缓存事件
Expand Down
5 changes: 3 additions & 2 deletions src/Shriek/Storage/SqlEventStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public void Save(Event theEvent)
eventStoreRepository.Store(storedEvent);
}

public void SaveAggregateRoot<TAggregateRoot>(TAggregateRoot aggregate) where TAggregateRoot : AggregateRoot
public void SaveAggregateRoot<TAggregateRoot>(TAggregateRoot aggregate)
where TAggregateRoot : IAggregateRoot
{
var uncommittedChanges = aggregate.GetUncommittedChanges();
var version = aggregate.Version;
Expand All @@ -91,7 +92,7 @@ public void SaveAggregateRoot<TAggregateRoot>(TAggregateRoot aggregate) where TA
}

public TAggregateRoot Source<TAggregateRoot, TKey>(TKey aggregateId)
where TAggregateRoot : AggregateRoot, new()
where TAggregateRoot : IAggregateRoot<TKey>, new()
where TKey : IEquatable<TKey>
{
IEnumerable<Event> events;
Expand Down

0 comments on commit 9e25765

Please sign in to comment.