FluentServiceConfigurator<TConfigurator>
public class FluentServiceConfigurator<TConfigurator> : BaseFluentConfigurator<TConfigurator> where TConfigurator : FluentServiceConfigurator<TConfigurator>
Represents the fluent service registration api.
using Stashbox.Lifetime;
using Stashbox.Resolution;
using Stashbox.Utils.Data;
using System;
using System.Linq;
namespace Stashbox.Registration.Fluent
{
public class FluentServiceConfigurator<TConfigurator> : BaseFluentConfigurator<TConfigurator> where TConfigurator : FluentServiceConfigurator<TConfigurator>
{
internal FluentServiceConfigurator(Type serviceType, Type implementationType)
: base(serviceType, implementationType)
{
}
public TConfigurator WithDependencyBinding<TDependency>(object dependencyName)
{
return base.WithDependencyBinding(typeof(TDependency), dependencyName);
}
public TConfigurator WhenDependantIs<TTarget>() where TTarget : class
{
base.Context.TargetTypeCondition = typeof(TTarget);
return (TConfigurator)this;
}
public TConfigurator WhenDependantIs(Type targetType)
{
base.Context.TargetTypeCondition = targetType;
return (TConfigurator)this;
}
public TConfigurator WhenHas<TAttribute>() where TAttribute : Attribute
{
((ExpandableArray<Type>)base.Context.AttributeConditions).Add(typeof(TAttribute));
return (TConfigurator)this;
}
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 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 WithName(object name)
{
base.Context.Name = name;
return (TConfigurator)this;
}
public TConfigurator AsImplementedTypes()
{
base.Context.AdditionalServiceTypes = ExpandableArray<Type>.FromEnumerable(Enumerable.Concat<Type>(base.ImplementationType.GetRegisterableInterfaceTypes(), base.ImplementationType.GetRegisterableBaseTypes()));
return (TConfigurator)this;
}
public TConfigurator InNamedScope(object scopeName)
{
base.Context.NamedScopeRestrictionIdentifier = scopeName;
return WithLifetime(Lifetimes.NamedScope);
}
public TConfigurator InScopeDefinedBy(Type type)
{
base.Context.NamedScopeRestrictionIdentifier = type;
return WithLifetime(Lifetimes.NamedScope);
}
public TConfigurator InScopeDefinedBy<TScopeDefiner>()
{
base.Context.NamedScopeRestrictionIdentifier = typeof(TScopeDefiner);
return this.WithLifetime(Lifetimes.NamedScope);
}
public TConfigurator DefinesScope(object scopeName = null)
{
base.Context.DefinedScopeName = (scopeName ?? base.ImplementationType);
return (TConfigurator)this;
}
public TConfigurator WithPerScopedRequestLifetime()
{
return WithLifetime(Lifetimes.PerScopedRequest);
}
public TConfigurator AsServiceAlso<TAdditionalService>()
{
return this.AsServiceAlso(typeof(TAdditionalService));
}
public TConfigurator AsServiceAlso(Type serviceType)
{
if (!base.ImplementationType.Implements(serviceType))
throw new ArgumentException($"""{base.ImplementationType}""{serviceType}""");
((ExpandableArray<Type>)base.Context.AdditionalServiceTypes).Add(serviceType);
return (TConfigurator)this;
}
}
}