Beispiel #1
0
        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);
        }
Beispiel #2
0
 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));
        }
Beispiel #4
0
        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));
        }
Beispiel #5
0
        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));
 }
Beispiel #8
0
 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;
        }
 protected bool SatisfiesPredicate(DecoratorPredicateContext context) =>
     this.Predicate == null || this.Predicate(context);
Beispiel #11
0
 private static bool DecorateByQueryType(DecoratorPredicateContext context, Type type)
 {
     return(context.ServiceType.GetGenericArguments()[0].GetInterfaces().Any(i =>
                                                                             i.IsGenericType &&
                                                                             i.GetGenericTypeDefinition() == type));
 }
Beispiel #12
0
 public static DecoratorConditionalContext ToConditionalContext(this DecoratorPredicateContext context)
 => new DecoratorConditionalContext(context.ServiceType, context.ImplementationType, context.AppliedDecorators);