Пример #1
0
        protected AggregateSaga()
        {
            Settings = new AggregateSagaSettings(Context.System.Settings.Config);
            var idValue = Context.Self.Path.Name;

            PersistenceId = idValue;
            Id            = (TIdentity)Activator.CreateInstance(typeof(TIdentity), idValue);

            if (Id == null)
            {
                throw new InvalidOperationException(
                          $"Identity for Saga '{Id.GetType().PrettyPrint()}' could not be activated.");
            }

            if ((this as TAggregateSaga) == null)
            {
                throw new InvalidOperationException(
                          $"AggregateSaga {Name} specifies Type={typeof(TAggregateSaga).PrettyPrint()} as generic argument, it should be its own type.");
            }

            if (State == null)
            {
                try
                {
                    State = (TSagaState)Activator.CreateInstance(typeof(TSagaState));
                }
                catch (Exception exception)
                {
                    Context.GetLogger().Error(exception, "AggregateSaga of Name={1}; was unable to activate SagaState of Type={0}.", Name, typeof(TSagaState).PrettyPrint());
                }
            }

            if (Settings.AutoReceive)
            {
                InitReceives();
                InitAsyncReceives();
            }

            InitTimeoutJobManagers();
            InitAsyncTimeoutJobManagers();

            if (Settings.UseDefaultEventRecover)
            {
                Recover <ICommittedEvent <TAggregateSaga, TIdentity, IAggregateEvent <TAggregateSaga, TIdentity> > >(Recover);
                Recover <RecoveryCompleted>(Recover);
            }

            if (Settings.UseDefaultSnapshotRecover)
            {
                Recover <SnapshotOffer>(Recover);
            }

            Command <SaveSnapshotSuccess>(SnapshotStatus);
            Command <SaveSnapshotFailure>(SnapshotStatus);

            _eventDefinitionService    = new EventDefinitionService(Context.GetLogger());
            _snapshotDefinitionService = new SnapshotDefinitionService(Context.GetLogger());
        }
Пример #2
0
        protected AggregateSaga()
        {
            Logger   = Context.GetLogger();
            Settings = new AggregateSagaSettings(Context.System.Settings.Config);
            var idValue = Context.Self.Path.Name;

            PersistenceId = idValue;
            Id            = (TIdentity)Activator.CreateInstance(typeof(TIdentity), idValue);

            if (Id == null)
            {
                throw new InvalidOperationException(
                          $"Identity for Saga '{Id.GetType().PrettyPrint()}' could not be activated.");
            }

            if ((this as TAggregateSaga) == null)
            {
                throw new InvalidOperationException(
                          $"AggregateSaga '{GetType().PrettyPrint()}' specifies '{typeof(TAggregateSaga).PrettyPrint()}' as generic argument, it should be its own type");
            }

            if (State == null)
            {
                try
                {
                    State = (TSagaState)Activator.CreateInstance(typeof(TSagaState));
                }
                catch
                {
                    Logger.Warning($"Unable to activate State for {GetType()}");
                }
            }

            if (Settings.AutoReceive)
            {
                InitReceives();
                InitAsyncReceives();
            }

            Register(State);

            if (Settings.UseDefaultEventRecover)
            {
                Recover <ICommittedEvent <TAggregateSaga, TIdentity, IAggregateEvent <TAggregateSaga, TIdentity> > >(Recover);
                Recover <RecoveryCompleted>(Recover);
            }


            if (Settings.UseDefaultSnapshotRecover)
            {
                Recover <SnapshotOffer>(Recover);
            }


            _eventDefinitionService    = new EventDefinitionService(Logger);
            _snapshotDefinitionService = new SnapshotDefinitionService(Logger);
        }
