コード例 #1
0
ファイル: CqrsFacility.cs プロジェクト: ddd-cqrs-es/Cqrs-1
        private void onComponentRegistered(string key, IHandler handler)
        {
            if (handler.ComponentModel.Name == m_EngineComponetName)
            {
                var dependencyModels = m_BoundedContexts.Cast <IRegistration>().Concat(m_Sagas).SelectMany(bc => bc.Dependencies).Select(d => new DependencyModel(d.Name, d, false));
                foreach (var dependencyModel in dependencyModels)
                {
                    handler.ComponentModel.Dependencies.Add(dependencyModel);
                }
                return;
            }

            var isCommandsHandler = (bool)(handler.ComponentModel.ExtendedProperties["IsCommandsHandler"] ?? false);
            var isProjection      = (bool)(handler.ComponentModel.ExtendedProperties["IsProjection"] ?? false);
            var isSaga            = (bool)(handler.ComponentModel.ExtendedProperties["isSaga"] ?? false);
            var isProcess         = (bool)(handler.ComponentModel.ExtendedProperties["isProcess"] ?? false);
            var dependsOnBoundedContextRepository = handler.ComponentModel.ExtendedProperties["dependsOnBoundedContextRepository"];

            if (dependsOnBoundedContextRepository != null)
            {
                handler.ComponentModel.Dependencies.Add(new DependencyModel(m_EngineComponetName, typeof(ICqrsEngine), false));


                return;
            }


            if (isCommandsHandler && isProjection)
            {
                throw new InvalidOperationException("Component can not be projection and commands handler simultaneousely");
            }

            if (isProjection)
            {
                var projectedBoundContext = (string)(handler.ComponentModel.ExtendedProperties["ProjectedBoundContext"]);
                var boundContext          = (string)(handler.ComponentModel.ExtendedProperties["BoundContext"]);
                var registration          = m_BoundedContexts.FirstOrDefault(bc => bc.Name == boundContext);
                if (registration == null)
                {
                    throw new ComponentRegistrationException(string.Format("Bounded context {0} was not registered", projectedBoundContext));
                }
                if (registration is RemoteBoundedContextRegistration)
                {
                    throw new ComponentRegistrationException(string.Format("Projection can be registered only for local bounded contexts. Bounded context {0} is remote", registration.Name));
                }

                //TODO: decide which service to use
                (registration as LocalBoundedContextRegistration).WithProjection(handler.ComponentModel.Services.First(), projectedBoundContext);
            }


            if (isCommandsHandler)
            {
                var commandsHandlerFor = (string)(handler.ComponentModel.ExtendedProperties["CommandsHandlerFor"]);

                var registration = m_BoundedContexts.FirstOrDefault(bc => bc.Name == commandsHandlerFor);
                if (registration == null)
                {
                    throw new ComponentRegistrationException(string.Format("Bounded context {0} was not registered", commandsHandlerFor));
                }
                if (registration is RemoteBoundedContextRegistration)
                {
                    throw new ComponentRegistrationException(string.Format("Commands handler can be registered only for local bounded contexts. Bounded context {0} is remote", commandsHandlerFor));
                }

                //TODO: decide which service to use
                (registration as LocalBoundedContextRegistration).WithCommandsHandler(handler.ComponentModel.Services.First());
            }

            if (isSaga)
            {
                var listenedBoundContexts = (string[])(handler.ComponentModel.ExtendedProperties["ListenedBoundContexts"]);
                m_Sagas.Add(Saga.OfType(handler.ComponentModel.Services.First()).Listening(listenedBoundContexts));
            }

            if (isProcess)
            {
                var processFor   = (string)(handler.ComponentModel.ExtendedProperties["ProcessFor"]);
                var registration = m_BoundedContexts.FirstOrDefault(bc => bc.Name == processFor);
                if (registration == null)
                {
                    throw new ComponentRegistrationException(string.Format("Bounded context {0} was not registered", processFor));
                }
                if (registration is RemoteBoundedContextRegistration)
                {
                    throw new ComponentRegistrationException(string.Format("Process can be registered only for local bounded contexts. Bounded context {0} is remote", processFor));
                }

                (registration as LocalBoundedContextRegistration).WithProcess(handler.ComponentModel.Services.First());
            }
        }