| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- 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<TDomainContext> : DisposableObject, IUnitOfWork<TDomainContext> 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<TDomainContext>();
- 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<TCustomDomainContext> As<TCustomDomainContext>() where TCustomDomainContext : IDomain
- {
- return new UnitOfWork<TCustomDomainContext>((TCustomDomainContext)(IDomain)Context, Stamp);
- }
- protected override void OnDisposing()
- {
- Debug.WriteLine($"UOW>>> {Stamp:D} [CTX:{CtxHash}] disposed");
- Context?.Rollback();
- Context = default;
- }
- }
- }
|