ServiceRegistration
Represents a service registration.
using Stashbox.Configuration;
using Stashbox.Lifetime;
using Stashbox.Resolution;
using Stashbox.Utils.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Stashbox.Registration
{
[System.Runtime.CompilerServices.NullableContext(1)]
[System.Runtime.CompilerServices.Nullable(0)]
public class ServiceRegistration
{
internal static readonly ServiceRegistration Empty = new ServiceRegistration();
private static int GlobalRegistrationId;
private static int GlobalRegistrationOrder;
public readonly Type ImplementationType;
public readonly int RegistrationId;
public readonly bool IsDecorator;
[System.Runtime.CompilerServices.Nullable(2)]
public readonly object Name;
[System.Runtime.CompilerServices.Nullable(2)]
public readonly ConstructorInfo SelectedConstructor;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
public readonly object[] ConstructorArguments;
public readonly LifetimeDescriptor Lifetime;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
2
})]
public readonly Dictionary<object, object> DependencyBindings;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
public readonly Action<object> Finalizer;
[System.Runtime.CompilerServices.Nullable(2)]
public readonly Delegate Initializer;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1,
1
})]
public readonly Func<object, IDependencyResolver, CancellationToken, Task> AsyncInitializer;
public readonly Rules.AutoMemberInjectionRules AutoMemberInjectionRule;
public readonly bool AutoMemberInjectionEnabled;
public readonly bool IsLifetimeExternallyOwned;
[System.Runtime.CompilerServices.Nullable(2)]
public readonly object DefinedScopeName;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1,
1,
1
})]
public readonly Func<IEnumerable<ConstructorInfo>, IEnumerable<ConstructorInfo>> ConstructorSelectionRule;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
public readonly Func<MemberInfo, bool> AutoMemberInjectionFilter;
[System.Runtime.CompilerServices.Nullable(2)]
public readonly object Metadata;
internal readonly bool ReplaceExistingRegistration;
internal readonly bool ReplaceExistingRegistrationOnlyIfExists;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
internal readonly ExpandableArray<Type> AdditionalServiceTypes;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})]
internal readonly ExpandableArray<KeyValuePair<string, object>> InjectionParameters;
internal readonly bool HasScopeName;
[System.Runtime.CompilerServices.Nullable(2)]
internal readonly object NamedScopeRestrictionIdentifier;
internal readonly bool HasCondition;
internal readonly object RegistrationDiscriminator;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
private readonly ExpandableArray<Type> targetTypeConditions;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
private readonly ExpandableArray<Func<TypeInformation, bool>> resolutionConditions;
[System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})]
private readonly ExpandableArray<Type> attributeConditions;
public int RegistrationOrder { get; set; }
private ServiceRegistration()
{
RegistrationDiscriminator = new object();
Lifetime = Lifetimes.Transient;
ImplementationType = GetType();
ConstructorSelectionRule = Rules.ConstructorSelection.PreferMostParameters;
}
internal ServiceRegistration(Type implementationType, RegistrationContext registrationContext, ContainerConfiguration containerConfiguration, bool isDecorator)
: this(implementationType, containerConfiguration.RegistrationBehavior, isDecorator, registrationContext.Name, registrationContext.SelectedConstructor, registrationContext.ConstructorArguments, registrationContext.Lifetime ?? containerConfiguration.DefaultLifetime, registrationContext.DependencyBindings, registrationContext.Finalizer, registrationContext.Initializer, registrationContext.AsyncInitializer, registrationContext.AutoMemberInjectionRule, registrationContext.AutoMemberInjectionEnabled, registrationContext.IsLifetimeExternallyOwned, registrationContext.DefinedScopeName, registrationContext.ConstructorSelectionRule, registrationContext.AutoMemberInjectionFilter, registrationContext.Metadata, registrationContext.TargetTypeConditions, registrationContext.ResolutionConditions, registrationContext.AttributeConditions, registrationContext.ReplaceExistingRegistration, registrationContext.ReplaceExistingRegistrationOnlyIfExists, registrationContext.AdditionalServiceTypes, registrationContext.InjectionParameters)
{
}
internal ServiceRegistration(Type implementationType, [System.Runtime.CompilerServices.Nullable(2)] object name, ContainerConfiguration containerConfiguration, ServiceRegistration baseRegistration)
: this(implementationType, containerConfiguration.RegistrationBehavior, baseRegistration.IsDecorator, name, baseRegistration.SelectedConstructor, baseRegistration.ConstructorArguments, baseRegistration.Lifetime, baseRegistration.DependencyBindings, baseRegistration.Finalizer, baseRegistration.Initializer, baseRegistration.AsyncInitializer, baseRegistration.AutoMemberInjectionRule, baseRegistration.AutoMemberInjectionEnabled, baseRegistration.IsLifetimeExternallyOwned, baseRegistration.DefinedScopeName, baseRegistration.ConstructorSelectionRule, baseRegistration.AutoMemberInjectionFilter, baseRegistration.Metadata, baseRegistration.targetTypeConditions, baseRegistration.resolutionConditions, baseRegistration.attributeConditions, baseRegistration.ReplaceExistingRegistration, baseRegistration.ReplaceExistingRegistrationOnlyIfExists, baseRegistration.AdditionalServiceTypes, baseRegistration.InjectionParameters)
{
}
internal bool IsUsableForCurrentContext(TypeInformation typeInfo)
{
if (!HasParentTypeConditionAndMatch(typeInfo) && !HasAttributeConditionAndMatch(typeInfo))
return HasResolutionConditionAndMatch(typeInfo);
return true;
}
internal void Replaces(ServiceRegistration serviceRegistration)
{
RegistrationOrder = serviceRegistration.RegistrationOrder;
}
private bool HasParentTypeConditionAndMatch(TypeInformation typeInfo)
{
if (targetTypeConditions != null && typeInfo.ParentType != (Type)null)
return targetTypeConditions.Contains(typeInfo.ParentType);
return false;
}
private bool HasAttributeConditionAndMatch(TypeInformation typeInfo)
{
if (attributeConditions != null && typeInfo.CustomAttributes != null)
return attributeConditions.Intersect(from attribute in typeInfo.CustomAttributes
select attribute.GetType()).Any();
return false;
}
private bool HasResolutionConditionAndMatch(TypeInformation typeInfo)
{
if (resolutionConditions == null)
return false;
int length = resolutionConditions.Length;
for (int i = 0; i < length; i++) {
if (resolutionConditions[i](typeInfo))
return true;
}
return false;
}
private static int ReserveRegistrationId()
{
return Interlocked.Increment(ref GlobalRegistrationId);
}
private static int ReserveRegistrationOrder()
{
return Interlocked.Increment(ref GlobalRegistrationOrder);
}
[System.Runtime.CompilerServices.NullableContext(2)]
private ServiceRegistration([System.Runtime.CompilerServices.Nullable(1)] Type implementationType, Rules.RegistrationBehavior currentRegistrationBehavior, bool isDecorator, object name, ConstructorInfo selectedConstructor, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] object[] constructorArguments, [System.Runtime.CompilerServices.Nullable(1)] LifetimeDescriptor lifetime, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
2
})] Dictionary<object, object> dependencyBindings, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] Action<object> finalizer, Delegate initializer, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1,
1
})] Func<object, IDependencyResolver, CancellationToken, Task> asyncInitializer, Rules.AutoMemberInjectionRules autoMemberInjectionRule, bool autoMemberInjectionEnabled, bool isLifetimeExternallyOwned, object definedScopeName, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1,
1,
1,
1
})] Func<IEnumerable<ConstructorInfo>, IEnumerable<ConstructorInfo>> constructorSelectionRule, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] Func<MemberInfo, bool> autoMemberInjectionFilter, object metadata, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] ExpandableArray<Type> targetTypeConditions, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] ExpandableArray<Func<TypeInformation, bool>> resolutionConditions, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] ExpandableArray<Type> attributeConditions, bool replaceExistingRegistration, bool replaceExistingRegistrationOnlyIfExists, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
1
})] ExpandableArray<Type> additionalServiceTypes, [System.Runtime.CompilerServices.Nullable(new byte[] {
2,
0,
1,
2
})] ExpandableArray<KeyValuePair<string, object>> injectionParameters)
{
ImplementationType = implementationType;
IsDecorator = isDecorator;
Name = name;
SelectedConstructor = selectedConstructor;
ConstructorArguments = constructorArguments;
Lifetime = lifetime;
DependencyBindings = dependencyBindings;
Finalizer = finalizer;
Initializer = initializer;
AsyncInitializer = asyncInitializer;
AutoMemberInjectionRule = autoMemberInjectionRule;
AutoMemberInjectionEnabled = autoMemberInjectionEnabled;
IsLifetimeExternallyOwned = isLifetimeExternallyOwned;
DefinedScopeName = definedScopeName;
ConstructorSelectionRule = constructorSelectionRule;
AutoMemberInjectionFilter = autoMemberInjectionFilter;
Metadata = metadata;
this.targetTypeConditions = targetTypeConditions;
this.resolutionConditions = resolutionConditions;
this.attributeConditions = attributeConditions;
ReplaceExistingRegistration = replaceExistingRegistration;
ReplaceExistingRegistrationOnlyIfExists = replaceExistingRegistrationOnlyIfExists;
AdditionalServiceTypes = additionalServiceTypes;
InjectionParameters = injectionParameters;
NamedScopeLifetime namedScopeLifetime = lifetime as NamedScopeLifetime;
if (namedScopeLifetime != null) {
HasScopeName = true;
NamedScopeRestrictionIdentifier = namedScopeLifetime.ScopeName;
}
HasCondition = (targetTypeConditions != null || resolutionConditions != null || attributeConditions != null);
RegistrationId = ReserveRegistrationId();
RegistrationOrder = ReserveRegistrationOrder();
RegistrationDiscriminator = ((currentRegistrationBehavior == Rules.RegistrationBehavior.PreserveDuplications) ? ((object)RegistrationId) : (name ?? implementationType));
}
}
}