コード例 #1
0
 /// <summary>
 /// Scan assembly for attributed Autofac services.
 /// </summary>
 /// <param name="assembly"><see cref="System.Reflection.Assembly"/> to scam</param>
 /// <param name="builder"><see cref="global::Autofac.ContainerBuilder"/> to add services to</param>
 /// <param name="appBlocksContainerBuilder"><see cref="AppBlocksContainerBuilder"/> for the AppBlocks application</param>
 protected static void RegisterAssembly(
     System.Reflection.Assembly assembly,
     ContainerBuilder builder,
     AppBlocksContainerBuilder appBlocksContainerBuilder)
 {
     // Register attributed services in assembly
     RegistrationUtils.RegisterAssembly
         (assembly, builder, appBlocksContainerBuilder);
 }
コード例 #2
0
        internal static void RegisterAssembly(Assembly assembly,
                                              ContainerBuilder builder,
                                              AppBlocksContainerBuilder appBlocksContainerBuilder)
        {
            // Scan assembly for services to be registered as interfaces
            RegisterAsInterfaces(builder, assembly, appBlocksContainerBuilder);

            // Scan assembly for named services
            RegisterNamedServices(builder, assembly, appBlocksContainerBuilder);

            // Scan assembly for keyed services
            RegisterKeyedServices(builder, assembly, appBlocksContainerBuilder);

            // Scan assembly for MediatR request services
            RegisterMediatrRequestServices(builder, assembly, appBlocksContainerBuilder);

            // Scan assembly for MediatR notification services
            RegisterMediatrNotificationServices(builder, assembly, appBlocksContainerBuilder);
        }
コード例 #3
0
        /// <summary>
        /// Registers types attributed with AutofacServiceAttribute as interfaces.
        /// </summary>
        /// <param name="builder">Services Container builder</param>
        /// <param name="assembly">Assembly to process</param>
        /// <param name="appBlocksContainerBuilder"><see cref="AppBlocksContainerBuilder"/> instance for this application</param>
        private static void RegisterAsInterfaces(
            ContainerBuilder builder,
            Assembly assembly,
            AppBlocksContainerBuilder appBlocksContainerBuilder)
        {
            // local method to filter in services to be registered with implemented
            // interfaces
            bool isAnonymousService(Type t)
            {
                // Look for AppBlocks service attribute on a type
                var serviceAttributes = t.GetCustomAttributes(typeof(AppBlocksServiceAttribute), true);

                // Attribute was not found, return.
                if (serviceAttributes.Length == 0)
                {
                    return(false);
                }

                // Get reference to attribute
                var serviceAttribute = (AppBlocksServiceAttribute)serviceAttributes[0];

                // Return if service is to be registered as a named or keyed service
                if (!string.IsNullOrEmpty(serviceAttribute.Name) || serviceAttribute.IsKeyed)
                {
                    return(false);
                }

                // Exclude live service if current application mode is test
                if (appBlocksContainerBuilder.ApplicationMode == AppBlocksApplicationMode.Test &&
                    serviceAttribute.ServiceDependencyType == AppBlocksServiceDependencyType.Live)
                {
                    return(false);
                }

                // Ask if AppBlocksContainerBuilder concrete implementation wants to exclude the service from registration
                return(appBlocksContainerBuilder.ShouldRegisterService(t, serviceAttribute));
            }

            // Search all assembly types for services to register
            assembly.GetTypes()
            .Where(t => isAnonymousService(t))
            .Select(t =>
                    // Create anonymous type
                    new
            {
                TypeInformation      = t,
                AttributeInformation = (AppBlocksServiceAttribute)t
                                       .GetCustomAttribute(typeof(AppBlocksServiceAttribute))
            })
            .ToList()
            .ForEach(i =>
            {
                // Perform attribute validations
                ValidateRegistration(i.AttributeInformation);

                if (logger.IsEnabled(LogLevel.Debug))
                {
                    logger.LogDebug($"Registering {i.TypeInformation.FullName} as implemented interfaces");
                }

                // Register type with Autofac container builder.
                var registration = builder.RegisterType(i.TypeInformation).AsImplementedInterfaces();

                // Set type lifetime scope
                SetTypeLifetimeScope(i.AttributeInformation, registration);

                // Add type to interceptors based on attribute information
                AddTypeInterceptors(i.AttributeInformation, registration);
            });
        }