Пример #3
0
        protected AggregateSaga()
        {
            Logger   = Context.GetLogger();
            Settings = new AggregateSagaSettings(Context.System.Settings.Config);
            var idValue = Context.Self.Path.Name;

            PersistenceId = Context.Self.Path.Name;
            Id            = (TIdentity)Activator.CreateInstance(typeof(TIdentity), idValue);

            if (Id == null)
            {
                throw new InvalidOperationException(
                          $"Identity for Saga '{Id.GetType().PrettyPrint()}' could not be activated.");
            }

            if ((this as TAggregateSaga) == null)
            {
                throw new InvalidOperationException(
                          $"AggregateSaga '{GetType().PrettyPrint()}' specifies '{typeof(TAggregateSaga).PrettyPrint()}' as generic argument, it should be its own type");
            }

            if (State == null)
            {
                try
                {
                    State = (TSagaState)Activator.CreateInstance(typeof(TSagaState));
                }
                catch
                {
                    Logger.Warning($"Unable to activate State for {GetType()}");
                }
            }

            if (Settings.AutoReceive)
            {
            }

            Register(State);
        }
Пример #4
0
        protected AggregateSaga()
        {
            Logger   = Context.GetLogger();
            Settings = new AggregateSagaSettings(Context.System.Settings.Config);
            var idValue = Context.Self.Path.Name;

            PersistenceId = idValue;
            Id            = (TIdentity)Activator.CreateInstance(typeof(TIdentity), idValue);

            if (Id == null)
            {
                throw new InvalidOperationException(
                          $"Identity for Saga '{Id.GetType().PrettyPrint()}' could not be activated.");
            }

            if ((this as TAggregateSaga) == null)
            {
                throw new InvalidOperationException(
                          $"AggregateSaga '{GetType().PrettyPrint()}' specifies '{typeof(TAggregateSaga).PrettyPrint()}' as generic argument, it should be its own type");
            }

            if (State == null)
            {
                try
                {
                    State = (TSagaState)Activator.CreateInstance(typeof(TSagaState));
                }
                catch
                {
                    Logger.Warning($"Unable to activate State for {GetType()}");
                }
            }

            if (Settings.AutoReceive)
            {
                var type = GetType();

                var subscriptionTypes =
                    type
                    .GetSagaEventSubscriptionTypes();

                var methods = type
                              .GetTypeInfo()
                              .GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                              .Where(mi =>
                {
                    if (mi.Name != "Handle")
                    {
                        return(false);
                    }

                    var parameters = mi.GetParameters();

                    return
                    (parameters.Length == 1);
                })
                              .ToDictionary(
                    mi => mi.GetParameters()[0].ParameterType,
                    mi => mi);


                var method = type
                             .GetBaseType("ReceivePersistentActor")
                             .GetMethods(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
                             .Where(mi =>
                {
                    if (mi.Name != "CommandAsync")
                    {
                        return(false);
                    }
                    var parameters = mi.GetParameters();
                    return
                    (parameters.Length == 2 &&
                     parameters[0].ParameterType.Name.Contains("Func"));
                })
                             .First();

                foreach (var subscriptionType in subscriptionTypes)
                {
                    var funcType             = typeof(Func <,>).MakeGenericType(subscriptionType, typeof(Task));
                    var subscriptionFunction = Delegate.CreateDelegate(funcType, this, methods[subscriptionType]);
                    var actorReceiveMethod   = method.MakeGenericMethod(subscriptionType);

                    actorReceiveMethod.Invoke(this, new[] { subscriptionFunction, null });
                }
            }

            Register(State);

            if (Settings.UseDefaultEventRecover)
            {
                Recover <DomainEvent <TAggregateSaga, TIdentity, IAggregateEvent <TAggregateSaga, TIdentity> > >(Recover);
                Recover <IAggregateEvent <TAggregateSaga, TIdentity> >(Recover);
                Recover <RecoveryCompleted>(Recover);
            }


            if (Settings.UseDefaultSnapshotRecover)
            {
                Recover <SnapshotOffer>(Recover);
            }
        }