예제 #1
0
        public ProxyGenerator(IAspectConfiguration configuration)
        {
            var asmBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(Constants.GeneratedNamespace), AssemblyBuilderAccess.RunAndCollect);

            moduleBuilder      = asmBuilder.DefineDynamicModule("core");
            interceptorCreator = new InterceptorCreator(configuration);
        }
예제 #2
0
 public static IAspectConfiguration EnablePolly(this IAspectConfiguration configuration)
 {
     configuration.NonPredicates.AddNamespace("Polly")
     .AddNamespace("Polly.*");
     configuration.GlobalInterceptors.Add(new PolicyInterceptor());
     return(configuration);
 }
        private static bool CanDecorate(ServiceRegistration registration, IAspectConfiguration aspectConfiguration)
        {
            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)) || _excepts.Any(x => implType.Namespace.Matches(x)))
            {
                return(false);
            }
            if (!serviceType.CanInherited() || serviceType.IsNonAspect())
            {
                return(false);
            }

            var aspectValidator = new AspectValidatorBuilder(aspectConfiguration).Build();

            if (!aspectValidator.Validate(serviceType, true) && !aspectValidator.Validate(implType, false))
            {
                return(false);
            }
            return(true);
        }
예제 #4
0
        public ServiceContainer(IEnumerable <ServiceDefinition> services, IAspectConfiguration aspectConfiguration)
        {
            _collection = new List <ServiceDefinition>();

            Singletons = new LifetimeServiceContainer(_collection, Lifetime.Singleton);
            Scopeds    = new LifetimeServiceContainer(_collection, Lifetime.Scoped);
            Transients = new LifetimeServiceContainer(_collection, Lifetime.Transient);

            if (services != null)
            {
                var configuration = services.LastOrDefault(x => x.ServiceType == typeof(IAspectConfiguration) && x is InstanceServiceDefinition);
                if (configuration != null)
                {
                    _configuration = (IAspectConfiguration)((InstanceServiceDefinition)configuration).ImplementationInstance;
                }
                foreach (var service in services)
                {
                    _collection.Add(service);
                }
            }

            if (aspectConfiguration != null)
            {
                _configuration = aspectConfiguration;
            }

            if (_configuration == null)
            {
                _configuration = new AspectConfiguration();
            }

            AddInternalServices();
        }
예제 #5
0
 protected override void Configure(IAspectConfiguration configuration)
 {
     configuration.Interceptors.AddDelegate((ctx, next) =>
     {
         return(next(ctx));
     });
     configuration.NonAspectPredicates.Add(m => m.DeclaringType == typeof(IFakeClass2));
 }
예제 #6
0
        public AspectValidatorBuilder(IAspectConfiguration aspectConfiguration)
        {
            _collections = new List <Func <AspectValidationDelegate, AspectValidationDelegate> >();

            foreach (var handler in aspectConfiguration.ValidationHandlers.OrderBy(x => x.Order))
            {
                _collections.Add(next => method => handler.Invoke(method, next));
            }
        }
 public static IAspectConfiguration AddMethodExecuteLogger(this IAspectConfiguration configuration, params AspectPredicate[] predicates)
 {
     if (configuration == null)
     {
         throw new ArgumentNullException(nameof(configuration));
     }
     configuration.Interceptors.AddTyped <MethodExecuteLoggerInterceptor>(predicates);
     return(configuration);
 }
예제 #8
0
 protected override void Configure(IAspectConfiguration configuration)
 {
     base.Configure(configuration);
     configuration.Interceptors.AddDelegate(async(ctx, next) =>
     {
         await ctx.Invoke(next);
         ctx.ReturnValue = "lemon";
     });
 }
예제 #9
0
        public ServiceTable(IAspectConfiguration configuration)
        {
            var aspectValidatorBuilder = new AspectValidatorBuilder(configuration);

            _proxyTypeGenerator              = new ProxyTypeGenerator(aspectValidatorBuilder);
            _serviceValidator                = new ServiceValidator(aspectValidatorBuilder);
            _linkedServiceDefinitions        = new ConcurrentDictionary <Type, LinkedList <ServiceDefinition> >();
            _linkedGenericServiceDefinitions = new ConcurrentDictionary <Type, LinkedList <ServiceDefinition> >();
        }
