using System; using System.Diagnostics; using Quadarax.Foundation.Core.Data.Interface.Domain; using Quadarax.Foundation.Core.Object; namespace Quadarax.Foundation.Core.Data.Domain { public class UnitOfWork : DisposableObject, IUnitOfWork where TDomainContext : IDomain { public TDomainContext Context { get; protected set; } public Guid Stamp { get; protected set; } public int CtxHash => Context.GetHashCode(); public UnitOfWork(IDomainContextResolver context,Guid? customStamp = null) { Context = context.GetCurrent(); if (customStamp == null) customStamp = Guid.NewGuid(); Stamp = customStamp.GetValueOrDefault(); Debug.WriteLine($"UOW>>> {Stamp:D} [CTX:{CtxHash}] created"); } public UnitOfWork(TDomainContext context, Guid? customStamp = null) { Context = context; if (customStamp == null) customStamp = Guid.NewGuid(); Stamp = customStamp.GetValueOrDefault(); Debug.WriteLine($"UOW>>> {Stamp:D} [CTX:{CtxHash}] created"); } public void Commit() { Context.Commit(); Debug.WriteLine($"UOW>>> {Stamp:D} [CTX:{CtxHash}] commited"); } public IUnitOfWork As() where TCustomDomainContext : IDomain { return new UnitOfWork((TCustomDomainContext)(IDomain)Context, Stamp); } protected override void OnDisposing() { Debug.WriteLine($"UOW>>> {Stamp:D} [CTX:{CtxHash}] disposed"); Context?.Rollback(); Context = default; } } }