/// <summary> /// Registers an open generic type to another open generic type, allowing, for example, IService&T& to be registered to resolve to Service&T&. /// </summary> /// <param name="container">The container.</param> /// <param name="variantsOf">The open generic interface that callers will attempt to resolve, e.g. typeof(IService&T&).</param> /// <param name="to">The open generic type to resolve, e.g. typeof(Service&T&).</param> /// <param name="singletons">If true, each type will be lazily registered as a singleton.</param> /// <returns></returns> /// <exception cref="System.ArgumentException"> /// Parameter 'variantsOf' is not an open generic type, e.g. typeof(IService&T&) /// or /// Parameter 'to' is not an open generic type, e.g. typeof(Service&T&) /// </exception> public static PocketContainer RegisterGeneric(this PocketContainer container, Type variantsOf, Type to, bool singletons = false) { if (!variantsOf.GetTypeInfo().IsGenericTypeDefinition) { throw new ArgumentException("Parameter 'variantsOf' is not an open generic type, e.g. typeof(IService<>)"); } if (!to.GetTypeInfo().IsGenericTypeDefinition) { throw new ArgumentException("Parameter 'to' is not an open generic type, e.g. typeof(Service<>)"); } return(container.AddStrategy(t => { if (t.GetTypeInfo().IsGenericType&& t.GetGenericTypeDefinition() == variantsOf) { var closedGenericType = to.MakeGenericType(t.GetTypeInfo().GenericTypeArguments); if (singletons) { container.TryRegisterSingle( t, cc => cc.Resolve(closedGenericType)); } return c => c.Resolve(closedGenericType); } return null; })); }
public static PocketContainer IncludeDependencyResolver( this PocketContainer container, IDependencyResolver dependencyResolver) { return(container.AddStrategy(type => { if (dependencyResolver.GetService(type) != null) { return c => dependencyResolver.GetService(type); } return null; })); }
public static PocketContainer AutoMockInterfacesAndAbstractClasses( this PocketContainer container) { return(container.AddStrategy(type => { if (type.IsInterface || type.IsAbstract) { var moqType = typeof(Mock <>).MakeGenericType(type); return c => { var mock = Activator.CreateInstance(moqType) as Mock; mock.DefaultValue = DefaultValue.Mock; return ((dynamic)mock).Object; }; } return null; })); }
public static PocketContainer IfOnlyOneImplementationUseIt( this PocketContainer container) { return(container.AddStrategy(type => { if (type.IsInterface || type.IsAbstract) { var implementations = Discover.ConcreteTypes() .DerivedFrom(type) .ToArray(); if (implementations.Count() == 1) { return c => c.Resolve(implementations.Single()); } } return null; })); }
/// <summary> /// Clones the container, allowing for selectively overriding registrations. /// </summary> public PocketContainer CreateOverrideContainer() { var fallback = this; var child = new PocketContainer { resolvers = new ConcurrentDictionary<Type, Func<PocketContainer, object>>(resolvers), strategyChain = strategyChain }; return child.AddStrategy(t => { // if the parent already has a registation, use it Func<PocketContainer, object> resolver; if (fallback.resolvers.TryGetValue(t, out resolver)) { return resolver; } return null; }); }
/// <summary> /// Registers an open generic type to another open generic type, allowing, for example, IService&T& to be registered to resolve to Service&T&. /// </summary> /// <param name="container">The container.</param> /// <param name="variantsOf">The open generic interface that callers will attempt to resolve, e.g. typeof(IService&T&).</param> /// <param name="to">The open generic type to resolve, e.g. typeof(Service&T&).</param> /// <returns></returns> /// <exception cref="System.ArgumentException"> /// Parameter 'variantsOf' is not an open generic type, e.g. typeof(IService&T&) /// or /// Parameter 'to' is not an open generic type, e.g. typeof(Service&T&) /// </exception> public static PocketContainer RegisterGeneric(this PocketContainer container, Type variantsOf, Type to) { if (!variantsOf.IsGenericTypeDefinition) { throw new ArgumentException("Parameter 'variantsOf' is not an open generic type, e.g. typeof(IService<>)"); } if (!to.IsGenericTypeDefinition) { throw new ArgumentException("Parameter 'to' is not an open generic type, e.g. typeof(Service<>)"); } return(container.AddStrategy(t => { if (t.IsGenericType && t.GetGenericTypeDefinition() == variantsOf) { var closedGenericType = to.MakeGenericType(t.GetGenericArguments()); return c => c.Resolve(closedGenericType); } return null; })); }
/// <summary> /// Clones the container, allowing for selectively overriding registrations. /// </summary> public PocketContainer CreateOverrideContainer() { var fallback = this; var child = new PocketContainer { resolvers = new ConcurrentDictionary <Type, Func <PocketContainer, object> >(resolvers), strategyChain = strategyChain }; return(child.AddStrategy(t => { // if the parent already has a registation, use it Func <PocketContainer, object> resolver; if (fallback.resolvers.TryGetValue(t, out resolver)) { return resolver; } return null; })); }
/// <summary> /// Configures a <see cref="PocketContainer" /> to prefer constructors that do not contain primitive types. /// </summary> /// <remarks>Primitive types include any type for which <see cref="Type.IsPrimitive" /> is true, as well as <see cref="String" />, <see cref="DateTime" />, and <see cref="DateTimeOffset" />.</remarks> /// <param name="container">The same container instance.</param> public static PocketContainer AvoidConstructorsWithPrimitiveTypes( this PocketContainer container) { return(container.AddStrategy(UseLongestConstructorHavingNoPrimitiveTypes)); }