/// <summary>
        ///     <para>
        ///         Registers the provided <paramref name="instance" /> as the <see langword="object" />
        ///     </para>
        ///     <para>to return when <paramref name="type" /> is requested.</para>
        /// </summary>
        /// <param name="type">The type that will be requested.</param>
        /// <param name="instance">The instance that will be returned.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="type" /> or <paramref name="instance" /> is <see langword="null" /> .
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// Thrown when the <paramref name="instance"/> is registered to a different container.
        /// </exception>
        public void RegisterProvidedInstance([NotNull] Type type, [NotNull] object instance)
        {
            //- Validate
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            var hasContainer = instance as IHasContainer <IObjectContainer>;

            if (hasContainer != null && hasContainer.Container != this)
            {
                throw new InvalidOperationException(
                          "Cannot register an instance that is registered to a different container");
            }

            // Put this instance inside the instance provider
            InstanceProvider.Register(type, instance);

            /* Tell the system to read from the Instance Provider when getting values for this.
             * The InstanceProvider provides all values registered this way using an internal dictionary.
             * This operation will handle both add to dictionary and update existing entry. */

            Mappings[type] = InstanceProvider;
        }
Пример #2
0
        public void CanDefineCustomDefaultProvider()
        {
            const string Bubba = "Bubba";

            // Change the fallback / default provider to be something that yields our magic string
            var provider = new InstanceProvider(null);
            provider.Register(typeof(string), Bubba);
            CommonProvider.RegisterDefaultProvider(provider);

            // Grab the instance and check to see if its our string
            var instance = CommonProvider.Provide<string>();
            instance.ShouldBeSameAs(Bubba);
        }
Пример #3
0
        public void EnsureDependencyContainerUsesItselfToGetDefaultProvider()
        {
            // Create our default provider with instructions to yield itself as an IObjectProvider
            var instanceProvider = new InstanceProvider(null);
            instanceProvider.Register(typeof(IObjectProvider), instanceProvider);

            // Register the new provider as a source for IObjectProvider
            CommonProvider.Register(typeof(IObjectProvider), instanceProvider);

            // Cause DefaultProvider to be lazy loaded and store the result
            var defaultProvider = CommonProvider.Container.FallbackProvider;

            // Check that they're the same object
            instanceProvider.ShouldBeSameAs(defaultProvider);
        }