Пример #1
0
        /// <summary>
        /// Extension method to execute default NSaga components registration in Autofac Container Builder.
        /// <para>
        /// Default registrations are:
        /// <list type="bullet">
        /// <item><description><see cref="JsonNetSerialiser"/> to serialise messages; </description></item>
        /// <item><description><see cref="InMemorySagaRepository"/> to store saga datas; </description></item>
        /// <item><description><see cref="AutofacSagaFactory"/> to resolve instances of Sagas;</description></item>
        /// <item><description><see cref="SagaMetadata"/> to work as the key component - SagaMediator;</description></item>
        /// <item><description><see cref="MetadataPipelineHook"/> added to the pipeline to preserve metadata about incoming messages.</description></item>
        /// </list>
        /// </para>
        /// </summary>
        /// <param name="builder">Container Builder to do the registration</param>
        /// <param name="assemblies">Assemblies to scan for Sagas</param>
        /// <returns>The same container builder so the calls can be chained in Builder-fashion</returns>
        public static ContainerBuilder RegisterNSagaComponents(this ContainerBuilder builder, params Assembly[] assemblies)
        {
            Guard.ArgumentIsNotNull(builder, nameof(builder));
            Guard.ArgumentIsNotNull(assemblies, nameof(assemblies));

            builder.RegisterType <AutofacSagaFactory>().As <ISagaFactory>();
            builder.RegisterType <JsonNetSerialiser>().As <IMessageSerialiser>();
            builder.RegisterType <InMemorySagaRepository>().As <ISagaRepository>();
            builder.RegisterType <SagaMediator>().As <ISagaMediator>();
            builder.RegisterType <MetadataPipelineHook>().As <IPipelineHook>();


            var sagatTypesDefinitions = NSagaReflection.GetAllSagasInterfaces(assemblies);

            foreach (var sagatTypesDefinition in sagatTypesDefinitions)
            {
                builder.RegisterType(sagatTypesDefinition.Key).As(sagatTypesDefinition.Value);
            }

            var allSagaTypes = NSagaReflection.GetAllSagaTypes(assemblies);

            builder.RegisterTypes(allSagaTypes.ToArray());

            return(builder);
        }
Пример #2
0
        public void GetSagaTypes_Always_ContainsMySaga()
        {
            // Act
            var result = NSagaReflection.GetAllSagaTypes(new Assembly[] { typeof(NSagaReflectionTests).Assembly });

            // Assert
            result.Should().Contain(s => s == typeof(MySaga))
            .And.Contain(s => s == typeof(SagaWithErrors))
            .And.HaveCount(2);
        }
Пример #3
0
        /// <summary>
        /// Extension method to execute default NSaga components registration in StructureMap Container Builder.
        /// <para>
        /// Default registrations are:
        /// <list type="bullet">
        /// <item><description><see cref="JsonNetSerialiser"/> to serialise messages; </description></item>
        /// <item><description><see cref="InMemorySagaRepository"/> to store saga datas; </description></item>
        /// <item><description><see cref="StructureMapSagaFactory"/> to resolve instances of Sagas;</description></item>
        /// <item><description><see cref="SagaMetadata"/> to work as the key component - SagaMediator;</description></item>
        /// <item><description><see cref="MetadataPipelineHook"/> added to the pipeline to preserve metadata about incoming messages.</description></item>
        /// </list>
        /// </para>
        /// </summary>
        /// <param name="builder">Container Builder to do the registration</param>
        /// <param name="assemblies">Assemblies to scan for Sagas</param>
        /// <returns>The same container builder so the calls can be chained in Builder-fashion</returns>
        public static Container RegisterNSagaComponents(this Container builder, params Assembly[] assemblies)
        {
            Guard.ArgumentIsNotNull(builder, nameof(builder));
            Guard.ArgumentIsNotNull(assemblies, nameof(assemblies));

            builder.Configure(x => {
                x.AddRegistry <NSagaRegistry>();

                var sagatTypesDefinitions = NSagaReflection.GetAllSagasInterfaces(assemblies);
                foreach (var sagatTypesDefinition in sagatTypesDefinitions)
                {
                    x.AddType(sagatTypesDefinition.Value, sagatTypesDefinition.Key.UnderlyingSystemType);
                }

                var allSagaTypes = NSagaReflection.GetAllSagaTypes(assemblies).ToList();
                allSagaTypes.ToList().ForEach(t => { x.For(t); });
            });

            return(builder);
        }