using System; using System.Collections.Generic; using System.Linq; using Quadarax.Foundation.Core.Data.Interface.Domain; using Quadarax.Foundation.Core.Object; namespace Quadarax.Foundation.Core.Data.Domain { public class DomainContextResolver : DisposableObject, IDomainContextResolver { private readonly IDictionary _contextCache; public string[] RegisteredContexts => _contextCache.Keys.ToArray(); public DomainContextResolver() { _contextCache = new Dictionary(); } public IDomain Get(string typeFullName) { if (!_contextCache.ContainsKey(typeFullName)) throw new InvalidOperationException($"Domain context '{typeFullName}' not found in DomainContextResolver. Not registered."); return _contextCache[typeFullName]; } public TDomainContext Register(TDomainContext context) where TDomainContext : IDomain { if (_contextCache.ContainsKey(typeof(TDomainContext).FullName!)) throw new InvalidOperationException($"Domain context '{typeof(TDomainContext).FullName}' is already registered."); _contextCache.Add(typeof(TDomainContext).FullName!, context); return GetCurrent(); } public TDomainContext GetCurrent() where TDomainContext : IDomain { if (!_contextCache.ContainsKey(typeof(TDomainContext).FullName!)) throw new InvalidOperationException($"Domain context '{typeof(TDomainContext).FullName}' not found in DomainContextResolver. Not registered."); return (TDomainContext)_contextCache[typeof(TDomainContext).FullName!]; } protected override void OnDisposing() { if (_contextCache != null) { foreach (var context in _contextCache.Values) { context.Dispose(); } } } } }