示例#1
0
        public PipelineBuilder WithBehavior(Type behaviorType)
        {
            if (!behaviorType.GetTypeInfo().ImplementedInterfaces.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICommandBehavior <>)))
            {
                throw new ArgumentException($"The supplied type must implement {typeof(ICommandBehavior<>).Name}", nameof(behaviorType));
            }

            if (behaviorType.IsGenericTypeDefinition)
            {
                _services.Scan(s =>
                               s.FromAssemblies(behaviorType.GetTypeInfo().Assembly)
                               .AddClasses(c => c.AssignableTo(behaviorType))
                               .UsingRegistrationStrategy(RegistrationStrategy.Replace(ReplacementBehavior.ImplementationType))
                               .AsImplementedInterfaces()
                               .WithTransientLifetime());
            }
            else
            {
                var behaviorCommandType            = behaviorType.GetGenericArguments().SingleOrDefault();
                var closedCommandBehaviorInterface = typeof(ICommandBehavior <>).MakeGenericType(behaviorCommandType);
                _services.Replace(ServiceDescriptor.Transient(closedCommandBehaviorInterface, behaviorType));
            }

            return(this);
        }
示例#2
0
 internal static IServiceCollection AddCommandHandlers(this IServiceCollection services, IEnumerable <Assembly> assemblies)
 {
     services.Scan(s =>
                   s.FromAssemblies(assemblies)
                   .AddClasses(c => c.AssignableTo(typeof(IMessageHandler <>))
                               .Where(handler => IsValidMessageHandler(handler, typeof(ICommand))))
                   .UsingRegistrationStrategy(RegistrationStrategy.Replace())
                   .AsImplementedInterfaces()
                   .WithTransientLifetime());
     return(services);
 }
示例#3
0
        public void UsingRegistrationStrategy_ReplaceServiceTypes()
        {
            Collection.Scan(scan => scan
                            .FromAssemblyOf <ITransientService>()
                            .AddClasses(classes => classes.AssignableTo <ITransientService>())
                            .AsImplementedInterfaces()
                            .WithTransientLifetime()
                            .AddClasses(classes => classes.AssignableTo <ITransientService>())
                            .UsingRegistrationStrategy(RegistrationStrategy.Replace(ReplacementBehavior.ServiceType))
                            .AsImplementedInterfaces()
                            .WithSingletonLifetime());

            var services = Collection.GetDescriptors <ITransientService>();

            Assert.Equal(1, services.Count(x => x.ServiceType == typeof(ITransientService)));
        }
示例#4
0
        public static ActivityDefinitionList Discover(
            this ActivityDefinitionList list,
            Action <ITypeSourceSelector> selector)
        {
            var typeSourceSelector = new TypeSourceSelector();

            selector(typeSourceSelector);

            var serviceCollection = new ServiceCollection();

            typeSourceSelector.Populate(serviceCollection, RegistrationStrategy.Replace(ReplacementBehavior.All));

            foreach (var service in serviceCollection)
            {
                list.Add(ActivityDescriber.Describe(service.ImplementationType));
            }

            return(list);
        }
