| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- 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<string, IDomain> _contextCache;
- public string[] RegisteredContexts => _contextCache.Keys.ToArray();
- public DomainContextResolver()
- {
- _contextCache = new Dictionary<string, IDomain>();
- }
- 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>(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<TDomainContext>();
- }
- public TDomainContext GetCurrent<TDomainContext>() 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();
- }
- }
- }
- }
- }
|