コード例 #4
0
        /// <summary>
        /// Register types to be registered as MediatR notification services
        /// </summary>
        private static void RegisterMediatrNotificationServices(
            ContainerBuilder builder,
            Assembly assembly,
            AppBlocksContainerBuilder appBlocksContainerBuilder)
        {
            // local method to filter in services to be registered as MediatR notification handlers
            bool isMediatrRequestService(Type t)
            {
                // Look for AppBlocks MediatR notification service attribute
                var serviceAttributes = t.GetCustomAttributes(typeof(AppBlocksMediatrNotificationServiceAttribute), true);

                // Attribute was not found, return
                if (serviceAttributes.Length == 0)
                {
                    return(false);
                }

                // Get reference to attribute
                var serviceAttribute = (AppBlocksMediatrNotificationServiceAttribute)serviceAttributes[0];

                // Exclude live service if current application mode is test
                if (appBlocksContainerBuilder.ApplicationMode == AppBlocksApplicationMode.Test &&
                    serviceAttribute.ServiceDependencyType == AppBlocksServiceDependencyType.Live)
                {
                    return(false);
                }

                // Ask if AppBlocksContainerBuilder concrete implementation wants to exclude the service from registration
                return(appBlocksContainerBuilder.ShouldRegisterService(t, serviceAttribute));
            }

            // Search all assembly types for services to register
            assembly.GetTypes()
            .Where(t => isMediatrRequestService(t))
            .Select(t =>
                    // Create anonymous type
                    new
            {
                TypeInformation      = t,
                AttributeInformation = (AppBlocksMediatrNotificationServiceAttribute)t
                                       .GetCustomAttribute(typeof(AppBlocksMediatrNotificationServiceAttribute))
            })
            .ToList()
            .ForEach(i =>
            {
                // Make sure type is a valid MediatR.INotificationHandler
                if (!i.TypeInformation.IsClosedTypeOf(typeof(INotificationHandler <>)))
                {
                    throw new Exception($"{i.TypeInformation.FullName} must closed type of " +
                                        $"Mediatr.INotificationHandler<>");
                }

                if (logger.IsEnabled(LogLevel.Debug))
                {
                    logger.LogDebug($"Registering {i.TypeInformation.FullName} as Mediatr Notification Handler");
                }

                // Register type with implemented interfaces
                var registration = builder
                                   .RegisterType(i.TypeInformation)
                                   .AsImplementedInterfaces();

                // Set type lifetime scope
                SetTypeLifetimeScope(i.AttributeInformation, registration);
            });
        }
コード例 #5
0
        /// <summary>
        /// Register types to be registered as Keyed services
        /// </summary>
        private static void RegisterKeyedServices(
            ContainerBuilder builder,
            Assembly assembly,
            AppBlocksContainerBuilder appBlocksContainerBuilder)
        {
            // local method to filter in services to be registered as keyed services
            bool isKeyedService(Type t)
            {
                // Look for AppBlocks service attribute on a type
                var serviceAttributes = t.GetCustomAttributes(typeof(AppBlocksServiceAttribute), true);

                // Attribute was not found, return
                if (serviceAttributes.Length == 0)
                {
                    return(false);
                }

                // Get reference to attribute
                var serviceAttribute = (AppBlocksServiceAttribute)serviceAttributes[0];

                // return if service keyed service registration is not requested
                if (string.IsNullOrEmpty(serviceAttribute.Name) || !serviceAttribute.IsKeyed)
                {
                    return(false);
                }

                // Exclude live service if current application mode is test
                if (appBlocksContainerBuilder.ApplicationMode == AppBlocksApplicationMode.Test &&
                    serviceAttribute.ServiceDependencyType == AppBlocksServiceDependencyType.Live)
                {
                    return(false);
                }

                // Ask if AppBlocksContainerBuilder concrete implementation wants to exclude the service from registration
                return(appBlocksContainerBuilder.ShouldRegisterService(t, serviceAttribute));
            }

            // Search all assembly types for services to register
            assembly.GetTypes()
            .Where(t => isKeyedService(t))
            .Select(t =>
                    // Create anonymous type
                    new
            {
                TypeInformation      = t,
                AttributeInformation = (AppBlocksServiceAttribute)t
                                       .GetCustomAttribute(typeof(AppBlocksServiceAttribute))
            })
            .ToList()
            .ForEach(i =>
            {
                // Perform attribute validations
                ValidateRegistration(i.AttributeInformation);

                //Find out what to Interface to use for service registration. For keyed service, we enforce
                //having attribute declared with non-null ServiceType. In other words, keyed services
                //must explicitly specify service type.
                if (logger.IsEnabled(LogLevel.Debug))
                {
                    logger.LogDebug($"Registering {i.TypeInformation.FullName} as keyed service of type {i.AttributeInformation.ServiceType.FullName} " +
                                    $"with key {i.AttributeInformation.Name}");
                }

                // Register type with Autofac container builder
                var registrationType = GetServiceRegistrationType(i.TypeInformation, i.AttributeInformation);
                var registration     = builder.RegisterType(i.TypeInformation).Keyed(i.AttributeInformation.Name, registrationType);

                // Set type lifetime scope
                SetTypeLifetimeScope(i.AttributeInformation, registration);

                // Add type to interceptors based on attribute information
                AddTypeInterceptors(i.AttributeInformation, registration);
            });
        }
コード例 #6
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="appBlocksContainerBuilder"><see cref="AppBlocksContainerBuilder"/> for the AppBlocks application</param>
 public AppBlocksModuleBase(AppBlocksContainerBuilder appBlocksContainerBuilder)
 {
     AppBlocksContainerBuilder = appBlocksContainerBuilder;
 }