generated from Nexus-Mods/NexusMods.App.Template
-
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.
Big rewrite using datom-like attributes
- Loading branch information
Showing
30 changed files
with
504 additions
and
179 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
src/NexusMods.EventSourcing.Abstractions/ACollectionAttribute.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,17 @@ | ||
using System; | ||
|
||
namespace NexusMods.EventSourcing.Abstractions; | ||
|
||
public class ACollectionAttribute<TOwner, TType>(string name) : IAttribute | ||
where TOwner : IEntity | ||
{ | ||
public bool IsScalar => false; | ||
public Type Owner => typeof(TOwner); | ||
public string Name => name; | ||
public IAccumulator CreateAccumulator() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public Type Type => typeof(TType); | ||
} |
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,12 @@ | ||
namespace NexusMods.EventSourcing.Abstractions; | ||
|
||
/// <summary> | ||
/// The base class for all entities. | ||
/// </summary> | ||
public abstract class AEntity(IEntityContext context, EntityId id) : IEntity | ||
{ | ||
public EntityId Id => id; | ||
|
||
public IEntityContext Context => context; | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
src/NexusMods.EventSourcing.Abstractions/AScalarAttribute.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,43 @@ | ||
using System; | ||
|
||
namespace NexusMods.EventSourcing.Abstractions; | ||
|
||
/// <summary> | ||
/// A scalar attribute that can be exposed on an entity. | ||
/// </summary> | ||
public abstract class AScalarAttribute<TOwner, TType>(string attrName) : IAttribute | ||
{ | ||
/// <inheritdoc /> | ||
public bool IsScalar => false; | ||
|
||
/// <inheritdoc /> | ||
public Type Owner => typeof(TOwner); | ||
|
||
/// <inheritdoc /> | ||
public string Name => attrName; | ||
|
||
/// <inheritdoc /> | ||
public IAccumulator CreateAccumulator() | ||
{ | ||
return new Accumulator<TType>(); | ||
} | ||
|
||
private class Accumulator<TVal> : IAccumulator | ||
{ | ||
private TVal _value = default! ; | ||
public void Add(object value) | ||
{ | ||
_value = (TVal) value; | ||
} | ||
|
||
public object Get() | ||
{ | ||
return _value!; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// The data type of the attribute. | ||
/// </summary> | ||
public Type AttributeType => typeof(TType); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/NexusMods.EventSourcing.Abstractions/AttributeDefinition.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,21 @@ | ||
using System; | ||
|
||
namespace NexusMods.EventSourcing.Abstractions; | ||
|
||
/// <summary> | ||
/// An attribute definition for an entity. | ||
/// </summary> | ||
/// <param name="attrName"></param> | ||
/// <typeparam name="TOwner"></typeparam> | ||
/// <typeparam name="TType"></typeparam> | ||
public class AttributeDefinition<TOwner, TType>(string attrName) : AScalarAttribute<TOwner, TType>(attrName) | ||
where TOwner : IEntity | ||
{ | ||
/// <summary> | ||
/// Gets the value of the attribute for the given entity. | ||
/// </summary> | ||
/// <param name="owner"></param> | ||
/// <returns></returns> | ||
/// <exception cref="NotImplementedException"></exception> | ||
public TType Get(TOwner owner) => (TType)owner.Context.GetAccumulator(owner.Id, this).Get(); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/NexusMods.EventSourcing.Abstractions/EntityAttributeDefinition.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,10 @@ | ||
using System; | ||
|
||
namespace NexusMods.EventSourcing.Abstractions; | ||
|
||
public class EntityAttributeDefinition<TOwner, TType>(string attrName) : AttributeDefinition<TOwner, EntityId<TType>>(attrName) | ||
where TOwner : AEntity | ||
where TType : IEntity | ||
{ | ||
public TType GetEntity(TOwner owner) => throw new NotImplementedException(); | ||
} |
7 changes: 7 additions & 0 deletions
7
src/NexusMods.EventSourcing.Abstractions/EntityCollectionAttributeDefinition.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,7 @@ | ||
namespace NexusMods.EventSourcing.Abstractions; | ||
|
||
public class EntityCollectionAttributeDefinition<TOwner, TEntity>(string name) : ACollectionAttribute<TOwner, TEntity>(name) where TOwner : IEntity | ||
where TEntity : IEntity | ||
{ | ||
|
||
} |
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,19 @@ | ||
namespace NexusMods.EventSourcing.Abstractions; | ||
|
||
/// <summary> | ||
/// An accumulator is used to accumulate values from events. | ||
/// </summary> | ||
public interface IAccumulator | ||
{ | ||
/// <summary> | ||
/// Adds a value to the accumulator. | ||
/// </summary> | ||
/// <param name="value"></param> | ||
public void Add(object value); | ||
|
||
/// <summary> | ||
/// Gets the accumulated value. | ||
/// </summary> | ||
/// <returns></returns> | ||
public object Get(); | ||
} |
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,33 @@ | ||
using System; | ||
|
||
namespace NexusMods.EventSourcing.Abstractions; | ||
|
||
/// <summary> | ||
/// Marker interface for attributes that can be exposed on an entity. | ||
/// </summary> | ||
public interface IAttribute | ||
{ | ||
/// <summary> | ||
/// True if the attribute is a scalar, false if it is a collection. | ||
/// </summary> | ||
public bool IsScalar { get; } | ||
|
||
/// <summary> | ||
/// The data type of the entity that owns the attribute. | ||
/// </summary> | ||
public Type Owner { get; } | ||
|
||
/// <summary> | ||
/// The name of the attribute, needs to be unique in a given entity but not unique across entities. | ||
/// </summary> | ||
public string Name { get; } | ||
|
||
|
||
/// <summary> | ||
/// Creates a new accumulator for the attribute. | ||
/// </summary> | ||
/// <returns></returns> | ||
public IAccumulator CreateAccumulator(); | ||
|
||
} | ||
|
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 |
---|---|---|
@@ -1,7 +1,25 @@ | ||
using System; | ||
|
||
namespace NexusMods.EventSourcing.Abstractions; | ||
|
||
/// <summary> | ||
/// The base interface for all entities. | ||
/// </summary> | ||
public interface IEntity | ||
{ | ||
/// <summary> | ||
/// The globally unique identifier of the entity. | ||
/// </summary> | ||
public EntityId Id { get; } | ||
} | ||
|
||
/// <summary> | ||
/// The context this entity belongs to. | ||
/// </summary> | ||
public IEntityContext Context { get; } | ||
|
||
|
||
/// <summary> | ||
/// The type descriptor for all entities. Emitted by the <see cref="IEventContext.New{TType}"/> method. | ||
/// </summary> | ||
public static readonly AttributeDefinition<IEntity, Type> TypeAttribute = new("$Type"); | ||
} |
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
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,34 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace NexusMods.EventSourcing.Abstractions; | ||
|
||
/// <summary> | ||
/// A interface for a transaction that can be used to add new events to storage. | ||
/// </summary> | ||
public interface ITransaction : IDisposable | ||
{ | ||
/// <summary> | ||
/// Confirms the transaction and commits the changes to the underlying storage. | ||
/// </summary> | ||
/// <returns></returns> | ||
public ValueTask CommitAsync(); | ||
|
||
/// <summary> | ||
/// Gets the current state of an entity. | ||
/// </summary> | ||
/// <param name="entityId"></param> | ||
/// <typeparam name="T"></typeparam> | ||
/// <returns></returns> | ||
public T Retrieve<T>(EntityId<T> entityId) where T : IEntity; | ||
|
||
/// <summary> | ||
/// Adds a new event to the transaction, this will also update the current | ||
/// entity states | ||
/// </summary> | ||
/// <param name="entityId"></param> | ||
/// <typeparam name="T"></typeparam> | ||
/// <returns></returns> | ||
public ValueTask Add<T>(T eventToAdd) where T : IEvent; | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/NexusMods.EventSourcing.Abstractions/MultiEntityAttributeDefinition.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,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace NexusMods.EventSourcing.Abstractions; | ||
|
||
public class MultiEntityAttributeDefinition<TOwner, TType>(string name) : | ||
ACollectionAttribute<TOwner, EntityId<TType>>(name) where TOwner : IEntity | ||
where TType : IEntity | ||
{ | ||
public IEnumerable<TType> GetAll(TOwner owner) => throw new NotImplementedException(); | ||
} |
Oops, something went wrong.