Stashbox by Peter Csajtai

<PackageReference Include="Stashbox" Version="3.5.0-preview-615" />

 BaseFluentConfigurator<TConfigurator>

public class BaseFluentConfigurator<TConfigurator> : RegistrationConfiguration where TConfigurator : BaseFluentConfigurator<TConfigurator>
Represents the base of the fluent registration api.
using Stashbox.Configuration; using Stashbox.Exceptions; using Stashbox.Lifetime; using Stashbox.Resolution; using Stashbox.Utils; using Stashbox.Utils.Data; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace Stashbox.Registration.Fluent { public class BaseFluentConfigurator<TConfigurator> : RegistrationConfiguration where TConfigurator : BaseFluentConfigurator<TConfigurator> { internal BaseFluentConfigurator(Type serviceType, Type implementationType) : base(serviceType, implementationType) { } internal BaseFluentConfigurator(Type serviceType, Type implementationType, RegistrationContext registrationContext) : base(serviceType, implementationType, registrationContext) { } public TConfigurator WithLifetime(LifetimeDescriptor lifetime) { base.Context.Lifetime = lifetime; return (TConfigurator)this; } public TConfigurator WithScopedLifetime() { return WithLifetime(Lifetimes.Scoped); } public TConfigurator WithSingletonLifetime() { return WithLifetime(Lifetimes.Singleton); } public TConfigurator WithTransientLifetime() { return WithLifetime(Lifetimes.Transient); } public TConfigurator WithPerScopedRequestLifetime() { return WithLifetime(Lifetimes.PerScopedRequest); } public TConfigurator InNamedScope(object scopeName) { return WithLifetime(Lifetimes.NamedScope(scopeName)); } public TConfigurator InScopeDefinedBy(Type type) { return WithLifetime(Lifetimes.NamedScope(type)); } public TConfigurator InScopeDefinedBy<TScopeDefiner>() { return this.WithLifetime(Lifetimes.NamedScope(typeof(TScopeDefiner))); } public TConfigurator WithDependencyBinding<TDependency>(object dependencyName) { return this.WithDependencyBinding(typeof(TDependency), dependencyName); } public TConfigurator WithDependencyBinding(Type dependencyType, object dependencyName) { Shield.EnsureNotNull<Type>(dependencyType, "dependencyType"); Shield.EnsureNotNull<object>(dependencyName, "dependencyName"); base.Context.DependencyBindings.Add(dependencyType, dependencyName); return (TConfigurator)this; } public TConfigurator WithDependencyBinding(string parameterName, object dependencyName) { Shield.EnsureNotNull<string>(parameterName, "parameterName"); Shield.EnsureNotNull<object>(dependencyName, "dependencyName"); base.Context.DependencyBindings.Add(parameterName, dependencyName); return (TConfigurator)this; } public TConfigurator WhenDependantIs<TTarget>() where TTarget : class { return this.WhenDependantIs(typeof(TTarget)); } public TConfigurator WhenDependantIs(Type targetType) { base.Context.TargetTypeCondition = targetType; return (TConfigurator)this; } public TConfigurator WhenHas<TAttribute>() where TAttribute : Attribute { return this.WhenHas(typeof(TAttribute)); } public TConfigurator WhenHas(Type attributeType) { ((ExpandableArray<Type>)base.Context.AttributeConditions).Add(attributeType); return (TConfigurator)this; } public TConfigurator When(Func<TypeInformation, bool> resolutionCondition) { base.Context.ResolutionCondition = resolutionCondition; return (TConfigurator)this; } public TConfigurator WithInjectionParameters(params KeyValuePair<string, object>[] injectionParameters) { ((ExpandableArray<KeyValuePair<string, object>>)base.Context.InjectionParameters).AddRange(injectionParameters); return (TConfigurator)this; } public TConfigurator WithInjectionParameter(string name, object value) { ((ExpandableArray<KeyValuePair<string, object>>)base.Context.InjectionParameters).Add(new KeyValuePair<string, object>(name, value)); return (TConfigurator)this; } public TConfigurator WithAutoMemberInjection(Rules.AutoMemberInjectionRules rule = Rules.AutoMemberInjectionRules.PropertiesWithPublicSetter, Func<MemberInfo, bool> filter = null) { base.Context.AutoMemberInjectionEnabled = true; base.Context.AutoMemberInjectionRule = rule; base.Context.AutoMemberInjectionFilter = filter; return (TConfigurator)this; } public TConfigurator WithConstructorSelectionRule(Func<IEnumerable<ConstructorInfo>, IEnumerable<ConstructorInfo>> rule) { base.Context.ConstructorSelectionRule = rule; return (TConfigurator)this; } public TConfigurator WithConstructorByArgumentTypes(params Type[] argumentTypes) { ConstructorInfo constructorByArguments = base.ImplementationType.GetConstructorByArguments(argumentTypes); if (constructorByArguments == (ConstructorInfo)null) ThrowConstructorNotFoundException(base.ImplementationType, argumentTypes); base.Context.SelectedConstructor = constructorByArguments; return (TConfigurator)this; } public TConfigurator WithConstructorByArguments(params object[] arguments) { Type[] array = Enumerable.ToArray<Type>(Enumerable.Select<object, Type>((IEnumerable<object>)arguments, (Func<object, Type>)((object arg) => arg.GetType()))); ConstructorInfo constructorByArguments = base.ImplementationType.GetConstructorByArguments(array); if (constructorByArguments == (ConstructorInfo)null) ThrowConstructorNotFoundException(base.ImplementationType, array); base.Context.SelectedConstructor = constructorByArguments; base.Context.ConstructorArguments = arguments; return (TConfigurator)this; } public TConfigurator InjectMember(string memberName, object dependencyName = null) { base.Context.InjectionMemberNames.Add(memberName, dependencyName); return (TConfigurator)this; } public TConfigurator WithoutDisposalTracking() { base.Context.IsLifetimeExternallyOwned = true; return (TConfigurator)this; } public TConfigurator ReplaceExisting() { base.Context.ReplaceExistingRegistration = true; return (TConfigurator)this; } public TConfigurator ReplaceOnlyIfExists() { base.Context.ReplaceExistingRegistrationOnlyIfExists = true; return (TConfigurator)this; } private static void ThrowConstructorNotFoundException(Type type, params Type[] argTypes) { ConstructorNotFoundException ex; switch (argTypes.LongLength) { case 0: ex = new ConstructorNotFoundException(type, (Exception)null); break; case 1: ex = new ConstructorNotFoundException(type, argTypes[0], null); break; default: ex = new ConstructorNotFoundException(type, argTypes, null); break; } throw ex; } } }