using System; using Microsoft.Practices.Unity; using System.Collections.Generic; namespace Hazware.Commons.Unity { public class TypeTrackingExtension : UnityContainerExtension { #region Fields private readonly Dictionary> _registeredTypes = new Dictionary>(); #endregion #region Private Methods protected override void Initialize() { Context.RegisteringInstance += OnNewInstance; Context.Registering += OnNewType; } private void OnNewInstance(object sender, RegisterInstanceEventArgs e) { HashSet names; string name = string.IsNullOrEmpty(e.Name) ? string.Empty : e.Name; if (!_registeredTypes.TryGetValue(e.RegisteredType, out names)) { // not found, so add it _registeredTypes.Add(e.RegisteredType, new HashSet { name }); } else { // already added type, so add name names.Add(name); } } private void OnNewType(object sender, RegisterEventArgs e) { HashSet names; string name = string.IsNullOrEmpty(e.Name) ? string.Empty : e.Name; if (!_registeredTypes.TryGetValue(e.TypeFrom, out names)) { // not found, so add it _registeredTypes.Add(e.TypeFrom, new HashSet { name }); } else { // already added type, so add name names.Add(name); } } #endregion #region CanResolve /// /// Determines whether this type can be resolved as the default. /// /// /// /// true if this instance can resolve; otherwise, false. /// public bool CanResolve() { return CanResolve(null); } /// /// Determines whether this type can be resolved with the specified name. /// /// /// The name. /// /// true if this instance can be resolved with the specified name; otherwise, false. /// public bool CanResolve(string name) { HashSet names; if (_registeredTypes.TryGetValue(typeof(T), out names)) { return names.Contains(name ?? string.Empty); } return false; } /// /// Determines whether this instance can be resolved at all. /// /// /// /// true if this instance can be resolved at all; otherwise, false. /// public bool CanResolveAny() { return _registeredTypes.ContainsKey(typeof(T)); } #endregion #region TryResolve /// /// Tries to resolve the type, returning null if not found. /// /// The type to try and resolve. /// An object of type if found, or null if not. public T TryResolve() { return TryResolve(default(T)); } /// /// Tries to resolve the type with the specified of name, returning null if not found. /// /// The type to try and resolve. /// The name associated with the type. /// An object of type if found, or null if not. public T TryResolve(string name) { return TryResolve(name, default(T)); } /// /// Tries to resolve the type, returning null if not found. /// /// The type to try and resolve. /// The default value to return if type not found. /// An object of type if found, or the if not. public T TryResolve(T defaultValue) { if (!CanResolve()) return defaultValue; return Container.Resolve(); } /// /// Tries to resolve the type with the specified of name, returning null if not found. /// /// The type to try and resolve. /// The name associated with the type. /// The default value to return if type not found. /// An object of type if found, or the if not. public T TryResolve(string name, T defaultValue) { if (!CanResolve(name)) return defaultValue; return Container.Resolve(name); } #endregion } }