예제 #10
0
 public static IAspectConfiguration EnableParameterAspect(this IAspectConfiguration configuration, params AspectPredicate[] predicates)
 {
     if (configuration == null)
     {
         throw new ArgumentNullException(nameof(configuration));
     }
     configuration.Interceptors.AddTyped <EnableParameterAspectInterceptor>(predicates);
     return(configuration);
 }
        private static DecoratorRegistration CreateDecorator(this IAspectConfiguration aspectConfiguration)
        {
            var reg = new DecoratorRegistration()
            {
                CanDecorate             = s => CanDecorate(s, aspectConfiguration),
                ImplementingTypeFactory = CreateProxyType
            };

            return(reg);
        }
예제 #12
0
 protected override void Configure(IAspectConfiguration configuration)
 {
     configuration.Interceptors.AddDelegate((ctx, next) => next(ctx), Predicates.ForService("IService"));
     configuration.Interceptors.AddDelegate(async(ctx, next) =>
     {
         await next(ctx);
         ctx.ReturnValue = "CreateClassProxy";
     }
                                            , Predicates.ForService("*BaseService"));
 }
예제 #13
0
 public static IAspectConfiguration EnableMemoryCache(this IAspectConfiguration configuration)
 {
     configuration.GlobalInterceptors.TryAdd(() => new CacheInterceptor());
     configuration.ConfigServices.Add(services =>
     {
         services.AddMemoryCache();
         services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(ICacheAdapter), typeof(MemoryCacheAdapter)));
         services.TryAddSingleton(typeof(ICacheProvider <>), typeof(CacheProvider <>));
     });
     return(configuration);
 }
        public static IKernel AddAspectCoreFacility(this IKernel Kernel, IAspectConfiguration configuration, Action <IAspectConfiguration> configure = null)
        {
            if (Kernel == null)
            {
                throw new ArgumentNullException(nameof(Kernel));
            }
            var config = configuration ?? new AspectConfiguration();

            configure?.Invoke(config);
            Kernel.AddFacility(new AspectCoreFacility(config));
            return(Kernel);
        }
