Skip to content

Commit

Permalink
docs: improve uow comments
Browse files Browse the repository at this point in the history
  • Loading branch information
PHILLIPS71 committed Jun 3, 2024
1 parent 7735259 commit 51af175
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

namespace Giantnodes.Infrastructure.EntityFrameworkCore.Uow;

/// <summary>
/// Represents a unit of work implementation for Entity Framework Core.
/// </summary>
/// <typeparam name="TDbContext">The type of the DbContext class.</typeparam>
public sealed class EntityFrameworkUnitOfWork<TDbContext> : UnitOfWork, ITransientDependency
where TDbContext : DbContext
{
Expand All @@ -19,6 +23,7 @@ public EntityFrameworkUnitOfWork(IUnitOfWorkExecutor executor, TDbContext databa
_database = database;
}

/// <inheritdoc />
protected override async Task OnBeginAsync(UnitOfWorkOptions options, CancellationToken cancellation = default)
{
if (options.Timeout.HasValue)
Expand All @@ -32,6 +37,7 @@ protected override async Task OnBeginAsync(UnitOfWorkOptions options, Cancellati
_transaction = await _database.Database.BeginTransactionAsync(cancellation);
}

/// <inheritdoc />
protected override async Task OnCommitAsync(CancellationToken cancellation = default)
{
var events = _database
Expand Down
51 changes: 50 additions & 1 deletion src/Infrastructure/src/Infrastructure/Uow/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,55 @@ public abstract class UnitOfWork : IUnitOfWork
private bool _committed;
private Exception? _exception;

/// <summary>
/// Gets the list of domain events that occurred during the execution of the unit of work.
/// </summary>
protected List<Event> DomainEvents { get; }

/// <summary>
/// Gets the unique correlation identifier for this unit of work.
/// </summary>
public Guid CorrelationId { get; }

/// <summary>
/// Gets the user identifier associated with this unit of work, or null if not set.
/// </summary>
public Guid? UserId { get; private set; }

/// <summary>
/// Gets the tenant identifier associated with this unit of work, or null if not set.
/// </summary>
public Guid? TenantId { get; private set; }

/// <summary>
/// Gets the value indicating whether this unit of work has been disposed.
/// </summary>
public bool IsDisposed { get; private set; }

/// <summary>
/// Occurs when the unit of work has been successfully committed.
/// </summary>
public event EventHandler? Completed;

/// <summary>
/// Occurs when an exception is thrown during the execution of the unit of work.
/// </summary>
public event EventHandler? Failed;

/// <summary>
/// Occurs when the unit of work is disposed.
/// </summary>
public event EventHandler? Disposed;

/// <summary>
/// Gets the options used to configure the behavior of this unit of work.
/// </summary>
public UnitOfWorkOptions? Options { get; private set; }

public IReadOnlyCollection<object> Events => DomainEvents.ToList().AsReadOnly();
/// <summary>
/// Gets a read-only collection of domain events that occurred during the execution of the unit of work.
/// </summary>
public IReadOnlyCollection<object> Events => DomainEvents.AsReadOnly();

protected UnitOfWork(IUnitOfWorkExecutor executor)
{
Expand All @@ -39,6 +69,12 @@ protected UnitOfWork(IUnitOfWorkExecutor executor)
DomainEvents = new List<Event>();
}

/// <summary>
/// Begins the unit of work with the specified options.
/// </summary>
/// <param name="options">The options used to configure the behavior of the unit of work.</param>
/// <param name="cancellation">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
/// <returns>The unit of work instance.</returns>
public async Task<IUnitOfWork> BeginAsync(UnitOfWorkOptions options, CancellationToken cancellation = default)
{
ArgumentNullException.ThrowIfNull(options);
Expand All @@ -61,6 +97,10 @@ public async Task<IUnitOfWork> BeginAsync(UnitOfWorkOptions options, Cancellatio
}
}

/// <summary>
/// Commits the unit of work.
/// </summary>
/// <param name="cancellation">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
public async Task CommitAsync(CancellationToken cancellation = default)
{
if (_committed)
Expand Down Expand Up @@ -100,7 +140,16 @@ protected virtual void Dispose(bool dispose)
Disposed?.Invoke(this, EventArgs.Empty);
}

/// <summary>
/// Performs any necessary initialization or setup tasks before the unit of work begins.
/// </summary>
/// <param name="options">The options used to configure the behavior of the unit of work.</param>
/// <param name="cancellation">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
protected abstract Task OnBeginAsync(UnitOfWorkOptions options, CancellationToken cancellation = default);

/// <summary>
/// Performs any necessary commit operations after the unit of work has completed successfully.
/// </summary>
/// <param name="cancellation">A <see cref="CancellationToken"/> to observe while waiting for the task to complete.</param>
protected abstract Task OnCommitAsync(CancellationToken cancellation = default);
}

0 comments on commit 51af175

Please sign in to comment.