示例#5
0
        public static IServiceCollection AddServiceModule(this IServiceCollection services)
        {
            services.Scan(scan => scan
                          .FromAssembliesOf(typeof(ServicesInterfacesAssemblyHelper), typeof(ServicesClassesAssemblyHelper))
                          // Find services and register its matching interfaces/implementations.
                          // For example: JobService matches IJobService, EmployeeService matches IEmployeeService, etc...
                          .AddClasses(classes => classes.InNamespaces(ServicesClassesAssemblyHelper.Namespace))
                          .UsingRegistrationStrategy(RegistrationStrategy.Replace(ReplacementBehavior.ServiceType))
                          .AsMatchingInterface()
                          .WithScopedLifetime()

                          // Now find services with class name ending with 'ExtendedService' and register it to interfaces
                          // it implements.
                          // For example: if JobExtendedService class is present and implements IJobService, then register
                          // it as the implementation for IJobService, replacing the generated service class (JobService).
                          .AddClasses(classes => classes.Where(type => type.Namespace.Equals(ServicesClassesAssemblyHelper.Namespace) &&
                                                               type.Name.EndsWith("ExtendedService")))
                          .UsingRegistrationStrategy(RegistrationStrategy.Replace(ReplacementBehavior.ServiceType))
                          .AsImplementedInterfaces()
                          .WithScopedLifetime()
                          );
            return(services);
        }
        public static IServiceCollection RegisterBehaviorForAllCommands(this IServiceCollection services, Type openGenericBehaviorType)
        {
            _ = openGenericBehaviorType ?? throw new ArgumentNullException(nameof(openGenericBehaviorType), "A non-null command behavior type is required");

            if (!openGenericBehaviorType.IsGenericTypeDefinition)
            {
                throw new ArgumentException($"An open generic behavior type is required, but a closed generic was supplied: '{openGenericBehaviorType.Name}'", nameof(openGenericBehaviorType));
            }

            if (!openGenericBehaviorType.GetInterfaces().Any(t => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(ICommandBehavior <>)))
            {
                throw new ArgumentException($"Generic type definition must be {typeof(ICommandBehavior<>).Name}", nameof(openGenericBehaviorType));
            }

            services.Scan(s =>
                          s.FromAssemblies(openGenericBehaviorType.Assembly)
                          .AddClasses(c => c.AssignableTo(openGenericBehaviorType))
                          .UsingRegistrationStrategy(RegistrationStrategy.Replace(ReplacementBehavior.ImplementationType))
                          .AsImplementedInterfaces()
                          .WithTransientLifetime());

            return(services);
        }
        public void Build_ReplacesHandler()
        {
            // Arrange
            var builder = _fixture.GetBuilder();

            ValidBuilderSetup(builder);

            var expectedService  = typeof(IMessageHandler <MessagingBuilderTests>);
            var originalLifetime = ServiceLifetime.Singleton;

            // Registration expected to be overwriten
            builder.Services.Add(ServiceDescriptor.Describe(
                                     expectedService,
                                     s => null,
                                     originalLifetime));

            var expectedLifetime = ServiceLifetime.Scoped;

            // Act
            builder.AddHandlerDiscovery(h =>
            {
                h.IncludeNonPublic           = true;
                h.DiscoveredHandlersLifetime = expectedLifetime;
                // Configuration to replace any matching service registrations
                h.RegistrationStrategy = RegistrationStrategy.Replace(ReplacementBehavior.ServiceType);
            });

            // Assert
            builder.Build();

            // Assert
            var actualDescriptor = _fixture.ServiceCollection.SingleOrDefault(s => s.ServiceType == expectedService);

            Assert.NotNull(actualDescriptor);
            Assert.NotEqual(originalLifetime, actualDescriptor.Lifetime);
            Assert.Equal(typeof(TestMessageHandler), actualDescriptor.ImplementationType);
        }
示例#8
0
        public static IServiceCollection AddRepositoryModule(this IServiceCollection services)
        {
            services.AddScoped <IUnitOfWork, UnitOfWork>();

            services.Scan(scan => scan
                          .FromAssembliesOf(typeof(IUnitOfWork), typeof(UnitOfWork))
                          // Register repository interfaces using the I prefix convention for interfaces to match interface/class
                          .AddClasses(classes => classes.InNamespaces("blazor.Infrastructure.Data.Repositories"))
                          .UsingRegistrationStrategy(RegistrationStrategy.Replace(ReplacementBehavior.ServiceType))
                          .AsMatchingInterface()
                          .WithScopedLifetime()

                          // Now find repositories that has class name ending with "ExtendedRepository" and register interfaces it implements with priority.
                          // For example: if JobExtendedRepository class is present and implements IJobRepository interface, take precedence over
                          // existing registrations.
                          .AddClasses(classes => classes.Where(type => type.Namespace.Equals("blazor.Infrastructure.Data.Repositories") &&
                                                               type.Name.EndsWith("ExtendedRepository")))
                          .UsingRegistrationStrategy(RegistrationStrategy.Replace(ReplacementBehavior.ServiceType))
                          .AsImplementedInterfaces()
                          .WithScopedLifetime()
                          );

            return(services);
        }
 private static IServiceCollection Populate(this IServiceCollection services, ISelector selector, RegistrationStrategy registrationStrategy)
 {
     selector.Populate(services, registrationStrategy);
     return(services);
 }