Stashbox by Peter Csajtai

<PackageReference Include="Stashbox" Version="5.8.2" />

 FluentServiceConfigurator<TService, TImplementation, TConfigurator>

public class FluentServiceConfigurator<TService, TImplementation, TConfigurator> : FluentServiceConfigurator<TConfigurator> where TImplementation : TService where TConfigurator : FluentServiceConfigurator<TService, TImplementation, TConfigurator>
Represents the generic fluent service registration api.
using Stashbox.Lifetime; using Stashbox.Utils; using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; namespace Stashbox.Registration.Fluent { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public class FluentServiceConfigurator<TService, TImplementation, [System.Runtime.CompilerServices.Nullable(0)] TConfigurator> : FluentServiceConfigurator<TConfigurator> where TService : class where TImplementation : class, TService where TConfigurator : FluentServiceConfigurator<TService, TImplementation, TConfigurator> { internal FluentServiceConfigurator(Type serviceType, Type implementationType, [System.Runtime.CompilerServices.Nullable(2)] object name, LifetimeDescriptor lifetimeDescriptor, bool isDecorator) : base(serviceType, implementationType, name, lifetimeDescriptor, isDecorator) { } public TConfigurator WithDependencyBinding<[System.Runtime.CompilerServices.Nullable(2)] TResult>(Expression<Func<TImplementation, TResult>> expression, [System.Runtime.CompilerServices.Nullable(2)] object dependencyName = null) { MemberExpression memberExpression = expression.Body as MemberExpression; if (memberExpression == null) throw new ArgumentException("The expression must be a member expression (Property or Field)", "expression"); if (Options == null) Options = new Dictionary<RegistrationOption, object>(); if (Options.TryGetValue(RegistrationOption.DependencyBindings, out object value)) { Dictionary<object, object> dictionary = value as Dictionary<object, object>; if (dictionary != null) { dictionary.Add((object)memberExpression.Member.Name, dependencyName); goto IL_0083; } } Options[RegistrationOption.DependencyBindings] = new Dictionary<object, object> { { (object)memberExpression.Member.Name, dependencyName } }; goto IL_0083; IL_0083: return (TConfigurator)this; } public TConfigurator WithFinalizer(Action<TImplementation> finalizer) { Shield.EnsureNotNull<Action<TImplementation>>(finalizer, "finalizer"); if (Options == null) Options = new Dictionary<RegistrationOption, object>(); Options[RegistrationOption.Finalizer] = (Action<object>)delegate(object o) { finalizer((TImplementation)o); }; return (TConfigurator)this; } public TConfigurator WithInitializer(Action<TImplementation, IDependencyResolver> initializer) { Shield.EnsureNotNull<Action<TImplementation, IDependencyResolver>>(initializer, "initializer"); if (Options == null) Options = new Dictionary<RegistrationOption, object>(); Options[RegistrationOption.Initializer] = initializer; return (TConfigurator)this; } public TConfigurator WithAsyncInitializer(Func<TImplementation, IDependencyResolver, CancellationToken, Task> initializer) { Shield.EnsureNotNull<Func<TImplementation, IDependencyResolver, CancellationToken, Task>>(initializer, "initializer"); if (Options == null) Options = new Dictionary<RegistrationOption, object>(); Options[RegistrationOption.AsyncInitializer] = (Func<object, IDependencyResolver, CancellationToken, Task>)((object o, IDependencyResolver r, CancellationToken t) => initializer((TImplementation)o, r, t)); return (TConfigurator)this; } public TConfigurator WithFactory(Func<TImplementation> factory, bool isCompiledLambda = false) { SetFactory(factory, isCompiledLambda, TypeCache<TImplementation>.Type); return (TConfigurator)this; } public TConfigurator WithFactory(Func<IDependencyResolver, TImplementation> factory, bool isCompiledLambda = false) { SetFactory(factory, isCompiledLambda, TypeCache<IDependencyResolver>.Type, TypeCache<TImplementation>.Type); return (TConfigurator)this; } public TConfigurator WithFactory<[System.Runtime.CompilerServices.Nullable(2)] T1>(Func<T1, TImplementation> factory, bool isCompiledLambda = false) { base.SetFactory((Delegate)factory, isCompiledLambda, new Type[2] { TypeCache<T1>.Type, TypeCache<TImplementation>.Type }); return (TConfigurator)this; } public TConfigurator WithFactory<[System.Runtime.CompilerServices.Nullable(2)] T1, [System.Runtime.CompilerServices.Nullable(2)] T2>(Func<T1, T2, TImplementation> factory, bool isCompiledLambda = false) { base.SetFactory((Delegate)factory, isCompiledLambda, new Type[3] { TypeCache<T1>.Type, TypeCache<T2>.Type, TypeCache<TImplementation>.Type }); return (TConfigurator)this; } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(1)] public TConfigurator WithFactory<T1, T2, T3>([System.Runtime.CompilerServices.Nullable(1)] Func<T1, T2, T3, TImplementation> factory, bool isCompiledLambda = false) { base.SetFactory((Delegate)factory, isCompiledLambda, new Type[4] { TypeCache<T1>.Type, TypeCache<T2>.Type, TypeCache<T3>.Type, TypeCache<TImplementation>.Type }); return (TConfigurator)this; } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(1)] public TConfigurator WithFactory<T1, T2, T3, T4>([System.Runtime.CompilerServices.Nullable(1)] Func<T1, T2, T3, T4, TImplementation> factory, bool isCompiledLambda = false) { base.SetFactory((Delegate)factory, isCompiledLambda, new Type[5] { TypeCache<T1>.Type, TypeCache<T2>.Type, TypeCache<T3>.Type, TypeCache<T4>.Type, TypeCache<TImplementation>.Type }); return (TConfigurator)this; } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(1)] public TConfigurator WithFactory<T1, T2, T3, T4, T5>([System.Runtime.CompilerServices.Nullable(1)] Func<T1, T2, T3, T4, T5, TImplementation> factory, bool isCompiledLambda = false) { base.SetFactory((Delegate)factory, isCompiledLambda, new Type[6] { TypeCache<T1>.Type, TypeCache<T2>.Type, TypeCache<T3>.Type, TypeCache<T4>.Type, TypeCache<T5>.Type, TypeCache<TImplementation>.Type }); return (TConfigurator)this; } } }