diff --git a/src/Splat.Prism/SplatContainerExtension.cs b/src/Splat.Prism/SplatContainerExtension.cs index 752ce08cd..5600b6ff4 100644 --- a/src/Splat.Prism/SplatContainerExtension.cs +++ b/src/Splat.Prism/SplatContainerExtension.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; +using System.Threading; using Prism.Ioc; namespace Splat.Prism @@ -15,12 +16,15 @@ namespace Splat.Prism [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1316:Tuple element names should use correct casing", Justification = "Match Prism naming scheme.")] public class SplatContainerExtension : IContainerExtension, IDependencyResolver { + private Action _disposeAction; + /// /// Initializes a new instance of the class. /// public SplatContainerExtension() { Locator.SetLocator(Instance); + _disposeAction = () => Locator.SetLocator(new ModernDependencyResolver()); } /// @@ -28,27 +32,32 @@ public SplatContainerExtension() /// public IDependencyResolver Instance { get; } = new ModernDependencyResolver(); + /// public void Dispose() { - throw new NotImplementedException(); + Dispose(true); + GC.SuppressFinalize(this); } /// public void FinalizeExtension() { - Locator.SetLocator(new ModernDependencyResolver()); + Dispose(); } + /// public object GetService(Type serviceType, string contract = null) { return Instance.GetService(serviceType, contract); } + /// public IEnumerable GetServices(Type serviceType, string contract = null) { return Instance.GetServices(serviceType, contract); } + /// public bool HasRegistration(Type serviceType, string contract = null) { return Instance.HasRegistration(serviceType, contract); @@ -80,6 +89,7 @@ public IContainerRegistry Register(Type from, Type to, string name) return this; } + /// public void Register(Func factory, Type serviceType, string contract = null) { Instance.Register(factory, serviceType, contract); @@ -137,19 +147,34 @@ public object Resolve(Type type, string name, params (Type Type, object Instance throw new NotImplementedException(); } + /// public IDisposable ServiceRegistrationCallback(Type serviceType, string contract, Action callback) { return Instance.ServiceRegistrationCallback(serviceType, contract, callback); } + /// public void UnregisterAll(Type serviceType, string contract = null) { Instance.UnregisterAll(serviceType, contract); } + /// public void UnregisterCurrent(Type serviceType, string contract = null) { Instance.UnregisterCurrent(serviceType, contract); } + + /// + /// Disposes data associated with the extension. + /// + /// If we are getting called by the Dispose() method rather than a finalizer. + protected virtual void Dispose(bool isDisposing) + { + if (isDisposing) + { + Interlocked.Exchange(ref _disposeAction, null)?.Invoke(); + } + } } }