예제 #15
0
        /// <summary>
        /// 添加全局aop拦截器
        /// </summary>
        /// <param name="aspectConfiguration"></param>
        /// <param name="configuration"></param>
        public static void AddInterceptor(this IAspectConfiguration aspectConfiguration, IConfiguration configuration)
        {
            var enableAopCache = Convert.ToBoolean(configuration["EnableAopCache"]);
            var enableAopLog   = Convert.ToBoolean(configuration["EnableAopLog"]);

            if (enableAopLog)
            {
                aspectConfiguration.Interceptors.AddTyped <LogInterceptor>(Predicates.Implement(typeof(IBaseService)));
            }
            if (enableAopCache)
            {
                aspectConfiguration.Interceptors.AddTyped <CacheInterceptor>(method => method.IsDefined(typeof(CachingAttribute)));
            }

            //aspectConfiguration.Interceptors.AddTyped<ControllerActionInterceptor>(Predicates.Implement(typeof(ControllerBase)));
        }
        public static IServiceContainer RegisterDynamicProxy(this IServiceContainer container,
                                                             IAspectConfiguration aspectConfig, Action <IAspectConfiguration> configure = null)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }
            aspectConfig = aspectConfig ?? new AspectConfiguration();

            foreach (var m in _nonAspect)
            {
                aspectConfig.NonAspectPredicates.AddNamespace(m);
            }

            configure?.Invoke(aspectConfig);

            container.AddSingleton <IServiceFactory>(container);
            container.AddSingleton <IServiceContainer>(container);

            container.AddSingleton <IAspectConfiguration>(aspectConfig)
            .AddTransient(typeof(IManyEnumerable <>), typeof(ManyEnumerable <>))
            .AddSingleton <IServiceProvider, LightInjectServiceResolver>()
            .AddSingleton <IServiceResolver, LightInjectServiceResolver>()
            .AddSingleton <IScopeResolverFactory, LightInjectScopeResolverFactory>()
            .AddSingleton <IAspectContextFactory, AspectContextFactory>()
            .AddSingleton <IAspectActivatorFactory, AspectActivatorFactory>()
            .AddSingleton <IProxyGenerator, ProxyGenerator>()
            .AddSingleton <IParameterInterceptorSelector, ParameterInterceptorSelector>()
            .AddSingleton <IPropertyInjectorFactory, PropertyInjectorFactory>()
            .AddSingleton <IInterceptorCollector, InterceptorCollector>()
            .AddSingleton <IInterceptorSelector, ConfigureInterceptorSelector>(nameof(ConfigureInterceptorSelector))
            .AddSingleton <IInterceptorSelector, AttributeInterceptorSelector>(nameof(AttributeInterceptorSelector))    // To register multiple services, you should set a name for each implement type.
            .AddSingleton <IAdditionalInterceptorSelector, AttributeAdditionalInterceptorSelector>()
            .AddSingleton <IAspectValidatorBuilder, AspectValidatorBuilder>()
            .AddSingleton <IAspectBuilderFactory, AspectBuilderFactory>()
            .AddSingleton <IProxyTypeGenerator, ProxyTypeGenerator>()
            .AddSingleton <IAspectCachingProvider, AspectCachingProvider>()
            .AddSingleton <IAspectExceptionWrapper, AspectExceptionWrapper>();

            var aspectValidator = new AspectValidatorBuilder(aspectConfig).Build();

            container.Decorate(aspectValidator.CreateDecorator());

            return(container);
        }
 public static void ConfigureRedisProfiler(this IAspectConfiguration configuration)
 {
     if (configuration == null)
     {
         throw new ArgumentNullException(nameof(configuration));
     }
     configuration.Interceptors.AddTyped <RedisProfilingInterceptor>(
         Predicates.ForService(typeof(IRedis).FullName),
         Predicates.ForService(typeof(IRedisAsync).FullName),
         Predicates.ForService(typeof(IDatabase).FullName),
         Predicates.ForService(typeof(IDatabaseAsync).FullName),
         Predicates.ForService(typeof(IServer).FullName),
         Predicates.ForService(typeof(ISubscriber).FullName));
     configuration.Interceptors.AddTyped <DatabaseProxyInterceptor>(
         Predicates.ForMethod(typeof(IConnectionMultiplexer).FullName, nameof(IConnectionMultiplexer.GetDatabase)));
     configuration.Interceptors.AddTyped <ServerProxyInterceptor>(
         Predicates.ForMethod(typeof(IConnectionMultiplexer).FullName, nameof(IConnectionMultiplexer.GetServer)));
     configuration.Interceptors.AddTyped <SubscriberProxyInterceptor>(
         Predicates.ForMethod(typeof(IConnectionMultiplexer).FullName, nameof(IConnectionMultiplexer.GetSubscriber)));
 }
        public static IServiceContainer RegisterDynamicProxy(this IServiceContainer container,
                                                             IAspectConfiguration aspectConfig       = null,
                                                             Action <IAspectConfiguration> configure = null)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }
            aspectConfig = aspectConfig ?? new AspectConfiguration();

            foreach (var m in _nonAspect)
            {
                aspectConfig.NonAspectPredicates.AddNamespace(m);
            }

            configure?.Invoke(aspectConfig);

            container.RegisterInstance <IAspectConfiguration>(aspectConfig)
            .Register(typeof(IManyEnumerable <>), typeof(ManyEnumerable <>))
            .RegisterInstance <IServiceContainer>(container)
            .Register <IServiceProvider, LightInjectServiceResolver>()
            .Register <IServiceResolver, LightInjectServiceResolver>()
            .Register <IAspectContextFactory, AspectContextFactory>()
            .Register <IAspectActivatorFactory, AspectActivatorFactory>()
            .Register <IProxyGenerator, ProxyGenerator>()
            .Register <IParameterInterceptorSelector, ParameterInterceptorSelector>()
            .Register <IPropertyInjectorFactory, PropertyInjectorFactory>()
            .Register <IInterceptorCollector, InterceptorCollector>()
            .Register <IInterceptorSelector, ConfigureInterceptorSelector>()
            .Register <IInterceptorSelector, AttributeInterceptorSelector>()
            .Register <IAdditionalInterceptorSelector, AttributeAdditionalInterceptorSelector>()
            .Register <IAspectValidatorBuilder, AspectValidatorBuilder>()
            .Register <IAspectBuilderFactory, AspectBuilderFactory>()
            .Register <IProxyTypeGenerator, ProxyTypeGenerator>()
            .Register <IAspectCachingProvider, AspectCachingProvider>()
            .Register <IAspectExceptionWrapper, AspectExceptionWrapper>();

            container.Decorate(aspectConfig.CreateDecorator());

            return(container);
        }
 public static IAspectConfiguration EnableHttpClient(this IAspectConfiguration configuration)
 {
     configuration.GlobalInterceptors.Add(new HttpClientInterceptor());
     configuration.ConfigServices.Add(services =>
     {
         services.AddHttpClient();
         services.TryAddSingleton <IHttpClientFactoryHandler, HttpClientFactoryHandler>();
         services.TryAddSingleton <IHttpRequestDynamicPathFactory, ConfigurationDynamicPathFactory>();
         services.TryAddSingleton <IQueryStringBuilder, QueryStringBuilder>();
         services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IHttpContentSerializer), typeof(SystemTextJsonContentSerializer)));
         services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IHttpContentSerializer), typeof(OctetStreamContentSerializer)));
         services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IHttpClientHandler), typeof(EnsureSuccessStatusCodeHandler)));
         services.PostConfigure <JsonSerializerOptions>(i =>
         {
             i.PropertyNameCaseInsensitive = true;
             i.PropertyNamingPolicy        = JsonNamingPolicy.CamelCase;
         });
         services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IHttpContentSerializer), typeof(XmlContentSerializer)));
         services.PostConfigure <XmlContentSerializerOptions>(i => { });
     });
     return(configuration);
 }
예제 #20
0
 public AspectCoreFacility(IAspectConfiguration aspectConfiguration)
 {
     _aspectConfiguration = aspectConfiguration ?? new AspectConfiguration();
 }
예제 #21
0
 public ServiceContainer(IAspectConfiguration aspectConfiguration)
     : this(null, aspectConfiguration)
 {
 }
 public ConfigureAspectValidationHandler(IAspectConfiguration aspectConfiguration)
 {
     _aspectConfiguration = aspectConfiguration ?? throw new ArgumentNullException(nameof(aspectConfiguration));
 }
        public static ContainerBuilder RegisterDynamicProxy(this ContainerBuilder containerBuilder, IAspectConfiguration configuration, Action <IAspectConfiguration> configure = null)
        {
            if (containerBuilder == null)
            {
                throw new ArgumentNullException(nameof(containerBuilder));
            }
            configuration = configuration ?? new AspectConfiguration();

            configuration.NonAspectPredicates.
            AddNamespace("Autofac").
            AddNamespace("Autofac.*");

            configure?.Invoke(configuration);

            containerBuilder.RegisterInstance <IAspectConfiguration>(configuration).SingleInstance();
            containerBuilder.RegisterGeneric(typeof(ManyEnumerable <>)).As(typeof(IManyEnumerable <>)).InstancePerDependency();
            containerBuilder.RegisterType <AutofacServiceResolver>().As <IServiceProvider, IServiceResolver>().InstancePerLifetimeScope();
            containerBuilder.RegisterType <AutofacScopeResolverFactory>().As <IScopeResolverFactory>().InstancePerLifetimeScope();
            containerBuilder.RegisterType <AspectContextFactory>().As <IAspectContextFactory>().InstancePerLifetimeScope();
            containerBuilder.RegisterType <AspectActivatorFactory>().As <IAspectActivatorFactory>().InstancePerLifetimeScope();
            containerBuilder.RegisterType <ProxyGenerator>().As <IProxyGenerator>().InstancePerLifetimeScope();
            containerBuilder.RegisterType <ParameterInterceptorSelector>().As <IParameterInterceptorSelector>().InstancePerLifetimeScope();
            containerBuilder.RegisterType <PropertyInjectorFactory>().As <IPropertyInjectorFactory>().InstancePerLifetimeScope();

            containerBuilder.RegisterType <InterceptorCollector>().As <IInterceptorCollector>().SingleInstance();
            containerBuilder.RegisterType <ConfigureInterceptorSelector>().As <IInterceptorSelector>().SingleInstance();
            containerBuilder.RegisterType <AttributeInterceptorSelector>().As <IInterceptorSelector>().SingleInstance();
            containerBuilder.RegisterType <AttributeAdditionalInterceptorSelector>().As <IAdditionalInterceptorSelector>().SingleInstance();
            containerBuilder.RegisterType <AspectValidatorBuilder>().As <IAspectValidatorBuilder>().SingleInstance();
            containerBuilder.RegisterType <AspectBuilderFactory>().As <IAspectBuilderFactory>().SingleInstance();
            containerBuilder.RegisterType <ProxyTypeGenerator>().As <IProxyTypeGenerator>().SingleInstance();
            containerBuilder.RegisterType <AspectCachingProvider>().As <IAspectCachingProvider>().SingleInstance();
            containerBuilder.RegisterType <AspectExceptionWrapper>().As <IAspectExceptionWrapper>().SingleInstance();

            containerBuilder.RegisterCallback(registry =>
            {
                foreach (var registration in registry.Registrations)
                {
                    registration.Activating += ComponentRegistration_Activating;
                }
                registry.Registered += Registry_Registered;
            });

            return(containerBuilder);
        }
