Created
December 19, 2018 17:26
-
-
Save tstephansen/e94716c7986dc659290d88f455946991 to your computer and use it in GitHub Desktop.
IContainerRegistry Extensions for Prism
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Prism.Ioc; | |
using Prism.Unity; | |
using Unity; | |
using Unity.Lifetime; | |
using Unity.Registration; | |
namespace MyProject.PrismExtensions | |
{ | |
/// <summary> | |
/// <see cref="IContainerRegistry"/> extension methods that allow you to specify a lifetime | |
/// manager and injection members when registering types and instances with the <see cref="IUnityContainer"/>. | |
/// </summary> | |
/// <remarks>This class was based on the <see cref="UnityContainerExtensions"/> class.</remarks> | |
public static class ContainerRegistryExtensions | |
{ | |
#region Type Registration Overloads | |
#region Non-Generic Overloads | |
/// <summary> | |
/// An IContainerRegistry extension method that registers this object. | |
/// </summary> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="t">The <see cref="Type"/> that will be returned.</param> | |
/// <param name="injectionMembers"> | |
/// A variable-length parameters list containing injection members. | |
/// </param> | |
public static void Register(this IContainerRegistry containerRegistry, Type t, | |
params InjectionMember[] injectionMembers) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterType(t, injectionMembers); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers this object. | |
/// </summary> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="from">The <see cref="Type"/> that will be requested.</param> | |
/// <param name="to">The <see cref="Type"/> that will be returned.</param> | |
/// <param name="injectionMembers"> | |
/// A variable-length parameters list containing injection members. | |
/// </param> | |
public static void Register(this IContainerRegistry containerRegistry, Type from, Type to, | |
params InjectionMember[] injectionMembers) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterType(from, to, injectionMembers); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers this object. | |
/// </summary> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="from">The <see cref="Type"/> that will be requested.</param> | |
/// <param name="to">The <see cref="Type"/> that will be returned.</param> | |
/// <param name="name">The name to use for registration, null if a default registration.</param> | |
/// <param name="injectionMembers"> | |
/// A variable-length parameters list containing injection members. | |
/// </param> | |
public static void Register(this IContainerRegistry containerRegistry, Type from, Type to, string name, | |
params InjectionMember[] injectionMembers) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterType(from, to, name, injectionMembers); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers this object. | |
/// </summary> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="from">The <see cref="Type"/> that will be requested.</param> | |
/// <param name="to">The <see cref="Type"/> that will be returned.</param> | |
/// <param name="lifetimeManager"> | |
/// The <see cref="LifetimeManager"/> that controls the lifetime of the returned instance. | |
/// </param> | |
/// <param name="injectionMembers"> | |
/// A variable-length parameters list containing injection members. | |
/// </param> | |
public static void Register(this IContainerRegistry containerRegistry, Type from, Type to, | |
LifetimeManager lifetimeManager, | |
params InjectionMember[] injectionMembers) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterType(from, to, lifetimeManager, injectionMembers); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers this object. | |
/// </summary> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="t">The <see cref="Type"/> that will be returned.</param> | |
/// <param name="lifetimeManager"> | |
/// The <see cref="LifetimeManager"/> that controls the lifetime of the returned instance. | |
/// </param> | |
/// <param name="injectionMembers"> | |
/// A variable-length parameters list containing injection members. | |
/// </param> | |
public static void Register(this IContainerRegistry containerRegistry, Type t, LifetimeManager lifetimeManager, | |
params InjectionMember[] injectionMembers) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterType(t, lifetimeManager, injectionMembers); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers this object. | |
/// </summary> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="t">The <see cref="Type"/> that will be returned.</param> | |
/// <param name="name">The name to use for registration, null if a default registration.</param> | |
/// <param name="injectionMembers"> | |
/// A variable-length parameters list containing injection members. | |
/// </param> | |
public static void Register(this IContainerRegistry containerRegistry, Type t, string name, | |
params InjectionMember[] injectionMembers) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterType(t, name, injectionMembers); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers this object. | |
/// </summary> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="t">The <see cref="Type"/> that will be returned.</param> | |
/// <param name="name">The name to use for registration, null if a default registration.</param> | |
/// <param name="lifetimeManager"> | |
/// The <see cref="LifetimeManager"/> that controls the lifetime of the returned instance. | |
/// </param> | |
/// <param name="injectionMembers"> | |
/// A variable-length parameters list containing injection members. | |
/// </param> | |
public static void Register(this IContainerRegistry containerRegistry, Type t, string name, | |
LifetimeManager lifetimeManager, | |
params InjectionMember[] injectionMembers) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterType(t, name, lifetimeManager, injectionMembers); | |
} | |
#endregion | |
#region Generic Overloads | |
/// <summary> | |
/// An IContainerRegistry extension method that registers this object. | |
/// </summary> | |
/// <typeparam name="T">The type to configure injection on.</typeparam> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="injectionMembers"> | |
/// A variable-length parameters list containing injection members. | |
/// </param> | |
public static void Register<T>(this IContainerRegistry containerRegistry, | |
params InjectionMember[] injectionMembers) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterType<T>(injectionMembers); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers this object. | |
/// </summary> | |
/// <typeparam name="T"> | |
/// The type to apply the <paramref name="lifetimeManager"/> to. | |
/// </typeparam> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="lifetimeManager"> | |
/// The <see cref="LifetimeManager"/> that controls the lifetime of the returned instance. | |
/// </param> | |
/// <param name="injectionMembers"> | |
/// A variable-length parameters list containing injection members. | |
/// </param> | |
public static void Register<T>(this IContainerRegistry containerRegistry, LifetimeManager lifetimeManager, | |
params InjectionMember[] injectionMembers) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterType<T>(lifetimeManager, injectionMembers); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers this object. | |
/// </summary> | |
/// <typeparam name="T"><see cref="Type"/> this registration is for.</typeparam> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="name">The name to use for registration, null if a default registration.</param> | |
/// <param name="injectionMembers"> | |
/// A variable-length parameters list containing injection members. | |
/// </param> | |
public static void Register<T>(this IContainerRegistry containerRegistry, string name, | |
params InjectionMember[] injectionMembers) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterType<T>(name, injectionMembers); | |
} | |
public static void Register<T>(this IContainerRegistry containerRegistry, string name, | |
LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterType<T>(name, lifetimeManager, injectionMembers); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers this object. | |
/// </summary> | |
/// <typeparam name="TFrom"><see cref="Type"/> that will be requested.</typeparam> | |
/// <typeparam name="TTo"><see cref="Type"/> that will be returned.</typeparam> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="injectionMembers"> | |
/// A variable-length parameters list containing injection members. | |
/// </param> | |
public static void Register<TFrom, TTo>(this IContainerRegistry containerRegistry, | |
params InjectionMember[] injectionMembers) where TTo : TFrom | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterType<TFrom, TTo>(injectionMembers); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers this object. | |
/// </summary> | |
/// <typeparam name="TFrom"><see cref="Type"/> that will be requested.</typeparam> | |
/// <typeparam name="TTo"><see cref="Type"/> that will be returned.</typeparam> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="lifetimeManager"> | |
/// The <see cref="LifetimeManager"/> that controls the lifetime of the returned instance. | |
/// </param> | |
/// <param name="injectionMembers"> | |
/// A variable-length parameters list containing injection members. | |
/// </param> | |
public static void Register<TFrom, TTo>(this IContainerRegistry containerRegistry, | |
LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers) where TTo : TFrom | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterType<TFrom, TTo>(lifetimeManager, injectionMembers); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers this object. | |
/// </summary> | |
/// <typeparam name="TFrom"><see cref="Type"/> that will be requested.</typeparam> | |
/// <typeparam name="TTo"><see cref="Type"/> that will be returned.</typeparam> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="name">The name to use for registration, null if a default registration.</param> | |
/// <param name="injectionMembers"> | |
/// A variable-length parameters list containing injection members. | |
/// </param> | |
public static void Register<TFrom, TTo>(this IContainerRegistry containerRegistry, string name, | |
params InjectionMember[] injectionMembers) where TTo : TFrom | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterType<TFrom, TTo>(name, injectionMembers); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers this object. | |
/// </summary> | |
/// <typeparam name="TFrom"><see cref="Type"/> that will be requested.</typeparam> | |
/// <typeparam name="TTo"><see cref="Type"/> that will be returned.</typeparam> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="name">The name to use for registration, null if a default registration.</param> | |
/// <param name="lifetimeManager"> | |
/// The <see cref="LifetimeManager"/> that controls the lifetime of the returned instance. | |
/// </param> | |
/// <param name="injectionMembers"> | |
/// A variable-length parameters list containing injection members. | |
/// </param> | |
public static void Register<TFrom, TTo>(this IContainerRegistry containerRegistry, string name, | |
LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers) where TTo : TFrom | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterType<TFrom, TTo>(name, lifetimeManager, injectionMembers); | |
} | |
#endregion | |
#endregion | |
#region Instance Registration Overloads | |
#region Generic Overloads | |
/// <summary> | |
/// An IContainerRegistry extension method that registers the instance. | |
/// </summary> | |
/// <typeparam name="T"> | |
/// Type of instance to register (may be an implemented interface instead of the full type). | |
/// </typeparam> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="instance">The instance that will be returned.</param> | |
/// <param name="lifetimeManager"> | |
/// The <see cref="LifetimeManager"/> that controls the lifetime of the returned instance. | |
/// </param> | |
public static void RegisterInstance<T>(this IContainerRegistry containerRegistry, T instance, | |
LifetimeManager lifetimeManager) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterInstance(typeof(T), instance, lifetimeManager); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers the instance. | |
/// </summary> | |
/// <typeparam name="T"> | |
/// Type of instance to register (may be an implemented interface instead of the full type). | |
/// </typeparam> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="name">The name to use for registration, null if a default registration.</param> | |
/// <param name="instance">The instance that will be returned.</param> | |
public static void RegisterInstance<T>(this IContainerRegistry containerRegistry, string name, T instance) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterInstance(typeof(T), name, instance); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers the instance. | |
/// </summary> | |
/// <typeparam name="T"> | |
/// Type of instance to register (may be an implemented interface instead of the full type). | |
/// </typeparam> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="name">The name to use for registration, null if a default registration.</param> | |
/// <param name="instance">The instance that will be returned.</param> | |
/// <param name="lifetimeManager"> | |
/// The <see cref="LifetimeManager"/> that controls the lifetime of the returned instance. | |
/// </param> | |
public static void RegisterInstance<T>(this IContainerRegistry containerRegistry, string name, T instance, | |
LifetimeManager lifetimeManager) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterInstance(typeof(T), name, instance, lifetimeManager); | |
} | |
#endregion | |
#region Non-Generic Overloads | |
/// <summary> | |
/// An IContainerRegistry extension method that registers the instance. | |
/// </summary> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="t"> | |
/// The <see cref="Type"/> of instance to register (may be an implemented interface | |
/// instead of the full type). | |
/// </param> | |
/// <param name="instance">The instance that will be returned.</param> | |
public static void RegisterInstance(this IContainerRegistry containerRegistry, Type t, object instance) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterInstance(t, instance); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers the instance. | |
/// </summary> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="t"> | |
/// The <see cref="Type"/> of instance to register (may be an implemented interface | |
/// instead of the full type). | |
/// </param> | |
/// <param name="instance">The instance that will be returned.</param> | |
/// <param name="lifetimeManager"> | |
/// The <see cref="LifetimeManager"/> that controls the lifetime of the returned instance. | |
/// </param> | |
public static void RegisterInstance(this IContainerRegistry containerRegistry, Type t, object instance, | |
LifetimeManager lifetimeManager) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterInstance(t, instance, lifetimeManager); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers the instance. | |
/// </summary> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="t"> | |
/// The <see cref="Type"/> of instance to register (may be an implemented interface | |
/// instead of the full type). | |
/// </param> | |
/// <param name="name">The name to use for registration, null if a default registration.</param> | |
/// <param name="instance">The instance that will be returned.</param> | |
public static void RegisterInstance(this IContainerRegistry containerRegistry, Type t, string name, | |
object instance) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterInstance(t, name, instance); | |
} | |
/// <summary> | |
/// An IContainerRegistry extension method that registers the instance. | |
/// </summary> | |
/// <param name="containerRegistry">The containerRegistry to act on.</param> | |
/// <param name="t"> | |
/// The <see cref="Type"/> of instance to register (may be an implemented interface | |
/// instead of the full type). | |
/// </param> | |
/// <param name="name">The name to use for registration, null if a default registration.</param> | |
/// <param name="instance">The instance that will be returned.</param> | |
/// <param name="lifetimeManager"> | |
/// The <see cref="LifetimeManager"/> that controls the lifetime of the returned instance. | |
/// </param> | |
public static void RegisterInstance(this IContainerRegistry containerRegistry, Type t, string name, | |
object instance, LifetimeManager lifetimeManager) | |
{ | |
var container = containerRegistry.GetContainer(); | |
container.RegisterInstance(t, name, instance, lifetimeManager); | |
} | |
#endregion | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment