Ejemplo n.º 1
0
 /// <summary>
 /// Register Module
 /// </summary>
 /// <param name="services">services</param>
 /// <param name="module">service module</param>
 /// <returns>services</returns>
 public static IServiceContainerBuilder RegisterModule <TServiceModule>(this IServiceContainerBuilder services, TServiceModule module)
     where TServiceModule : IServiceContainerModule
 {
     Guard.NotNull(module, nameof(module));
     module.ConfigureServices(services);
     return(services);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// RegisterAssemblyTypes
        /// </summary>
        /// <param name="services">services</param>
        /// <param name="typesFilter">filter types to register</param>
        /// <param name="serviceLifetime">service lifetime</param>
        /// <param name="assemblies">assemblies</param>
        /// <returns>services</returns>
        public static IServiceContainerBuilder RegisterAssemblyTypes([NotNull] this IServiceContainerBuilder services, Func <Type, bool> typesFilter, ServiceLifetime serviceLifetime, params Assembly[] assemblies)
        {
            if (assemblies == null || assemblies.Length == 0)
            {
                assemblies = ReflectHelper.GetAssemblies();
            }

            var types = assemblies
                        .Select(assembly => assembly.GetTypes())
                        .SelectMany(t => t)
                        .Where(t => !t.IsAbstract)
            ;

            if (typesFilter != null)
            {
                types = types.Where(typesFilter);
            }

            foreach (var type in types)
            {
                services.Add(new ServiceDefinition(type, type, serviceLifetime));
            }

            return(services);
        }
Ejemplo n.º 3
0
        public static IServiceContainerBuilder RegisterAssemblyModules(
            [NotNull] this IServiceContainerBuilder serviceContainerBuilder, params Assembly[] assemblies)
        {
            if (null == assemblies || assemblies.Length == 0)
            {
                assemblies = ReflectHelper.GetAssemblies();
            }

            foreach (var type in assemblies.
                     SelectMany(ass => ass.GetExportedTypes())
                     .Where(t => t.IsClass && !t.IsAbstract && typeof(IServiceContainerModule).IsAssignableFrom(t))
                     )
            {
                try
                {
                    if (Activator.CreateInstance(type) is IServiceContainerModule module)
                    {
                        module.ConfigureServices(serviceContainerBuilder);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            return(serviceContainerBuilder);
        }
        public static IServiceContainerBuilder AddProxyService <TService>(this IServiceContainerBuilder serviceCollection, ServiceLifetime serviceLifetime)
            where TService : class
        {
            serviceCollection.Add(new ServiceDefinition(typeof(TService), sp =>
            {
                var proxyFactory = sp.ResolveRequiredService <IProxyFactory>();
                return(proxyFactory.CreateProxy <TService>());
            }, serviceLifetime));

            return(serviceCollection);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// RegisterTypeAsImplementedInterfaces
 /// </summary>
 /// <param name="services">services</param>
 /// <param name="type">type</param>
 /// <param name="serviceLifetime">service lifetime</param>
 /// <returns>services</returns>
 public static IServiceContainerBuilder RegisterTypeAsImplementedInterfaces([NotNull] this IServiceContainerBuilder services, Type type, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
 {
     if (type != null)
     {
         foreach (var interfaceType in type.GetImplementedInterfaces())
         {
             services.Add(new ServiceDefinition(interfaceType, type, serviceLifetime));
         }
     }
     return(services);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// RegisterTypeAsImplementedInterfaces
 /// </summary>
 /// <param name="services">services</param>
 /// <param name="type">type</param>
 /// <param name="interfaceTypeFilter">interfaceTypeFilter</param>
 /// <param name="serviceLifetime">service lifetime</param>
 /// <returns>services</returns>
 public static IServiceContainerBuilder RegisterTypeAsImplementedInterfaces(this IServiceContainerBuilder services, Type type, Func <Type, bool>?interfaceTypeFilter, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
 {
     Guard.NotNull(type);
     foreach (var interfaceType in type.GetImplementedInterfaces())
     {
         if (interfaceTypeFilter?.Invoke(interfaceType) != false)
         {
             services.Add(new ServiceDefinition(interfaceType, type, serviceLifetime));
         }
     }
     return(services);
 }
        public static IFluentAspectsServiceContainerBuilder AddFluentAspects(this IServiceContainerBuilder serviceCollection)
        {
            if (null == serviceCollection)
            {
                throw new ArgumentNullException(nameof(serviceCollection));
            }

            serviceCollection.AddTransient <IProxyTypeFactory, DefaultProxyTypeFactory>();
            serviceCollection.AddTransient <IProxyFactory, DefaultProxyFactory>();
            serviceCollection.AddSingleton(FluentConfigInterceptorResolver.Instance);

            return(new FluentAspectsServiceContainerBuilder(serviceCollection));
        }
 public static IFluentAspectsServiceContainerBuilder AddFluentAspects(this IServiceContainerBuilder serviceCollection, Action <FluentAspectOptions> optionsAction)
 {
     if (null == serviceCollection)
     {
         throw new ArgumentNullException(nameof(serviceCollection));
     }
     if (null == optionsAction)
     {
         throw new ArgumentNullException(nameof(optionsAction));
     }
     FluentAspects.Configure(optionsAction);
     return(AddFluentAspects(serviceCollection));
 }
        public static IServiceContainerBuilder RegisterAssemblyModules(
            [NotNull] this IServiceContainerBuilder serviceContainerBuilder, params Assembly[] assemblies)
        {
#if NET45
            if (null == assemblies || assemblies.Length == 0)
            {
                if (System.Web.Hosting.HostingEnvironment.IsHosted)
                {
                    assemblies = System.Web.Compilation.BuildManager.GetReferencedAssemblies()
                                 .Cast <Assembly>().ToArray();
                }
            }
#endif

            if (null == assemblies || assemblies.Length == 0)
            {
                assemblies = AppDomain.CurrentDomain.GetAssemblies();
            }

            foreach (var type in assemblies.WhereNotNull().SelectMany(ass => ass.GetTypes())
                     .Where(t => t.IsClass && !t.IsAbstract && typeof(IServiceContainerModule).IsAssignableFrom(t))
                     )
            {
                try
                {
                    if (Activator.CreateInstance(type) is ServiceContainerModule module)
                    {
                        module.ConfigureServices(serviceContainerBuilder);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
            return(serviceContainerBuilder);
        }
Ejemplo n.º 10
0
 /// <summary>
 /// RegisterTypeAsImplementedInterfaces
 /// </summary>
 /// <param name="services">services</param>
 /// <param name="type">type</param>
 /// <param name="serviceLifetime">service lifetime</param>
 /// <returns>services</returns>
 public static IServiceContainerBuilder RegisterTypeAsImplementedInterfaces(
     [NotNull] this IServiceContainerBuilder services, Type type,
     ServiceLifetime serviceLifetime = ServiceLifetime.Singleton)
 => RegisterTypeAsImplementedInterfaces(services, type, null, serviceLifetime);
Ejemplo n.º 11
0
 /// <summary>
 /// RegisterAssemblyTypes
 /// </summary>
 /// <param name="services">services</param>
 /// <param name="serviceLifetime">service lifetime</param>
 /// <param name="assemblies">assemblies</param>
 /// <returns>services</returns>
 public static IServiceContainerBuilder RegisterAssemblyTypes([NotNull] this IServiceContainerBuilder services,
                                                              ServiceLifetime serviceLifetime, params Assembly[] assemblies)
 => RegisterAssemblyTypes(services, null, serviceLifetime, assemblies);
Ejemplo n.º 12
0
 public ServiceContainerDependencyResolver(IServiceContainerBuilder serviceContainerBuilder) : this(serviceContainerBuilder.Build())
 {
 }
Ejemplo n.º 13
0
 public static void SetDependencyResolver([NotNull] IServiceContainerBuilder serviceContainerBuilder) => SetDependencyResolver(new ServiceContainerDependencyResolver(serviceContainerBuilder));
 public static IServiceContainerBuilder AddTransientProxy <TService>(this IServiceContainerBuilder serviceCollection)
     where TService : class =>
 serviceCollection.AddProxyService <TService>(ServiceLifetime.Transient);
        public static IServiceContainer BuildFluentAspectsContainer(this IServiceContainerBuilder serviceCollection,
                                                                    Action <FluentAspectOptions> optionsAction,
                                                                    Action <IFluentAspectsServiceContainerBuilder> aspectBuildAction = null,
                                                                    Expression <Func <Type, bool> > ignoreTypesFilter = null)
        {
            var services = new ServiceContainerBuilder();

            var aspectBuilder = null != optionsAction
                ? serviceCollection.AddFluentAspects(optionsAction)
                : serviceCollection.AddFluentAspects();

            aspectBuildAction?.Invoke(aspectBuilder);

            Expression <Func <Type, bool> > ignoreTypesExpression = t => "WeihanLi.Common.Aspect".Equals(t.Namespace);

            if (null != ignoreTypesFilter)
            {
                ignoreTypesExpression = ignoreTypesExpression.Or(ignoreTypesFilter);
            }

            var ignoreTypesPredicate = ignoreTypesExpression.Compile();

            using (var serviceProvider = serviceCollection.Build())
            {
                var proxyTypeFactory = serviceProvider.ResolveRequiredService <IProxyTypeFactory>();

                foreach (var descriptor in serviceCollection)
                {
                    if (descriptor.ServiceType.IsSealed ||
                        descriptor.ServiceType.IsNotPublic ||
                        descriptor.ServiceType.IsGenericTypeDefinition
                        )
                    {
                        services.Add(descriptor);
                        continue;
                    }

                    if (ignoreTypesPredicate(descriptor.ServiceType))
                    {
                        services.Add(descriptor);
                        continue;
                    }

                    if (descriptor.ImplementType != null)
                    {
                        if (descriptor.ImplementType.IsNotPublic ||
                            descriptor.ImplementType.IsProxyType()
                            )
                        {
                            services.Add(descriptor);
                            continue;
                        }

                        if (descriptor.ServiceType.IsClass &&
                            descriptor.ImplementType.IsSealed)
                        {
                            services.Add(descriptor);
                            continue;
                        }

                        if (descriptor.ServiceType.IsGenericTypeDefinition ||
                            descriptor.ImplementType.IsGenericTypeDefinition)
                        {
                            var proxyType = proxyTypeFactory.CreateProxyType(descriptor.ServiceType, descriptor.ImplementType);
                            services.Add(new ServiceDefinition(descriptor.ServiceType, proxyType,
                                                               descriptor.ServiceLifetime));
                            continue;
                        }
                    }

                    Func <IServiceProvider, object> serviceFactory = null;

                    if (descriptor.ImplementationInstance != null)
                    {
                        if (descriptor.ImplementationInstance.GetType().IsPublic)
                        {
                            serviceFactory = provider => provider.ResolveRequiredService <IProxyFactory>()
                                             .CreateProxyWithTarget(descriptor.ServiceType, descriptor.ImplementationInstance);
                        }
                    }
                    else if (descriptor.ImplementType != null)
                    {
                        serviceFactory = provider =>
                        {
                            var proxy = provider.ResolveRequiredService <IProxyFactory>()
                                        .CreateProxy(descriptor.ServiceType, descriptor.ImplementType);
                            return(proxy);
                        };
                    }
                    else if (descriptor.ImplementationFactory != null)
                    {
                        serviceFactory = provider =>
                        {
                            var implement = descriptor.ImplementationFactory(provider);
                            if (implement == null)
                            {
                                return(null);
                            }

                            var implementType = implement.GetType();
                            if (implementType.IsNotPublic ||
                                implementType.IsProxyType())
                            {
                                return(implement);
                            }

                            return(provider.ResolveRequiredService <IProxyFactory>()
                                   .CreateProxyWithTarget(descriptor.ServiceType, implement));
                        };
                    }

                    if (null != serviceFactory)
                    {
                        services.Add(new ServiceDefinition(descriptor.ServiceType, serviceFactory,
                                                           descriptor.ServiceLifetime));
                    }
                    else
                    {
                        services.Add(descriptor);
                    }
                }
            }

            var container = services.Build();

            DependencyResolver.SetDependencyResolver(container);
            return(container);
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Register Module
 /// </summary>
 /// <param name="services">services</param>
 /// <param name="module">service module</param>
 /// <returns>services</returns>
 public static IServiceContainerBuilder RegisterModule <TServiceModule>([NotNull] this IServiceContainerBuilder services, TServiceModule module)
     where TServiceModule : IServiceContainerModule
 {
     module?.ConfigureServices(services);
     return(services);
 }
Ejemplo n.º 17
0
    /// <summary>
    /// RegisterTypeAsImplementedInterfaces
    /// </summary>
    /// <param name="services">services</param>
    /// <param name="typesFilter">filter types to register</param>
    /// <param name="interfaceTypeFilter">filter interface types to register</param>
    /// <param name="serviceLifetime">service lifetime</param>
    /// <param name="assemblies">assemblies</param>
    /// <returns>services</returns>
    public static IServiceContainerBuilder RegisterAssemblyTypesAsImplementedInterfaces(this IServiceContainerBuilder services, Func <Type, bool>?typesFilter, Func <Type, bool>?interfaceTypeFilter, ServiceLifetime serviceLifetime, params Assembly[] assemblies)
    {
        Guard.NotNull(assemblies, nameof(assemblies));
        if (assemblies.Length == 0)
        {
            assemblies = ReflectHelper.GetAssemblies();
        }

        var types = assemblies
                    .Select(assembly => assembly.GetTypes())
                    .SelectMany(t => t)
                    .Where(t => !t.IsAbstract)
        ;

        if (typesFilter != null)
        {
            types = types.Where(typesFilter);
        }

        foreach (var type in types)
        {
            foreach (var implementedInterface in type.GetImplementedInterfaces())
            {
                if (interfaceTypeFilter?.Invoke(implementedInterface) != false)
                {
                    services.Add(new ServiceDefinition(implementedInterface, type, serviceLifetime));
                }
            }
        }

        return(services);
    }
Ejemplo n.º 18
0
 public static IServiceContainerBuilder AddSingleton <TService>([NotNull] this IServiceContainerBuilder serviceContainerBuilder, [NotNull] TService service)
 {
     serviceContainerBuilder.Add(new ServiceDefinition(service, typeof(TService)));
     return(serviceContainerBuilder);
 }
Ejemplo n.º 19
0
 public FluentAspectsServiceContainerBuilder(IServiceContainerBuilder serviceCollection)
 {
     Services = serviceCollection;
 }
Ejemplo n.º 20
0
 /// <summary>
 /// RegisterAssemblyTypes
 /// </summary>
 /// <param name="services">services</param>
 /// <param name="assemblies">assemblies</param>
 /// <returns>services</returns>
 public static IServiceContainerBuilder RegisterAssemblyTypes(this IServiceContainerBuilder services, params Assembly[] assemblies)
 => RegisterAssemblyTypes(services, null, ServiceLifetime.Singleton, assemblies);
Ejemplo n.º 21
0
 /// <summary>
 /// RegisterTypeAsImplementedInterfaces
 /// </summary>
 /// <param name="services">services</param>
 /// <param name="assemblies">assemblies</param>
 /// <returns>services</returns>
 public static IServiceContainerBuilder RegisterAssemblyTypesAsImplementedInterfaces(this IServiceContainerBuilder services,
                                                                                     params Assembly[] assemblies)
 => RegisterAssemblyTypesAsImplementedInterfaces(services, typesFilter: null, ServiceLifetime.Singleton, assemblies);
Ejemplo n.º 22
0
 public static IServiceContainerBuilder AddSingleton <TService>(this IServiceContainerBuilder serviceContainerBuilder, TService service)
 {
     Guard.NotNull(service, nameof(service));
     serviceContainerBuilder.Add(new ServiceDefinition(service !, typeof(TService)));
     return(serviceContainerBuilder);
 }
Ejemplo n.º 23
0
 /// <summary>
 /// RegisterTypeAsImplementedInterfaces
 /// </summary>
 /// <param name="services">services</param>
 /// <param name="serviceLifetime">service lifetime</param>
 /// <param name="assemblies">assemblies</param>
 /// <returns>services</returns>
 public static IServiceContainerBuilder RegisterAssemblyTypesAsImplementedInterfaces([NotNull] this IServiceContainerBuilder services,
                                                                                     ServiceLifetime serviceLifetime, params Assembly[] assemblies)
 => RegisterAssemblyTypesAsImplementedInterfaces(services, typesFilter: null, serviceLifetime, assemblies);
Ejemplo n.º 24
0
 public abstract void ConfigureServices(IServiceContainerBuilder serviceContainerBuilder);
 public static IServiceContainerBuilder AddTransientProxy <TService, TImplement>(this IServiceContainerBuilder serviceCollection)
     where TImplement : TService
     where TService : class
 {
     return(serviceCollection.AddProxyService <TService, TImplement>(ServiceLifetime.Transient));
 }
Ejemplo n.º 26
0
 /// <summary>
 /// RegisterTypeAsImplementedInterfaces
 /// </summary>
 /// <param name="services">services</param>
 /// <param name="typesFilter">filter types to register</param>
 /// <param name="serviceLifetime">service lifetime</param>
 /// <param name="assemblies">assemblies</param>
 /// <returns>services</returns>
 public static IServiceContainerBuilder RegisterAssemblyTypesAsImplementedInterfaces(
     this IServiceContainerBuilder services, Func <Type, bool>?typesFilter,
     ServiceLifetime serviceLifetime, params Assembly[] assemblies)
 => RegisterAssemblyTypesAsImplementedInterfaces(services, typesFilter, null, serviceLifetime, assemblies);
Ejemplo n.º 27
0
 /// <summary>
 /// RegisterAssemblyTypes
 /// </summary>
 /// <param name="services">services</param>
 /// <param name="typesFilter">filter types to register</param>
 /// <param name="assemblies">assemblies</param>
 /// <returns>services</returns>
 public static IServiceContainerBuilder RegisterAssemblyTypes([NotNull] this IServiceContainerBuilder services,
                                                              Func <Type, bool> typesFilter, params Assembly[] assemblies)
 => RegisterAssemblyTypes(services, typesFilter, ServiceLifetime.Singleton, assemblies);
 public static IServiceContainerBuilder AddScopedProxy <TService>(this IServiceContainerBuilder serviceCollection)
     where TService : class =>
 serviceCollection.AddProxyService <TService>(ServiceLifetime.Scoped);
 public override void ConfigureServices(IServiceContainerBuilder serviceContainerBuilder)
 {
     serviceContainerBuilder.AddSingleton <IIdGenerator>(GuidIdGenerator.Instance);
 }
Ejemplo n.º 30
0
        public static IServiceContainer BuildFluentAspectsContainer(this IServiceContainerBuilder serviceCollection,
                                                                    Action <FluentAspectOptions> optionsAction,
                                                                    Action <IFluentAspectsServiceContainerBuilder> aspectBuildAction = null,
                                                                    Func <Type, bool> ignoreTypesPredict = null)
        {
            var services = new ServiceContainerBuilder();

            var aspectBuilder = null != optionsAction
                ? services.AddFluentAspects(optionsAction)
                : services.AddFluentAspects();

            aspectBuildAction?.Invoke(aspectBuilder);

            foreach (var descriptor in serviceCollection)
            {
                if (ignoreTypesPredict?.Invoke(descriptor.ServiceType) == true)
                {
                    services.Add(descriptor);
                    continue;
                }

                if (descriptor.ServiceType.IsSealed ||
                    descriptor.ImplementType.IsProxyType() ||
                    (descriptor.ServiceType.IsClass && (descriptor.ImplementType?.IsSealed == true || descriptor.ImplementType?.IsPublic == false)))
                {
                    services.Add(descriptor);
                }
                else
                {
                    Func <IServiceProvider, object> serviceFactory = null;

                    if (descriptor.ImplementationInstance != null)
                    {
                        serviceFactory = provider => provider.ResolveRequiredService <IProxyFactory>()
                                         .CreateProxyWithTarget(descriptor.ServiceType, descriptor.ImplementationInstance);
                    }
                    else if (descriptor.ImplementationFactory != null)
                    {
                        serviceFactory = provider =>
                        {
                            var implement = descriptor.ImplementationFactory(provider);
                            if (implement?.GetType().IsProxyType() == true)
                            {
                                return(implement);
                            }
                            return(provider.ResolveRequiredService <IProxyFactory>()
                                   .CreateProxyWithTarget(descriptor.ServiceType, implement));
                        };
                    }
                    else if (descriptor.ImplementType != null)
                    {
                        serviceFactory = provider => provider.ResolveRequiredService <IProxyFactory>()
                                         .CreateProxy(descriptor.ServiceType, descriptor.ImplementType);
                    }

                    if (null != serviceFactory)
                    {
                        services.Add(new ServiceDefinition(descriptor.ServiceType, serviceFactory,
                                                           descriptor.ServiceLifetime));
                    }
                    else
                    {
                        services.Add(descriptor);
                    }
                }
            }

            var container = services.Build();

            DependencyResolver.SetDependencyResolver(container);
            return(container);
        }