Example #1
0
        /// <summary>
        /// Register an instance as an instance of a given type. It will returned when resolving any
        /// base class or implemented interface.
        /// </summary>
        /// <param name="facade">The type that is registered.</param>
        /// <param name="instance">The instance for this type. If null it will be created on demand
        /// on the first access to <paramref name="facade"/></param>
        public void Register(Type facade, object instance)
        {
            EnsureAlive();
            if (facade == null)
            {
                throw new ArgumentNullException(nameof(facade));
            }
            if (instance != null && !Registry.IsInstance(facade, instance))
            {
                throw new ArgumentException($"{instance} is not a {facade.Name}");
            }
            if (instance == null && !CanBeInstantiated(facade))
            {
                throw new ArgumentNullException(nameof(instance), $"Type {facade.Name} is not Resolvable, registered instance should NOT be null.");
            }
            TypeVault vault;

            if (!exact.TryGetValue(facade, out vault))
            {
                vault         = new TypeVault(facade);
                exact[facade] = vault;

                foreach (var implements in vault.Implements())
                {
                    List <TypeVault> list;
                    if (!reverseInheritance.TryGetValue(implements, out list))
                    {
                        list = new List <TypeVault>();
                        reverseInheritance[implements] = list;
                    }
                    list.Add(vault);
                }
            }
            vault.Instance = instance;
        }
Example #2
0
 object Resolve(TypeVault vault, RequestCache cache)
 {
     if (vault.Instance == null)
     {
         vault.Instance = Create(vault.Type, cache);
     }
     return(vault.Instance);
 }