Skip to content

Commit

Permalink
Merge pull request #37 from neuroglia-io/fix-snapshot-serialization
Browse files Browse the repository at this point in the history
Removed the non-generic Snapshot abstract class to avoid clashes during serialization (i.e. duplicated data property)
  • Loading branch information
JBBianchi authored Nov 14, 2022
2 parents 64440c1 + b405446 commit d4c8256
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 95 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 19 additions & 59 deletions src/Data/Neuroglia.Data.EventSourcing/Snapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ namespace Neuroglia.Data.EventSourcing
/// <summary>
/// Represents the snapshot envelope of an <see cref="IAggregateRoot"/>
/// </summary>
public abstract class Snapshot
: ISnapshot
/// <typeparam name="TAggregate">The type of the snapshot <see cref="IAggregateRoot"/></typeparam>
public class Snapshot<TAggregate>
: ISnapshot<TAggregate>
where TAggregate : class, IAggregateRoot
{

/// <summary>
Expand All @@ -38,7 +40,7 @@ protected Snapshot()
/// </summary>
/// <param name="data">The <see cref="IAggregateRoot"/> to snapshot</param>
/// <param name="metadata">The metadata of the <see cref="Snapshot"/> to create</param>
protected Snapshot(IAggregateRoot data, object metadata)
public Snapshot(TAggregate data, object metadata)
{
this.Version = data.StateVersion;
this.Data = data;
Expand All @@ -49,82 +51,40 @@ protected Snapshot(IAggregateRoot data, object metadata)
/// Initializes a new <see cref="Snapshot{TKey}"/>
/// </summary>
/// <param name="data">The <see cref="IAggregateRoot"/> to snapshot</param>
protected Snapshot(IAggregateRoot data)
public Snapshot(TAggregate data)
: this(data, null)
{

}

/// <inheritdoc/>
public virtual long Version { get; protected set; }
public virtual TAggregate Data { get; protected set; }

IAggregateRoot ISnapshot.Data => this.Data;

/// <inheritdoc/>
public virtual IAggregateRoot Data { get; protected set; }
public virtual long Version { get; protected set; }

/// <inheritdoc/>
public virtual object Metadata { get; protected set; }

/// <summary>
/// Creates a new <see cref="Snapshot{TAggregate}"/> for the specified <see cref="IAggregateRoot"/>
/// </summary>
/// <param name="aggregate">The <see cref="IAggregateRoot"/> to create a new <see cref="Snapshot{TAggregate}"/> for</param>
/// <returns>A new <see cref="Snapshot{TAggregate}"/> of the specified <see cref="IAggregateRoot"/></returns>
public static Snapshot<TAggregate> CreateFor<TAggregate>(TAggregate aggregate)
where TAggregate : class, IAggregateRoot
{
return new(aggregate);
}

}

/// <summary>
/// Represents the snapshot envelope of an <see cref="IAggregateRoot"/>
/// Defines helpers methods to handle <see cref="ISnapshot"/>s
/// </summary>
/// <typeparam name="TAggregate">The type of the snapshot <see cref="IAggregateRoot"/></typeparam>
public class Snapshot<TAggregate>
: Snapshot, ISnapshot<TAggregate>
where TAggregate : class, IAggregateRoot
public static class Snapshot
{

/// <summary>
/// Initializes a new <see cref="Snapshot{TKey}"/>
/// </summary>
protected Snapshot()
{

}

/// <summary>
/// Initializes a new <see cref="Snapshot{TKey}"/>
/// </summary>
/// <param name="data">The <see cref="IAggregateRoot"/> to snapshot</param>
/// <param name="metadata">The metadata of the <see cref="Snapshot"/> to create</param>
public Snapshot(TAggregate data, object metadata)
: base(data, metadata)
{

}

/// <summary>
/// Initializes a new <see cref="Snapshot{TKey}"/>
/// Creates a new <see cref="Snapshot{TAggregate}"/> for the specified <see cref="IAggregateRoot"/>
/// </summary>
public Snapshot(TAggregate data)
: base(data, null)
{

}

/// <inheritdoc/>
public new virtual TAggregate Data
/// <param name="aggregate">The <see cref="IAggregateRoot"/> to create a new <see cref="Snapshot{TAggregate}"/> for</param>
/// <returns>A new <see cref="Snapshot{TAggregate}"/> of the specified <see cref="IAggregateRoot"/></returns>
public static Snapshot<TAggregate> CreateFor<TAggregate>(TAggregate aggregate)
where TAggregate : class, IAggregateRoot
{
get
{
return (TAggregate)base.Data;
}
protected set
{
base.Data = value;
}
return new(aggregate);
}

}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d4c8256

Please sign in to comment.