public AspectCoreInterceptor(IAspectBuilderFactory aspectBuilderFactory,
                              IAspectContextFactory aspectContextFactory, IAspectValidatorBuilder aspectValidatorBuilder)
 {
     _aspectBuilderFactory = aspectBuilderFactory ?? throw new ArgumentNullException(nameof(aspectBuilderFactory));
     _aspectContextFactory = aspectContextFactory ?? throw new ArgumentNullException(nameof(aspectContextFactory));
     _aspectValidator      = aspectValidatorBuilder?.Build() ?? throw new ArgumentNullException(nameof(aspectValidatorBuilder));
 }
        private static bool Validate(ServiceDescriptor descriptor, IAspectValidator aspectValidator)
        {
            var implementationType = descriptor.ImplementationType;

            if (implementationType == null)
            {
                return(false);
            }

            if (!aspectValidator.Validate(descriptor.ServiceType))
            {
                return(false);
            }

            if (implementationType.GetTypeInfo().IsDynamically())
            {
                return(false);
            }

            if (!implementationType.GetTypeInfo().CanInherited())
            {
                return(false);
            }

            return(true);
        }
        private static bool CanDecorate(ServiceRegistration registration, IAspectValidator aspectValidator)
        {
            var serviceType = registration.ServiceType.GetTypeInfo();
            var implType    = registration.GetImplType().GetTypeInfo();

            if (implType.IsProxy() || !implType.CanInherited())
            {
                return(false);
            }
            if (_excepts.Any(x => implType.Name.Matches(x)) ||
                implType.Namespace != null && _excepts.Any(x => implType.Namespace.Matches(x)))
            {
                return(false);
            }
            if (!serviceType.CanInherited() || serviceType.IsNonAspect())
            {
                return(false);
            }

            if (!aspectValidator.Validate(serviceType, true) && !aspectValidator.Validate(implType, false))
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 4
0
 public ProxyTypeGenerator(IAspectValidatorBuilder aspectValidatorBuilder)
 {
     if (aspectValidatorBuilder == null)
     {
         throw new ArgumentNullException(nameof(aspectValidatorBuilder));
     }
     _aspectValidator = aspectValidatorBuilder.Build();
 }
Ejemplo n.º 5
0
 public ProxyGenerator(IAspectValidator aspectValidator)
 {
     if (aspectValidator == null)
     {
         throw new ArgumentNullException(nameof(aspectValidator));
     }
     this.aspectValidator = aspectValidator;
 }
 public DynamicProxyInterceptor(IAspectBuilderFactory aspectBuilderFactory,
                                IAspectContextFactory aspectContextFactory, IAspectValidatorBuilder aspectValidatorBuilder,
                                IAspectExceptionWrapper aspectExceptionWrapper)
 {
     _aspectBuilderFactory   = aspectBuilderFactory ?? throw new ArgumentNullException(nameof(aspectBuilderFactory));
     _aspectContextFactory   = aspectContextFactory ?? throw new ArgumentNullException(nameof(aspectContextFactory));
     _aspectValidator        = aspectValidatorBuilder?.Build() ?? throw new ArgumentNullException(nameof(aspectValidatorBuilder));
     _aspectExceptionWrapper = aspectExceptionWrapper ?? throw new ArgumentNullException(nameof(aspectExceptionWrapper));
 }
        private static DecoratorRegistration CreateDecorator(this IAspectValidator aspectValidator)
        {
            var registration = new DecoratorRegistration()
            {
                CanDecorate             = s => CanDecorate(s, aspectValidator),
                ImplementingTypeFactory = CreateProxyType
            };

            return(registration);
        }
Ejemplo n.º 8
0
        public static bool Validate(this IAspectValidator aspectValidator, Type type, bool isStrictValidation)
        {
            if (aspectValidator == null)
            {
                throw new ArgumentNullException(nameof(aspectValidator));
            }
            if (type == null)
            {
                return(false);
            }
            var typeInfo = type.GetTypeInfo();


            if (typeInfo.IsNonAspect() ||
                typeInfo.IsProxyType() ||
                typeInfo.IsValueType ||
                typeInfo.IsEnum ||
                !typeInfo.IsVisible())
            {
                return(false);
            }

            if (typeInfo.IsClass && !typeInfo.CanInherited())
            {
                return(false);
            }

            foreach (var method in typeInfo.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
            {
                if (aspectValidator.Validate(method, isStrictValidation))
                {
                    return(true);
                }
            }

            foreach (var interfaceType in typeInfo.GetInterfaces())
            {
                if (aspectValidator.Validate(interfaceType, isStrictValidation))
                {
                    return(true);
                }
            }

            var baseType = typeInfo.BaseType;

            if (baseType == null || baseType == typeof(object))
            {
                return(false);
            }

            return(aspectValidator.Validate(baseType, isStrictValidation));
        }
Ejemplo n.º 9
0
        public static bool Validate(this IAspectValidator aspectValidator, PropertyInfo property)
        {
            if (aspectValidator == null)
            {
                throw new ArgumentNullException(nameof(aspectValidator));
            }
            if (property == null)
            {
                throw new ArgumentNullException(nameof(property));
            }

            return((property.CanRead && aspectValidator.Validate(property.GetGetMethod())) || (property.CanWrite && aspectValidator.Validate(property.GetSetMethod())));
        }
Ejemplo n.º 10
0
        public static bool Validate(this IAspectValidator aspectValidator, Type type)
        {
            if (aspectValidator == null)
            {
                throw new ArgumentNullException(nameof(aspectValidator));
            }
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            return(Validate(aspectValidator, type.GetTypeInfo()));
        }
Ejemplo n.º 11
0
        public static bool Validate(this IAspectValidator aspectValidator, Type typeInfo)
        {
            if (aspectValidator == null)
            {
                throw new ArgumentNullException(nameof(aspectValidator));
            }
            if (typeInfo == null)
            {
                throw new ArgumentNullException(nameof(typeInfo));
            }
            if (typeInfo.IsValueType)
            {
                return(false);
            }

            return(typeInfo.GetMethods().Any(method => aspectValidator.Validate(method)));
        }
Ejemplo n.º 12
0
        public ProxyTypeGenerator(Type serviceType, IAspectValidator aspectValidator) : base(ModuleGenerator.Default.ModuleBuilder)
        {
            if (serviceType == null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }
            if (aspectValidator == null)
            {
                throw new InvalidOperationException("No service for type 'AspectCore.Abstractions.IAspectValidator' has been registered.");
            }
            if (!serviceType.IsAccessibility())
            {
                throw new InvalidOperationException($"Validate '{serviceType}' failed because the type does not satisfy the conditions of the generate proxy class.");
            }

            ServiceType     = serviceType;
            AspectValidator = aspectValidator;
        }
Ejemplo n.º 13
0
 public ClassProxyTypeGenerator(Type serviceType, Type parentType, Type[] interfaces, IAspectValidator aspectValidator) : base(serviceType, aspectValidator)
 {
     if (!parentType.CanInherited())
     {
         throw new InvalidOperationException($"Validate '{parentType}' failed because the type does not satisfy the condition to be inherited.");
     }
     ParentType = parentType;
     Interfaces = interfaces?.Distinct().Where(i => aspectValidator.Validate(i))?.ToArray() ?? Type.EmptyTypes;
 }
Ejemplo n.º 14
0
 public InterfaceProxyTypeGenerator(Type serviceType, Type parentType, Type[] interfaces, IAspectValidator aspectValidator) : base(serviceType, aspectValidator)
 {
     if (!serviceType.GetTypeInfo().IsInterface)
     {
         throw new ArgumentException($"Type '{serviceType}' should be interface.", nameof(serviceType));
     }
     if (parentType == null)
     {
         throw new ArgumentNullException(nameof(parentType));
     }
     Interfaces      = new Type[] { serviceType }.Concat(interfaces ?? Type.EmptyTypes).ToArray();
     this.parentType = parentType;
 }
Ejemplo n.º 15
0
 internal ServiceValidator(IAspectValidatorBuilder aspectValidatorBuilder)
 {
     _aspectValidator = aspectValidatorBuilder.Build();
 }