Пример #1
0
        private object[] GetSagasFor(object message)
        {
            var instances = new List <object>();

            Type orchestratesType = reflection.GetGenericTypeOf(typeof(Orchestrates <>), message);
            Type initiatedByType  = reflection.GetGenericTypeOf(typeof(InitiatedBy <>), message);

            var handlers = serviceLocator.GetAllHandlersFor(orchestratesType)
                           .Union(serviceLocator.GetAllHandlersFor(initiatedByType));

            foreach (IHandler sagaHandler in handlers)
            {
                Type sagaType = sagaHandler.Implementation;

                //first try to execute any saga finders.
                Type sagaFinderType     = reflection.GetGenericTypeOf(typeof(ISagaFinder <,>), sagaType, ProxyUtil.GetUnproxiedType(message));
                var  sagaFinderHandlers = serviceLocator.GetAllHandlersFor(sagaFinderType);
                foreach (var sagaFinderHandler in sagaFinderHandlers)
                {
                    try
                    {
                        var sagaFinder = sagaFinderHandler.Resolve();
                        var saga       = reflection.InvokeSagaFinderFindBy(sagaFinder, message);
                        if (saga != null)
                        {
                            instances.Add(saga);
                        }
                    }
                    finally
                    {
                        serviceLocator.Release(sagaFinderHandler);
                    }
                }

                //we will try to use an ISagaMessage's Correlation id next.
                var sagaMessage = message as ISagaMessage;
                if (sagaMessage == null)
                {
                    continue;
                }

                Type sagaPersisterType = reflection.GetGenericTypeOf(typeof(ISagaPersister <>),
                                                                     sagaType);

                object sagaPersister = serviceLocator.Resolve(sagaPersisterType);
                try
                {
                    object sagas = reflection.InvokeSagaPersisterGet(sagaPersister, sagaMessage.CorrelationId);
                    if (sagas == null)
                    {
                        continue;
                    }
                    instances.Add(sagas);
                }
                finally
                {
                    serviceLocator.Release(sagaPersister);
                }
            }
            return(instances.ToArray());
        }