예제 #24
0
 internal static AspectValidationHandlerCollection AddDefault(this AspectValidationHandlerCollection aspectValidationHandlers, IAspectConfiguration configuration)
 {
     aspectValidationHandlers.Add(new OverwriteAspectValidationHandler());
     aspectValidationHandlers.Add(new AttributeAspectValidationHandler());
     aspectValidationHandlers.Add(new CacheAspectValidationHandler());
     aspectValidationHandlers.Add(new ConfigureAspectValidationHandler(configuration));
     return(aspectValidationHandlers);
 }
예제 #25
0
 public InterceptorCreator(IAspectConfiguration configuration)
 {
     IsNonAspectType    = configuration.NonPredicates.BuildNonAspectTypePredicate();
     IsNonAspectMethod  = configuration.NonPredicates.BuildNonAspectMethodPredicate();
     this.configuration = configuration;
 }
예제 #26
0
 public ConfigureInterceptorSelector(IAspectConfiguration aspectConfiguration, IServiceProvider serviceProvider)
 {
     _aspectConfiguration = aspectConfiguration ?? throw new ArgumentNullException(nameof(aspectConfiguration));
     _serviceProvider     = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
 }
 protected override void Configure(IAspectConfiguration configuration)
 {
     configuration.EnableParameterAspect();
 }
 public static IWindsorContainer AddAspectCoreFacility(this IWindsorContainer windsorContainer, IAspectConfiguration configuration, Action <IAspectConfiguration> configure = null)
 {
     if (windsorContainer == null)
     {
         throw new ArgumentNullException(nameof(windsorContainer));
     }
     AddAspectCoreFacility(windsorContainer.Kernel, configuration, configure);
     return(windsorContainer);
 }
예제 #29
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SnapFluentConfiguration"/> class.
 /// </summary>
 /// <param name="config">The config.</param>
 public SnapFluentConfiguration(IAspectConfiguration config)
 {
     _configuration = config;
 }
 public ProxyGeneratorBuilder()
 {
     _configuration  = new AspectConfiguration();
     _serviceContext = new ServiceContext(_configuration);
 }
 protected override void Configure(IAspectConfiguration configuration)
 {
     configuration.Interceptors.AddTyped <Intercept1>(Predicates.ForNameSpace("*"));
     configuration.Interceptors.AddTyped <Intercept2>();
 }