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
{
ArrayStore<Type> arrayStore = (ArrayStore<Type>)base.Context.AttributeConditions;
base.Context.AttributeConditions = arrayStore.Add(typeof(TAttribute));
return (TConfigurator)this;
}
public TConfigurator WhenHas(Type attributeType)
{
ArrayStore<Type> arrayStore = (ArrayStore<Type>)base.Context.AttributeConditions;
base.Context.AttributeConditions = arrayStore.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(ILifetime lifetime)
{
base.Context.Lifetime = lifetime;
return (TConfigurator)this;
}
public TConfigurator WithScopedLifetime()
{
base.Context.Lifetime = new ScopedLifetime();
return (TConfigurator)this;
}
public TConfigurator WithSingletonLifetime()
{
base.Context.Lifetime = new SingletonLifetime();
return (TConfigurator)this;
}
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 = new ArrayStore<Type>(EnumerableExtensions.CastToArray<Type>(Enumerable.Concat<Type>(base.ImplementationType.GetRegisterableInterfaceTypes(), base.ImplementationType.GetRegisterableBaseTypes())));
return (TConfigurator)this;
}
public TConfigurator InNamedScope(object scopeName)
{
return WithLifetime(new NamedScopeLifetime(scopeName));
}
public TConfigurator DefinesScope(object scopeName)
{
base.Context.DefinedScopeName = scopeName;
return (TConfigurator)this;
}
public TConfigurator WithPerResolutionRequestLifetime()
{
return WithLifetime(new ResolutionRequestLifetime());
}
public TConfigurator AsServiceAlso(Type serviceType)
{
if (!base.ImplementationType.Implements(serviceType))
throw new ArgumentException("The given service type is not assignable from the current implementation type.");
base.Context.AdditionalServiceTypes = ((ArrayStore<Type>)base.Context.AdditionalServiceTypes).Add(serviceType);
return (TConfigurator)this;
}
}
}