public bool CanResolve(IContainerContext containerContext, TypeInformation typeInformation, InjectionParameter[] injectionParameters) { return this.resolutionStrategy.CanResolve(containerContext, typeInformation, injectionParameters) || this.resolutionStrategy.CanResolve(containerContext.Container.ParentContainer.ContainerContext, typeInformation, injectionParameters); }
/// <summary> /// Gets the instance managed by the <see cref="SingletonLifetime"/> /// </summary> /// <param name="objectBuilder">An <see cref="IObjectBuilder"/> implementation.</param> /// <param name="resolutionInfo">The info about the actual resolution.</param> /// <param name="resolveType">The type info about the resolved type.</param> /// <returns>The lifetime managed object.</returns> public object GetInstance(IObjectBuilder objectBuilder, ResolutionInfo resolutionInfo, TypeInformation resolveType) { if (this.instance != null) return this.instance; lock (this.syncObject) { if (this.instance != null) return this.instance; this.instance = objectBuilder.BuildInstance(resolutionInfo, resolveType); } return this.instance; }
public ResolutionTarget BuildResolutionTarget(IContainerContext containerContext, TypeInformation typeInformation, InjectionParameter[] injectionParameters) { var target = this.resolutionStrategy.BuildResolutionTarget(containerContext, typeInformation, injectionParameters); if (target.Resolver == null && target.ResolutionTargetValue == null) return this.resolutionStrategy.BuildResolutionTarget( containerContext.Container.ParentContainer.ContainerContext, typeInformation, injectionParameters); else return target; }
public ResolutionTarget BuildResolutionTarget(IContainerContext containerContext, TypeInformation typeInformation, InjectionParameter[] injectionParameters) { Resolver resolver; this.resolverSelector.TryChooseResolver(containerContext, typeInformation, out resolver); return new ResolutionTarget { Resolver = resolver, TypeInformation = typeInformation, ResolutionTargetValue = injectionParameters?.FirstOrDefault(param => param.Name == typeInformation.MemberName)?.Value }; }
public object BuildInstance(ResolutionInfo resolutionInfo, TypeInformation resolveType) { if (this.builtInstance != null) return this.builtInstance; lock (this.syncObject) { if (this.builtInstance != null) return this.builtInstance; this.builtInstance = this.objectExtender.FillResolutionMembers(this.instance, this.containerContext, resolutionInfo); this.builtInstance = this.objectExtender.FillResolutionMethods(this.builtInstance, this.containerContext, resolutionInfo); this.builtInstance = this.containerExtensionManager.ExecutePostBuildExtensions(this.builtInstance, this.instanceType, this.containerContext, resolutionInfo, resolveType); } return this.builtInstance; }
public EnumerableResolver(IContainerContext containerContext, TypeInformation typeInfo) : base(containerContext, typeInfo) { this.enumerableType = new TypeInformation { Type = typeInfo.Type.GetEnumerableType(), CustomAttributes = typeInfo.CustomAttributes, ParentType = typeInfo.ParentType, DependencyName = typeInfo.DependencyName }; containerContext.RegistrationRepository.TryGetTypedRepositoryRegistrations(this.enumerableType, out registrationCache); }
public bool TryChooseResolver(IContainerContext containerContext, TypeInformation typeInfo, out Resolver resolver, Func<ResolverRegistration, bool> filter = null) { using (this.readerWriterLock.AcquireReadLock()) { var resolverFactory = filter == null ? this.resolverRepository.FirstOrDefault( registration => registration.Predicate(containerContext, typeInfo)) : this.resolverRepository.Where(filter).FirstOrDefault( registration => registration.Predicate(containerContext, typeInfo)); if (resolverFactory != null) { resolver = resolverFactory.ResolverFactory(containerContext, typeInfo); return true; } resolver = null; return false; } }
public Expression GetExpression(ResolutionInfo resolutionInfo, Expression resolutionInfoExpression, TypeInformation resolveType) { IServiceRegistration registration; if (this.containerContext.RegistrationRepository .TryGetRegistrationWithConditionsWithoutGenericDefinitionExtraction(resolveType, out registration)) return registration.GetExpression(resolutionInfo, resolutionInfoExpression, resolveType); lock (this.syncObject) { if (this.containerContext.RegistrationRepository .TryGetRegistrationWithConditionsWithoutGenericDefinitionExtraction(resolveType, out registration)) return registration.GetExpression(resolutionInfo, resolutionInfoExpression, resolveType); var genericType = this.metaInfoProvider.TypeTo.MakeGenericType(resolveType.Type.GenericTypeArguments); this.containerContext.Container.RegisterType(resolveType.Type, genericType); this.containerContext.RegistrationRepository .TryGetRegistrationWithConditionsWithoutGenericDefinitionExtraction(resolveType, out registration); return registration.GetExpression(resolutionInfo, resolutionInfoExpression, resolveType); } }
public object BuildInstance(ResolutionInfo resolutionInfo, TypeInformation resolveType) { if (this.metaInfoProvider.HasInjectionMethod || this.containerExtensionManager.HasPostBuildExtensions) { if (this.constructorDelegate != null && !this.isConstructorDirty) return this.ResolveType(containerContext, resolutionInfo, resolveType); lock (this.syncObject) { if (this.constructorDelegate != null && !this.isConstructorDirty) return this.ResolveType(containerContext, resolutionInfo, resolveType); ResolutionConstructor constructor; if (!this.metaInfoProvider.TryChooseConstructor(out constructor, resolutionInfo, this.injectionParameters)) throw new ResolutionFailedException(this.metaInfoProvider.TypeTo.FullName); this.constructorDelegate = ExpressionDelegateFactory.CreateConstructorExpression(this.containerContext, constructor, this.GetResolutionMembers()); this.resolutionConstructor = constructor; this.isConstructorDirty = false; return this.ResolveType(containerContext, resolutionInfo, resolveType); } } if (this.createDelegate != null && !this.isConstructorDirty) return this.createDelegate(resolutionInfo); this.createDelegate = this.containerContext.DelegateRepository.GetOrAdd(this.registrationName, () => { ResolutionConstructor constructor; if (!this.metaInfoProvider.TryChooseConstructor(out constructor, resolutionInfo, this.injectionParameters)) throw new ResolutionFailedException(this.metaInfoProvider.TypeTo.FullName); var parameter = Expression.Parameter(typeof(ResolutionInfo)); this.createDelegate = Expression.Lambda<Func<ResolutionInfo, object>>(this.GetExpressionInternal(constructor, resolutionInfo, parameter), parameter).Compile(); this.resolutionConstructor = constructor; this.isConstructorDirty = false; return this.createDelegate; }, this.isConstructorDirty); return this.createDelegate(resolutionInfo); }
public object BuildInstance(ResolutionInfo resolutionInfo, TypeInformation resolveType) { object instance = null; if (this.singleFactory != null) instance = this.singleFactory.Invoke(); if (this.oneParamsFactory != null) instance = this.oneParamsFactory.Invoke(resolutionInfo.FactoryParams.ElementAt(0)); if (this.twoParamsFactory != null) instance = this.twoParamsFactory.Invoke(resolutionInfo.FactoryParams.ElementAt(0), resolutionInfo.FactoryParams.ElementAt(1)); if (this.threeParamsFactory != null) instance = this.threeParamsFactory.Invoke( resolutionInfo.FactoryParams.ElementAt(0), resolutionInfo.FactoryParams.ElementAt(1), resolutionInfo.FactoryParams.ElementAt(2)); var builtInstance = this.objectExtender.FillResolutionMembers(instance, containerContext, resolutionInfo); builtInstance = this.objectExtender.FillResolutionMembers(builtInstance, containerContext, resolutionInfo); return this.containerExtensionManager.ExecutePostBuildExtensions(builtInstance, builtInstance?.GetType(), containerContext, resolutionInfo, resolveType); }
/// <summary> /// Constructs the <see cref="Resolver"/> /// </summary> /// <param name="containerContext">The <see cref="IContainerContext"/> of the <see cref="StashboxContainer"/></param> /// <param name="typeInfo">The type information about the resolved service.</param> protected Resolver(IContainerContext containerContext, TypeInformation typeInfo) { this.BuilderContext = containerContext; this.TypeInfo = typeInfo; }
private bool TryGetByTypeKey(TypeInformation typeInfo, out IServiceRegistration registration) { ImmutableTree<IServiceRegistration> registrations; if (!this.TryGetRegistrationsByType(typeInfo.Type, out registrations)) { registration = null; return false; } registration = registrations.Value; return true; }
private bool TryGetByTypeKeyWithConditionsWithoutGenericDefinitionExtraction(TypeInformation typeInfo, out IServiceRegistration registration) { ImmutableTree<IServiceRegistration> registrations; if (!this.TryGetRegistrationsByTypeWithoutGenericDefinitionExtraction(typeInfo.Type, out registrations)) { registration = null; return false; } var serviceRegistrations = registrations.Enumerate().Select(reg => reg.Value).ToArray(); if (serviceRegistrations.Any(reg => reg.HasCondition)) registration = serviceRegistrations.Where(reg => reg.HasCondition) .FirstOrDefault(reg => reg.IsUsableForCurrentContext(typeInfo)); else registration = serviceRegistrations.FirstOrDefault(reg => reg.IsUsableForCurrentContext(typeInfo)); return registration != null; }
/// <summary> /// Tries to retrieve all registrations for a type. /// </summary> /// <param name="typeInfo">The requested type information.</param> /// <param name="registrations">The retrieved registrations.</param> /// <returns>True if registrations were found, otherwise false.</returns> public bool TryGetTypedRepositoryRegistrations(TypeInformation typeInfo, out IServiceRegistration[] registrations) { var serviceRegistrations = this.serviceRepository.GetValueOrDefault(typeInfo.Type.GetHashCode()); if (serviceRegistrations == null) { Type genericTypeDefinition; if (this.TryHandleOpenGenericType(typeInfo.Type, out genericTypeDefinition)) { serviceRegistrations = this.genericDefinitionRepository.GetValueOrDefault(genericTypeDefinition.GetHashCode()); } else { registrations = null; return false; } } registrations = serviceRegistrations?.Enumerate().Select(reg => reg.Value).ToArray(); return registrations != null; }
private bool TryGetByNamedKeyWithoutGenericDefinitionExtraction(TypeInformation typeInfo, out IServiceRegistration registration) { ImmutableTree<IServiceRegistration> registrations; if (this.TryGetRegistrationsByTypeWithoutGenericDefinitionExtraction(typeInfo.Type, out registrations)) { registration = registrations.GetValueOrDefault(typeInfo.DependencyName.GetHashCode()); return registration != null; } registration = null; return false; }
/// <summary> /// Tries to retrieve a registration with conditions. /// </summary> /// <param name="typeInfo">The requested type information.</param> /// <param name="registration">The retrieved registration.</param> /// <returns>True if a registration was found, otherwise false.</returns> public bool TryGetRegistrationWithConditions(TypeInformation typeInfo, out IServiceRegistration registration) { return typeInfo.DependencyName == null ? this.TryGetByTypeKeyWithConditions(typeInfo, out registration) : this.TryGetByNamedKey(typeInfo, out registration); }
/// <summary> /// Tries to retrieve a non generic definition registration with conditions. /// </summary> /// <param name="typeInfo">The requested type information.</param> /// <param name="registration">The retrieved registration.</param> /// <returns>True if a registration was found, otherwise false.</returns> public bool TryGetRegistrationWithConditionsWithoutGenericDefinitionExtraction(TypeInformation typeInfo, out IServiceRegistration registration) { return typeInfo.DependencyName == null ? this.TryGetByTypeKeyWithConditionsWithoutGenericDefinitionExtraction(typeInfo, out registration) : this.TryGetByNamedKeyWithoutGenericDefinitionExtraction(typeInfo, out registration); }
public bool CanResolve(IContainerContext containerContext, TypeInformation typeInfo) { using (this.readerWriterLock.AcquireReadLock()) return this.resolverRepository.Any(registration => registration.Predicate(containerContext, typeInfo)); }
/// <summary> /// Creates an expression for creating the resolved instance. /// </summary> /// <param name="resolutionInfo">The info about the current resolution.</param> /// <param name="resolutionInfoExpression">The expression of the info about the current resolution.</param> /// <param name="resolveType">The resolve type.</param> /// <returns>The expression.</returns> public Expression GetExpression(ResolutionInfo resolutionInfo, Expression resolutionInfoExpression, TypeInformation resolveType) { return this.lifetimeManager.GetExpression(this.objectBuilder, resolutionInfo, resolutionInfoExpression, resolveType); }
/// <summary> /// Gets the resolved instance. /// </summary> /// <param name="resolutionInfo">The info about the current resolution.</param> /// <param name="resolveType">The resolve type.</param> /// <returns>The created object.</returns> public object GetInstance(ResolutionInfo resolutionInfo, TypeInformation resolveType) { return this.lifetimeManager.GetInstance(this.objectBuilder, resolutionInfo, resolveType); }
/// <summary> /// Gets an overridden value if has any. /// </summary> /// <param name="parameter">The type information.</param> /// <returns>The overridden value.</returns> public object GetOverriddenValue(TypeInformation parameter) { return GetTypedValue(parameter.Type) ?? GetNamedValue(parameter.DependencyName); }
public bool CanResolve(IContainerContext containerContext, TypeInformation typeInformation, InjectionParameter[] injectionParameters) { return this.resolverSelector.CanResolve(containerContext, typeInformation) || (injectionParameters != null && injectionParameters.Any(injectionParameter => injectionParameter.Name == typeInformation.MemberName)); }
public object ExecutePostBuildExtensions(object instance, Type targetType, IContainerContext containerContext, ResolutionInfo resolutionInfo, TypeInformation resolveType, InjectionParameter[] injectionParameters = null) { if (!this.hasPostBuildExtensions) return instance; using (this.readerWriterLock.AcquireReadLock()) { var result = instance; foreach (var extension in this.postbuildExtensions) result = extension.PostBuild(instance, targetType, containerContext, resolutionInfo, resolveType, injectionParameters); return result; } }
/// <summary> /// Gets the expression for getting the instance managed by the <see cref="TransientLifetime"/> /// </summary> /// <param name="objectBuilder">An <see cref="IObjectBuilder"/> implementation.</param> /// <param name="resolutionInfo">The info about the actual resolution.</param> /// <param name="resolutionInfoExpression">The expression of the info about the actual resolution.</param> /// <param name="resolveType">The type info about the resolved type.</param> /// <returns>The lifetime managed object.</returns> public Expression GetExpression(IObjectBuilder objectBuilder, ResolutionInfo resolutionInfo, Expression resolutionInfoExpression, TypeInformation resolveType) { return objectBuilder.GetExpression(resolutionInfo, resolutionInfoExpression, resolveType); }
/// <summary> /// Gets the instance managed by the <see cref="TransientLifetime"/> /// </summary> /// <param name="objectBuilder">An <see cref="IObjectBuilder"/> implementation.</param> /// <param name="resolutionInfo">The info about the actual resolution.</param> /// <param name="resolveType">The type info about the resolved type.</param> /// <returns>The lifetime managed object.</returns> public object GetInstance(IObjectBuilder objectBuilder, ResolutionInfo resolutionInfo, TypeInformation resolveType) { return objectBuilder.BuildInstance(resolutionInfo, resolveType); }
/// <summary> /// Checks whether the registration can be used for a current resolution. /// </summary> /// <param name="typeInfo">The type information.</param> /// <returns>True if the registration can be used for the current resolution, otherwise false.</returns> public bool IsUsableForCurrentContext(TypeInformation typeInfo) { return (this.targetTypeCondition == null && this.resolutionCondition == null && (this.attributeConditions == null || !this.attributeConditions.Any())) || (this.targetTypeCondition != null && typeInfo.ParentType != null && this.targetTypeCondition == typeInfo.ParentType) || (this.attributeConditions != null && typeInfo.CustomAttributes != null && this.attributeConditions.Intersect(typeInfo.CustomAttributes.Select(attribute => attribute.GetType())).Any()) || (this.resolutionCondition != null && this.resolutionCondition(typeInfo)); }
public Expression GetExpression(ResolutionInfo resolutionInfo, Expression resolutionInfoExpression, TypeInformation resolveType) { var callExpression = Expression.Call(Expression.Constant(this), "BuildInstance", null, resolutionInfoExpression, Expression.Constant(resolveType)); return Expression.Convert(callExpression, resolveType.Type); }
/// <summary> /// Check a type exists with conditions. /// </summary> /// <param name="typeInfo">The type information.</param> /// <returns>True if the registration found, otherwise false.</returns> public bool ConstainsTypeKeyWithConditions(TypeInformation typeInfo) { var registrations = this.serviceRepository.GetValueOrDefault(typeInfo.Type.GetHashCode()); if (registrations != null) return registrations.Value != null && registrations.Enumerate() .Any(registration => registration.Value.IsUsableForCurrentContext(typeInfo)); { Type genericTypeDefinition; if (!this.TryHandleOpenGenericType(typeInfo.Type, out genericTypeDefinition)) return false; registrations = this.genericDefinitionRepository.GetValueOrDefault(genericTypeDefinition.GetHashCode()); return registrations != null && registrations.Enumerate().Any(registration => registration.Value.IsUsableForCurrentContext(new TypeInformation { Type = genericTypeDefinition, ParentType = typeInfo.ParentType, DependencyName = typeInfo.DependencyName, CustomAttributes = typeInfo.CustomAttributes })); } }
/// <summary> /// Checks a non generic definition type exists in the repository. /// </summary> /// <param name="typeInfo">The type information.</param> /// <returns>True if the registration found, otherwise false.</returns> public bool ConstainsTypeKeyWithConditionsWithoutGenericDefinitionExtraction(TypeInformation typeInfo) { var registrations = this.serviceRepository.GetValueOrDefault(typeInfo.Type.GetHashCode()); if (registrations == null) return false; return registrations.Value != null && registrations.Enumerate().Any(registration => registration.Value.IsUsableForCurrentContext(typeInfo)); }
public TestResolver(IContainerContext containerContext, TypeInformation typeInfo) : base(containerContext, typeInfo) { }