using System; using System.Collections.Generic; using Umbraco.Core.Composing; namespace Umbraco.Core { /// /// Provides extension methods to the class. /// public static class ContainerExtensions { /// /// Gets an instance. /// /// The type of the instance. /// The container. /// An instance of the specified type. /// Throws an exception if the container failed to get an instance of the specified type. public static T GetInstance(this IContainer container) => (T) container.GetInstance(typeof(T)); /// /// Gets an instance with arguments. /// /// The type of the instance. /// The container. /// Arguments. /// An instance of the specified type. /// /// Throws an exception if the container failed to get an instance of the specified type. /// The arguments are used as dependencies by the container. /// public static T GetInstance(this IContainer container, object[] args) => (T) container.GetInstance(typeof(T), args); /// /// Tries to get an instance. /// /// The type of the instance. /// An instance of the specified type, or null. /// 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. public static T TryGetInstance(this IContainer container) => (T) container.TryGetInstance(typeof(T)); // fixme - document all these public static IEnumerable GetRegistered(this IContainer container) => container.GetRegistered(typeof(TService)); public static void Register(this IContainer container, Lifetime lifetime = Lifetime.Transient) => container.Register(typeof(TService), typeof(TImplementing), lifetime); public static void Register(this IContainer container, string name, Lifetime lifetime = Lifetime.Transient) => container.Register(typeof(TService), typeof(TImplementing), name, lifetime); public static void Register(this IContainer container, Lifetime lifetime = Lifetime.Transient) => container.Register(typeof(TService), lifetime); public static void RegisterSingleton(this IContainer container) => container.Register(typeof(TService), Lifetime.Singleton); public static void RegisterSingleton(this IContainer container) => container.Register(typeof(TService), typeof(TImplementing), Lifetime.Singleton); public static void RegisterSingleton(this IContainer container, Func factory) => container.Register(factory, Lifetime.Singleton); public static void RegisterInstance(this IContainer container, TService instance) => container.RegisterInstance(typeof(TService), instance); public static void RegisterAuto(this IContainer container) => container.RegisterAuto(typeof(TServiceBase)); } }