FluentServiceConfigurator<TConfigurator>
public class FluentServiceConfigurator<TConfigurator> : BaseFluentConfigurator<TConfigurator>, IFluentServiceConfigurator<TConfigurator>, IBaseFluentConfigurator<TConfigurator> where TConfigurator : FluentServiceConfigurator<TConfigurator>
Represents the fluent service registraton api.
using Stashbox.Entity;
using Stashbox.Lifetime;
using Stashbox.Utils;
using System;
using System.Linq;
namespace Stashbox.Registration.Fluent
{
public class FluentServiceConfigurator<TConfigurator> : BaseFluentConfigurator<TConfigurator>, IFluentServiceConfigurator<TConfigurator>, IBaseFluentConfigurator<TConfigurator> where TConfigurator : FluentServiceConfigurator<TConfigurator>
{
internal FluentServiceConfigurator(Type serviceType, Type implementationType)
: base(serviceType, implementationType)
{
}
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 WithFactory(Func<IDependencyResolver, object> containerFactory, bool isCompiledLambda = false)
{
base.Context.ContainerFactory = containerFactory;
base.Context.IsFactoryDelegateACompiledLambda = isCompiledLambda;
return (TConfigurator)this;
}
public TConfigurator WithFactory(Func<object> singleFactory, bool isCompiledLambda = false)
{
base.Context.SingleFactory = singleFactory;
base.Context.IsFactoryDelegateACompiledLambda = isCompiledLambda;
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 WithInstance(object instance, bool wireUp = false)
{
base.Context.ExistingInstance = instance;
base.Context.IsWireUp = wireUp;
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 WithPerResolutionRequestLifetime()
{
return WithLifetime(Lifetimes.PerRequest);
}
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;
}
public TConfigurator SetImplementationType(Type implementationType)
{
if (!implementationType.Implements(base.ServiceType))
throw new ArgumentException($"""{implementationType}""{base.ServiceType}""");
base.ImplementationType = implementationType;
return (TConfigurator)this;
}
}
}