The problem with the above interface is that all implementations of the interface has to implement all the methods. If you use extension methods here, than you can rewrite the interface like the following:
public interface IUnityContainer : IDisposable
{
IUnityContainer RegisterType(Type from, Type to, string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers);
IUnityContainer RegisterInstance(Type t, string name, object instance, LifetimeManager lifetime);
object Resolve(Type t, string name);
IEnumerable<object> ResolveAll(Type t);
IUnityContainer AddExtension(UnityContainerExtension extension);
IUnityContainer RemoveAllExtensions();
object BuildUp(Type t, object existing, string name);
void Teardown(object o);
IUnityContainer Parent { get; }
IUnityContainer CreateChildContainer();
object Configure(Type configurationInterface);
}
The following code defines all extension methods:
public static class IUnityContainerExtentions
{
public static IUnityContainer AddNewExtension<TExtension>(this IUnityContainer container) where TExtension : UnityContainerExtension, new()
{
return container.AddExtension(Activator.CreateInstance<TExtension>());
}
public static T BuildUp<T>(this IUnityContainer container, T existing)
{
return (T)container.BuildUp(typeof(T), existing, null);
}
public static object BuildUp(this IUnityContainer container, Type t, object existing)
{
return container.BuildUp(t, existing, null);
}
public static T BuildUp<T>(this IUnityContainer container, T existing, string name)
{
return (T)container.BuildUp(typeof(T), existing, name);
}
public static TConfigurator Configure<TConfigurator>(this IUnityContainer container) where TConfigurator : IUnityContainerExtensionConfigurator
{
return (TConfigurator)container.Configure(typeof(TConfigurator));
}
public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, TInterface instance)
{
return container.RegisterInstance(typeof(TInterface), null, instance, new ContainerControlledLifetimeManager());
}
public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, string name, TInterface instance)
{
return container.RegisterInstance(typeof(TInterface), name, instance, new ContainerControlledLifetimeManager());
}
public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, TInterface instance, LifetimeManager lifetimeManager)
{
return container.RegisterInstance(typeof(TInterface), null, instance, lifetimeManager);
}
public static IUnityContainer RegisterInstance(this IUnityContainer container, Type t, object instance)
{
return container.RegisterInstance(t, null, instance, new ContainerControlledLifetimeManager());
}
public static IUnityContainer RegisterInstance<TInterface>(this IUnityContainer container, string name, TInterface instance, LifetimeManager lifetimeManager)
{
return container.RegisterInstance(typeof(TInterface), name, instance, lifetimeManager);
}
public static IUnityContainer RegisterInstance(this IUnityContainer container, Type t, object instance, LifetimeManager lifetimeManager)
{
return container.RegisterInstance(t, null, instance, lifetimeManager);
}
public static IUnityContainer RegisterInstance(this IUnityContainer container, Type t, string name, object instance)
{
return container.RegisterInstance(t, name, instance, new ContainerControlledLifetimeManager());
}
public static IUnityContainer RegisterType<T>(this IUnityContainer container, params InjectionMember[] injectionMembers)
{
return container.RegisterType(typeof(T), null, null, null, injectionMembers);
}
public static IUnityContainer RegisterType<TFrom, TTo>(this IUnityContainer container, params InjectionMember[] injectionMembers) where TTo : TFrom
{
return container.RegisterType(typeof(TFrom), typeof(TTo), null, null, injectionMembers);
}
public static IUnityContainer RegisterType<T>(this IUnityContainer container, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
{
return container.RegisterType(typeof(T), null, null, lifetimeManager, injectionMembers);
}
public static IUnityContainer RegisterType<TFrom, TTo>(this IUnityContainer container, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers) where TTo : TFrom
{
return container.RegisterType(typeof(TFrom), typeof(TTo), null, lifetimeManager, injectionMembers);
}
public static IUnityContainer RegisterType<T>(this IUnityContainer container, string name, params InjectionMember[] injectionMembers)
{
return container.RegisterType(typeof(T), null, name, null, injectionMembers);
}
public static IUnityContainer RegisterType<TFrom, TTo>(this IUnityContainer container, string name, params InjectionMember[] injectionMembers) where TTo : TFrom
{
return container.RegisterType(typeof(TFrom), typeof(TTo), name, null, injectionMembers);
}
public static IUnityContainer RegisterType(this IUnityContainer container, Type t, params InjectionMember[] injectionMembers)
{
return container.RegisterType(t, null, null, null, injectionMembers);
}
public static IUnityContainer RegisterType<T>(this IUnityContainer container, string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
{
return container.RegisterType(typeof(T), null, name, lifetimeManager, injectionMembers);
}
public static IUnityContainer RegisterType<TFrom, TTo>(this IUnityContainer container, string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers) where TTo : TFrom
{
return container.RegisterType(typeof(TFrom), typeof(TTo), name, lifetimeManager, injectionMembers);
}
public static IUnityContainer RegisterType(this IUnityContainer container, Type t, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
{
return container.RegisterType(t, null, null, lifetimeManager, injectionMembers);
}
public static IUnityContainer RegisterType(this IUnityContainer container, Type t, string name, params InjectionMember[] injectionMembers)
{
return container.RegisterType(t, null, name, null, injectionMembers);
}
public static IUnityContainer RegisterType(this IUnityContainer container, Type from, Type to, params InjectionMember[] injectionMembers)
{
container.RegisterType(from, to, null, null, injectionMembers);
return container;
}
public static IUnityContainer RegisterType(this IUnityContainer container, Type t, string name, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
{
return container.RegisterType(t, null, name, lifetimeManager, injectionMembers);
}
public static IUnityContainer RegisterType(this IUnityContainer container, Type from, Type to, LifetimeManager lifetimeManager, params InjectionMember[] injectionMembers)
{
return container.RegisterType(from, to, null, lifetimeManager, injectionMembers);
}
public static IUnityContainer RegisterType(this IUnityContainer container, Type from, Type to, string name, params InjectionMember[] injectionMembers)
{
return container.RegisterType(from, to, name, null, injectionMembers);
}
public static T Resolve<T>(this IUnityContainer container)
{
return (T)container.Resolve(typeof(T));
}
public static T Resolve<T>(this IUnityContainer container, string name)
{
return (T)container.Resolve(typeof(T), name);
}
public static object Resolve(this IUnityContainer container, Type t)
{
return container.Resolve(t, null);
}
public static IEnumerable<T> ResolveAll<T>(this IUnityContainer container)
{
//return new <ResolveAll>d__0<T>(-2) { <>4__this = this };
//This implementation requires more effort than I am willing to give (6hours till my holiday!)
}
}