Ejemplo n.º 1
0
        public static void RegisterSagaStateMachines(this ISagaStateMachineRegistrar registrar, params Assembly[] assemblies)
        {
            if (assemblies.Length == 0)
            {
                assemblies = AppDomain.CurrentDomain.GetAssemblies();
            }

            var types = AssemblyTypeCache.FindTypes(assemblies, x => x.HasInterface(typeof(SagaStateMachine <>))).GetAwaiter().GetResult();

            registrar.RegisterSagaStateMachines(types.FindTypes(TypeClassification.Concrete).ToArray());
        }
Ejemplo n.º 2
0
        public static void RegisterSagaStateMachines(this ISagaStateMachineRegistrar registrar, params Type[] types)
        {
            foreach (var type in types)
            {
                if (!type.HasInterface(typeof(SagaStateMachine <>)))
                {
                    throw new ArgumentException($"The type is not a saga state machine: {TypeMetadataCache.GetShortName(type)}", nameof(types));
                }

                SagaStateMachineRegistrationCache.Register(type, registrar);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Adds all sagas in the specified assemblies matching the namespace. If you are using both state machine and regular sagas, be
        /// sure to call AddSagaStateMachinesFromNamespaceContaining prior to calling this one.
        /// </summary>
        /// <param name="configurator"></param>
        /// <param name="registrar"></param>
        /// <param name="type">The type to use to identify the assembly and namespace to scan</param>
        /// <param name="filter"></param>
        public static void AddSagaStateMachinesFromNamespaceContaining(this IRegistrationConfigurator configurator, ISagaStateMachineRegistrar registrar,
                                                                       Type type, Func <Type, bool> filter)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (type.Assembly == null || type.Namespace == null)
            {
                throw new ArgumentException($"The type {TypeMetadataCache.GetShortName(type)} is not in an assembly with a valid namespace", nameof(type));
            }

            IEnumerable <Type> types;

            if (filter != null)
            {
                bool IsAllowed(Type candidate)
                {
                    return(IsSagaStateMachineOrDefinition(candidate) && filter(candidate));
                }

                types = FindTypesInNamespace(type, IsAllowed);
            }
            else
            {
                types = FindTypesInNamespace(type, IsSagaStateMachineOrDefinition);
            }

            AddSagaStateMachines(configurator, registrar, types.ToArray());
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Adds SagaStateMachines to the registry, using the factory method, and updates the registrar prior to registering so that the default
        /// saga registrar isn't notified.
        /// </summary>
        /// <param name="configurator"></param>
        /// <param name="registrar"></param>
        /// <param name="assemblies">The assemblies to scan for state machines</param>
        public static void AddSagaStateMachines(this IRegistrationConfigurator configurator, ISagaStateMachineRegistrar registrar, params Assembly[] assemblies)
        {
            if (assemblies.Length == 0)
            {
                assemblies = AppDomain.CurrentDomain.GetAssemblies();
            }

            var types = AssemblyTypeCache.FindTypes(assemblies, IsSagaStateMachineOrDefinition).GetAwaiter().GetResult();

            configurator.AddSagaStateMachines(registrar, types.FindTypes(TypeClassification.Concrete).ToArray());
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Adds a SagaStateMachine to the registry, using the factory method, and updates the registrar prior to registering so that the default
        /// saga registrar isn't notified.
        /// </summary>
        /// <param name="configurator"></param>
        /// <param name="registrar"></param>
        /// <param name="sagaDefinitionType"></param>
        /// <typeparam name="TStateMachine"></typeparam>
        /// <typeparam name="TInstance"></typeparam>
        public static ISagaRegistrationConfigurator <TInstance> AddSagaStateMachine <TStateMachine, TInstance>(this IRegistrationConfigurator configurator,
                                                                                                               ISagaStateMachineRegistrar registrar, Type sagaDefinitionType = null)
            where TStateMachine : class, SagaStateMachine <TInstance>
            where TInstance : class, SagaStateMachineInstance
        {
            ISagaRegistration Factory(IContainerRegistrar containerRegistrar)
            {
                SagaStateMachineRegistrationCache.Register(typeof(TStateMachine), registrar);

                if (sagaDefinitionType != null)
                {
                    SagaDefinitionRegistrationCache.Register(sagaDefinitionType, containerRegistrar);
                }

                return(new SagaStateMachineRegistration <TInstance>());
            }

            return(configurator.AddSaga <TInstance>(Factory));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Adds SagaStateMachines to the registry, using the factory method, and updates the registrar prior to registering so that the default
        /// saga registrar isn't notified.
        /// </summary>
        /// <param name="configurator"></param>
        /// <param name="registrar"></param>
        /// <param name="types">The state machine types to add</param>
        public static void AddSagaStateMachines(this IRegistrationConfigurator configurator, ISagaStateMachineRegistrar registrar, params Type[] types)
        {
            IEnumerable <Type> sagaTypes           = types.Where(x => x.HasInterface(typeof(SagaStateMachine <>)));
            IEnumerable <Type> sagaDefinitionTypes = types.Where(x => x.HasInterface(typeof(ISagaDefinition <>)));

            var sagas = from c in sagaTypes
                        join d in sagaDefinitionTypes on c.GetClosingArguments(typeof(SagaStateMachine <>)).Single()
                        equals d.GetClosingArguments(typeof(ISagaDefinition <>)).Single() into dc
                        from d in dc.DefaultIfEmpty()
                        select new { SagaType = c, DefinitionType = d };

            foreach (var saga in sagas)
            {
                SagaStateMachineRegistrationCache.AddSagaStateMachine(configurator, saga.SagaType, saga.DefinitionType, registrar);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Adds all sagas in the specified assemblies matching the namespace. If you are using both state machine and regular sagas, be
        /// sure to call AddSagaStateMachinesFromNamespaceContaining prior to calling this one.
        /// </summary>
        /// <param name="configurator"></param>
        /// <param name="registrar"></param>
        /// <param name="type">The type to use to identify the assembly and namespace to scan</param>
        public static void AddSagaStateMachinesFromNamespaceContaining(this IRegistrationConfigurator configurator, ISagaStateMachineRegistrar registrar,
                                                                       Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (type.Assembly == null || type.Namespace == null)
            {
                throw new ArgumentException($"The type {TypeMetadataCache.GetShortName(type)} is not in an assembly with a valid namespace", nameof(type));
            }

            IEnumerable <Type> types = FindTypesInNamespace(type, IsSagaStateMachineOrDefinition);

            AddSagaStateMachines(configurator, registrar, types.ToArray());
        }
Ejemplo n.º 8
0
 public static void AddSagaStateMachine(IRegistrationConfigurator configurator, Type stateMachineType, Type sagaDefinitionType = null,
                                        ISagaStateMachineRegistrar registrar = null)
 {
     GetOrAdd(stateMachineType).AddSaga(configurator, sagaDefinitionType, registrar ?? new NullSagaStateMachineRegistrar());
 }
Ejemplo n.º 9
0
 public static void Register(Type stateMachineType, ISagaStateMachineRegistrar registrar)
 {
     GetOrAdd(stateMachineType).Register(registrar);
 }