private Type GetDecoratorTypeFromDecoratorFactory(Type requestedServiceType, DecoratorPredicateContext context) { Type decoratorType = this.data.DecoratorTypeFactory(context); if (decoratorType.Info().ContainsGenericParameters) { if (!requestedServiceType.Info().IsGenericType) { throw new ActivationException( StringResources.TheDecoratorReturnedFromTheFactoryShouldNotBeOpenGeneric( requestedServiceType, decoratorType)); } Requires.TypeFactoryReturnedTypeThatDoesNotContainUnresolvableTypeArguments( requestedServiceType, decoratorType); var builder = new GenericTypeBuilder(requestedServiceType, decoratorType); decoratorType = builder.BuildClosedGenericImplementation().ClosedGenericImplementation; // decoratorType == null when type constraints don't match. if (decoratorType != null) { Requires.HasFactoryCreatedDecorator(this.data.Container, requestedServiceType, decoratorType); } } else { Requires.FactoryReturnsATypeThatIsAssignableFromServiceType(requestedServiceType, decoratorType); } return(decoratorType); }
protected DecoratorPredicateContext CreatePredicateContext( Type registeredServiceType, Expression expression, ServiceTypeDecoratorInfo info) { // NOTE: registeredServiceType can be different from registeredProducer.ServiceType. // This is the case for container-uncontrolled collections where producer.ServiceType is the // IEnumerable<T> and registeredServiceType is T. return(DecoratorPredicateContext.CreateFromInfo(registeredServiceType, expression, info)); }
private static bool DecorateWhenHasValidator(DecoratorPredicateContext c, Container container) { var metadata = container.GetInstance <HandlerMetadataService>(); var handlerType = c?.ImplementationType; return(handlerType != null && metadata.HasValidator(handlerType)); }
protected DecoratorPredicateContext CreatePredicateContext(InstanceProducer registeredProducer, Type registeredServiceType, Expression expression, Lifestyle lifestyle) { var info = this.GetServiceTypeInfo(expression, registeredProducer, registeredServiceType, lifestyle); // NOTE: registeredServiceType can be different from registeredProducer.ServiceType. // This is the case for container uncontrolled collections where producer.ServiceType is the // IEnumerable<T> and registeredServiceType is T. return(DecoratorPredicateContext.CreateFromInfo(registeredServiceType, expression, info)); }
private static Type ValidatorFactory(DecoratorPredicateContext c) { var tRequestHandler = c.ImplementationType .GetInterfaces() .Single(x => x.IsGenericType && ( x.GetGenericTypeDefinition() == typeof(IRequestHandler <>) || x.GetGenericTypeDefinition() == typeof(IRequestHandler <,>))); var handlerArguments = tRequestHandler.GetGenericArguments(); var tRequest = handlerArguments[0]; ValidateAttribute FindAttribute(Type t) { var attr = t.GetCustomAttribute <ValidateAttribute>(false); if (attr == null && t.BaseType != null) { attr = FindAttribute(t.BaseType); } if (attr == null) { foreach (var x in t.GetInterfaces()) { attr = FindAttribute(x); if (attr != null) { return(attr); } } } return(attr); } var validateAttribute = FindAttribute(tRequest); var tValidator = validateAttribute?.ValidatorType ?? typeof(IRequestValidator <>).MakeGenericType(tRequest); if (handlerArguments.Length == 1) { return(typeof(CrudValidateDecorator <,>).MakeGenericType(tRequest, tValidator)); } if (handlerArguments.Length == 2) { var tResult = handlerArguments[1]; return(typeof(CrudValidateDecorator <, ,>).MakeGenericType(tRequest, tResult, tValidator)); } return(null); }
private DecoratorPredicateContext DecorateContext(DecoratorPredicateContext predicateContext) { // CreateRegistration must only be called once per decorated item in the collection, but this is // guaranteed by BuildDecoratorExpression, which simply locks the decoration of the complete // collection. var registration = this.CreateRegistration(this.registeredServiceType, this.decoratorConstructor, predicateContext.Expression); var producer = new InstanceProducer(this.registeredServiceType, registration); var decoratedExpression = registration.BuildExpression(); return(predicateContext.Decorate(this.decoratorType, decoratedExpression, producer)); }
protected bool SatisfiesPredicate(DecoratorPredicateContext context) { return(this.Predicate == null || this.Predicate(context)); }
protected bool SatisfiesPredicate(DecoratorPredicateContext context) => this.Predicate == null || this.Predicate(context);
private Type GetDecoratorTypeFromDecoratorFactory(Type requestedServiceType, DecoratorPredicateContext context) { Type decoratorType = this.data.DecoratorTypeFactory(context); if (decoratorType.ContainsGenericParameters) { if (!requestedServiceType.IsGenericType) { throw new ActivationException( StringResources.TheDecoratorReturnedFromTheFactoryShouldNotBeOpenGeneric( requestedServiceType, decoratorType)); } Requires.TypeFactoryReturnedTypeThatDoesNotContainUnresolvableTypeArguments( requestedServiceType, decoratorType); var builder = new GenericTypeBuilder(requestedServiceType, decoratorType); decoratorType = builder.BuildClosedGenericImplementation().ClosedGenericImplementation; // decoratorType == null when type constraints don't match. if (decoratorType != null) { Requires.HasFactoryCreatedDecorator(this.data.Container, requestedServiceType, decoratorType); } } else { Requires.DecoratorFactoryReturnsATypeThatIsAssignableFromServiceType(decoratorType, requestedServiceType); } return decoratorType; }
private static bool DecorateByQueryType(DecoratorPredicateContext context, Type type) { return(context.ServiceType.GetGenericArguments()[0].GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == type)); }
public static DecoratorConditionalContext ToConditionalContext(this DecoratorPredicateContext context) => new DecoratorConditionalContext(context.ServiceType, context.ImplementationType, context.AppliedDecorators);