/// <summary>
 /// Registers <typeparamref name="TFactory"/> to be factory for singleton service of type <typeparamref name="TInterface"/>.
 /// </summary>
 /// <typeparam name="TInterface">A type of the contract.</typeparam>
 /// <typeparam name="TFactory">A type of the factory.</typeparam>
 /// <param name="collection">A collection dependencies.</param>
 /// <returns><paramref name="collection"/>.</returns>
 public static IDependencyDefinitionCollection AddSingletonFactory <TInterface, TFactory>(this IDependencyDefinitionCollection collection)
     where TFactory : IFactory <TInterface>
 {
     Ensure.NotNull(collection, "collection");
     collection.Add(typeof(TInterface), DependencyLifetime.NameScope(collection.ScopeName), typeof(TFactory));
     return(collection);
 }
 /// <summary>
 /// Maps <typeparamref name="TInterface"/> to <typeparamref name="TImplementation"/> in current scope (as a singleton).
 /// </summary>
 /// <typeparam name="TInterface">A type of the contract.</typeparam>
 /// <typeparam name="TImplementation">A type of the implementation.</typeparam>
 /// <param name="collection">A collection of dependencies.</param>
 /// <returns><paramref name="collection"/>.</returns>
 public static IDependencyDefinitionCollection AddSingleton <TInterface, TImplementation>(this IDependencyDefinitionCollection collection)
     where TImplementation : TInterface
 {
     Ensure.NotNull(collection, "collection");
     collection.Add(typeof(TInterface), DependencyLifetime.NameScope(collection.ScopeName), typeof(TImplementation));
     return(collection);
 }
Ejemplo n.º 3
0
        private SimpleDependencyContainer(string scopeName, SimpleDependencyContainer parentContainer)
        {
            Ensure.NotNullOrEmpty(scopeName, "scopeName");
            this.scopeName       = scopeName;
            this.parentContainer = parentContainer;

            InstanceStorage instances = new InstanceStorage();

            if (parentContainer == null)
            {
                this.definitions = new DependencyDefinitionCollection(scopeName, instances);
            }
            else
            {
                this.definitions = new DependencyDefinitionCollection(scopeName, instances, parentContainer.definitions);
            }

            this.resolver = new InstanceResolver(definitions, instances);

            definitions.Add(typeof(IDependencyContainer), DependencyLifetime.NameScope(scopeName), this);
            definitions.Add(typeof(IDependencyProvider), DependencyLifetime.NameScope(scopeName), this);
        }
 /// <summary>
 /// Registers <typeparamref name="TImplementation"/> as a singleton <paramref name="instance"/> in current scope.
 /// </summary>
 /// <typeparam name="TImplementation">A type of the service.</typeparam>
 /// <param name="collection">A collection dependencies.</param>
 /// <param name="instance">An instance to be used.</param>
 /// <returns><paramref name="collection"/>.</returns>
 public static IDependencyDefinitionCollection AddSingleton <TImplementation>(this IDependencyDefinitionCollection collection, TImplementation instance)
 {
     Ensure.NotNull(collection, "collection");
     collection.Add(typeof(TImplementation), DependencyLifetime.NameScope(collection.ScopeName), instance);
     return(collection);
 }
 /// <summary>
 /// Registers <typeparamref name="TImplementation"/> as a singleton service in scope named <paramref name="scopeName"/>.
 /// </summary>
 /// <typeparam name="TImplementation">A type of the service.</typeparam>
 /// <param name="collection">A collection dependencies.</param>
 /// <param name="scopeName">A na of the scope where service will be available.</param>
 /// <returns><paramref name="collection"/>.</returns>
 public static IDependencyDefinitionCollection AddScoped <TImplementation>(this IDependencyDefinitionCollection collection, string scopeName)
 {
     Ensure.NotNull(collection, "collection");
     collection.Add(typeof(TImplementation), DependencyLifetime.NameScope(scopeName), typeof(TImplementation));
     return(collection);
 }