Stashbox by Peter Csajtai

<PackageReference Include="Stashbox" Version="4.0.0-preview-670" />

 FluentServiceConfigurator<TService, TImplementation, TConfigurator>

public class FluentServiceConfigurator<TService, TImplementation, TConfigurator> : FluentServiceConfigurator<TConfigurator>, IFluentCompositor<TImplementation, TConfigurator> where TConfigurator : FluentServiceConfigurator<TService, TImplementation, TConfigurator>
Represents the generic fluent service registration api.
using Stashbox.Utils; using System; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; namespace Stashbox.Registration.Fluent { public class FluentServiceConfigurator<TService, TImplementation, TConfigurator> : FluentServiceConfigurator<TConfigurator>, IFluentCompositor<TImplementation, TConfigurator> where TConfigurator : FluentServiceConfigurator<TService, TImplementation, TConfigurator> { internal FluentServiceConfigurator(Type serviceType, Type implementationType) : base(serviceType, implementationType) { } internal FluentServiceConfigurator(Type serviceType, Type implementationType, RegistrationContext registrationContext) : base(serviceType, implementationType, registrationContext) { } public TConfigurator WithDependencyBinding<TResult>(Expression<Func<TImplementation, TResult>> expression, object dependencyName = null) { MemberExpression memberExpression = expression.Body as MemberExpression; if (memberExpression != null) { base.Context.DependencyBindings.Add((object)memberExpression.Member.Name, dependencyName); return (TConfigurator)this; } throw new ArgumentException("The expression must be a member expression (Property or Field)", "expression"); } public TConfigurator WithFinalizer(Action<TImplementation> finalizer) { if (finalizer == null) return (TConfigurator)this; base.Context.Finalizer = delegate(object o) { finalizer((TImplementation)o); }; return (TConfigurator)this; } public TConfigurator WithInitializer(Action<TImplementation, IDependencyResolver> initializer) { base.Context.Initializer = initializer; return (TConfigurator)this; } public TConfigurator WithAsyncInitializer(Func<TImplementation, IDependencyResolver, CancellationToken, Task> initializer) { if (initializer == null) return (TConfigurator)this; base.Context.AsyncInitializer = ((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, typeof(TImplementation)); return (TConfigurator)this; } public TConfigurator WithFactory(Func<IDependencyResolver, TImplementation> factory, bool isCompiledLambda = false) { SetFactory(factory, isCompiledLambda, Constants.ResolverType, typeof(TImplementation)); return (TConfigurator)this; } public TConfigurator WithFactory<T1>(Func<T1, TImplementation> factory, bool isCompiledLambda = false) { base.SetFactory((Delegate)factory, isCompiledLambda, new Type[2] { typeof(T1), typeof(TImplementation) }); return (TConfigurator)this; } public TConfigurator WithFactory<T1, T2>(Func<T1, T2, TImplementation> factory, bool isCompiledLambda = false) { base.SetFactory((Delegate)factory, isCompiledLambda, new Type[3] { typeof(T1), typeof(T2), typeof(TImplementation) }); return (TConfigurator)this; } public TConfigurator WithFactory<T1, T2, T3>(Func<T1, T2, T3, TImplementation> factory, bool isCompiledLambda = false) { base.SetFactory((Delegate)factory, isCompiledLambda, new Type[4] { typeof(T1), typeof(T2), typeof(T3), typeof(TImplementation) }); return (TConfigurator)this; } public TConfigurator WithFactory<T1, T2, T3, T4>(Func<T1, T2, T3, T4, TImplementation> factory, bool isCompiledLambda = false) { base.SetFactory((Delegate)factory, isCompiledLambda, new Type[5] { typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(TImplementation) }); return (TConfigurator)this; } public TConfigurator WithFactory<T1, T2, T3, T4, T5>(Func<T1, T2, T3, T4, T5, TImplementation> factory, bool isCompiledLambda = false) { base.SetFactory((Delegate)factory, isCompiledLambda, new Type[6] { typeof(T1), typeof(T2), typeof(T3), typeof(T4), typeof(T5), typeof(TImplementation) }); return (TConfigurator)this; } } }