Files
Umbraco-CMS/src/Umbraco.Core/Composing/ContainerExtensions.cs

123 lines
5.7 KiB
C#
Raw Normal View History

2018-07-20 15:45:01 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
2018-07-20 09:49:05 +02:00
2018-07-20 16:39:39 +02:00
namespace Umbraco.Core.Composing
2018-07-20 09:49:05 +02:00
{
/// <summary>
/// Provides extension methods to the <see cref="IContainer"/> class.
/// </summary>
public static class ContainerExtensions
{
/// <summary>
/// Gets an instance.
/// </summary>
/// <typeparam name="T">The type of the instance.</typeparam>
/// <param name="container">The container.</param>
/// <returns>An instance of the specified type.</returns>
/// <remarks>Throws an exception if the container failed to get an instance of the specified type.</remarks>
public static T GetInstance<T>(this IContainer container)
=> (T) container.GetInstance(typeof(T));
/// <summary>
/// Tries to get an instance.
/// </summary>
/// <typeparam name="T">The type of the instance.</typeparam>
/// <returns>An instance of the specified type, or null.</returns>
/// <remarks>Returns null if the container does not know how to get an instance
/// of the specified type. Throws an exception if the container does know how
/// to get an instance of the specified type, but failed to do so.</remarks>
public static T TryGetInstance<T>(this IContainer container)
=> (T) container.TryGetInstance(typeof(T));
2018-07-23 09:21:55 +02:00
/// <summary>
/// Gets registration for a service.
/// </summary>
/// <typeparam name="TService">The type of the service.</typeparam>
/// <returns>The registrations for the service.</returns>
public static IEnumerable<Registration> GetRegistered<TService>(this IContainer container)
=> container.GetRegistered(typeof(TService));
2018-07-23 09:21:55 +02:00
/// <summary>
/// Creates an instance with arguments.
/// </summary>
/// <typeparam name="T">The type of the instance.</typeparam>
/// <param name="container">The container.</param>
/// <param name="args">Arguments.</param>
/// <returns>An instance of the specified type.</returns>
/// <remarks>
/// <para>Throws an exception if the container failed to get an instance of the specified type.</para>
/// <para>The arguments are used as dependencies by the container.</para>
/// </remarks>
public static T CreateInstance<T>(this IContainer container, params object[] args)
=> (T) container.CreateInstance(typeof(T), args);
/// <summary>
/// Registers a service with an implementation type.
/// </summary>
2018-07-20 15:45:01 +02:00
public static void Register<TService, TImplementing>(this IContainer container, Lifetime lifetime = Lifetime.Transient)
=> container.Register(typeof(TService), typeof(TImplementing), lifetime);
2018-07-23 09:21:55 +02:00
/// <summary>
/// Registers a service with a named implementation type.
/// </summary>
2018-07-20 15:45:01 +02:00
public static void Register<TService, TImplementing>(this IContainer container, string name, Lifetime lifetime = Lifetime.Transient)
=> container.Register(typeof(TService), typeof(TImplementing), name, lifetime);
2018-07-23 09:21:55 +02:00
/// <summary>
/// Registers a service as its own implementation.
/// </summary>
2018-07-20 15:45:01 +02:00
public static void Register<TService>(this IContainer container, Lifetime lifetime = Lifetime.Transient)
=> container.Register(typeof(TService), lifetime);
2018-07-23 09:21:55 +02:00
/// <summary>
/// Registers a singleton service as its own implementation.
/// </summary>
2018-07-20 15:45:01 +02:00
public static void RegisterSingleton<TService>(this IContainer container)
=> container.Register(typeof(TService), Lifetime.Singleton);
2018-07-23 09:21:55 +02:00
/// <summary>
/// Registers a singleton service with an implementation type.
/// </summary>
2018-07-20 15:45:01 +02:00
public static void RegisterSingleton<TService, TImplementing>(this IContainer container)
=> container.Register(typeof(TService), typeof(TImplementing), Lifetime.Singleton);
2018-07-23 09:21:55 +02:00
/// <summary>
/// Registers a singleton service with an implementation factory.
/// </summary>
2018-07-20 15:45:01 +02:00
public static void RegisterSingleton<TService>(this IContainer container, Func<IContainer, TService> factory)
=> container.Register(factory, Lifetime.Singleton);
2018-07-23 09:21:55 +02:00
/// <summary>
/// Registers a service with an implementing instance.
/// </summary>
2018-07-20 15:45:01 +02:00
public static void RegisterInstance<TService>(this IContainer container, TService instance)
=> container.RegisterInstance(typeof(TService), instance);
2018-07-23 09:21:55 +02:00
/// <summary>
/// Registers a base type for auto-registration.
/// </summary>
2018-07-20 15:45:01 +02:00
public static void RegisterAuto<TServiceBase>(this IContainer container)
=> container.RegisterAuto(typeof(TServiceBase));
/// <summary>
/// Registers and instanciates a collection builder.
/// </summary>
/// <typeparam name="TBuilder">The type of the collection builder.</typeparam>
/// <returns>A collection builder of the specified type.</returns>
public static TBuilder RegisterCollectionBuilder<TBuilder>(this IContainer container)
{
// make sure it's not already registered
// we just don't want to support re-registering collection builders
if (container.GetRegistered<TBuilder>().Any())
throw new InvalidOperationException("Collection builders should be registered only once.");
2018-07-23 09:21:55 +02:00
// register the builder - passing the container as an arg to the factory
container.RegisterSingleton(c => c.CreateInstance<TBuilder>(container));
2018-07-23 09:21:55 +02:00
// initialize and return the builder
return container.GetInstance<TBuilder>();
}
2018-07-20 09:49:05 +02:00
}
}