2014-04-12 2 views
0

에 지정된 경로가 cqrs 기반 전자 상거래 응용 프로그램 개발을위한 Mark Nihof (Fohjin) 아키텍처 다음입니다. 내 개발 프레임 워크는 Asp.net MVC5입니다. 사이드 작업 벌금을보고하지만 난 검색하여 명령을 실행하려고 할 때 로컬 호스트 : 63738/API/보안/가입이 나를 예외 메시지를 지정하지 경로를 다음 보여줍니다 'RavenProject.Commands.CreateUserCommand'메시지

내 메시지 라우터 클래스

public class MessageRouter : IRouteMessages 
    { 
     private readonly IDictionary<Type, ICollection<Action<object>>> _routes; 

     public MessageRouter() 
     { 
      _routes = new Dictionary<Type, ICollection<Action<object>>>(); 
     } 

     public void Register<TMessage>(Action<TMessage> route) where TMessage : class 
     { 
      var routingKey = typeof(TMessage); 
      ICollection<Action<object>> routes; 

      if (!_routes.TryGetValue(routingKey, out routes)) 
       _routes[routingKey] = routes = new LinkedList<Action<object>>(); 

      routes.Add(message => route(message as TMessage)); 
     } 

     public void Route(object message) 
     { 
      ICollection<Action<object>> routes; 

      if (!_routes.TryGetValue(message.GetType(), out routes)) 
       throw new RouteNotRegisteredException(message.GetType()); 

      foreach (var route in routes) 
       route(message); 
     } 
    } 

를 다음과 같이 내 경로 레지스터 클래스는 다음과 같습니다 : 다음과 같다

public class RegisterCommandHandlersInMessageRouter 
    { 
     private static MethodInfo _createPublishActionWrappedInTransactionMethod; 
     private static MethodInfo _registerMethod; 

     public static void BootStrap() 
     { 
      new RegisterCommandHandlersInMessageRouter().RegisterRoutes(ObjectFactory.GetInstance<IRouteMessages>() as MessageRouter); 
     } 

     public void RegisterRoutes(MessageRouter messageRouter) 
     { 
      _createPublishActionWrappedInTransactionMethod = GetType().GetMethod("CreatePublishActionWrappedInTransaction"); 
      _registerMethod = messageRouter.GetType().GetMethod("Register"); 

      var commands = CommandHandlerFactory.GetCommands(); 
      var commandHandlers = CommandHandlerFactory.GetCommandHandlers(); 

      foreach (var command in commands) 
      { 
       IList<Type> commandHandlerTypes; 
       if (!commandHandlers.TryGetValue(command, out commandHandlerTypes)) 
        throw new Exception(string.Format("No command handlers found for event '{0}'", command.FullName)); 

       foreach (var commandHandler in commandHandlerTypes) 
       { 
        var injectedCommandHandler = GetCorrectlyInjectedCommandHandler(commandHandler); 
        var action = CreateTheProperAction(command, injectedCommandHandler); 
        RegisterTheCreatedActionWithTheMessageRouter(messageRouter, command, action); 
       } 
      } 
     } 

     private static object GetCorrectlyInjectedCommandHandler(Type commandHandler) 
     { 
      return ObjectFactory.GetInstance(commandHandler); 
     } 

     private static void RegisterTheCreatedActionWithTheMessageRouter(MessageRouter messageRouter, Type commandType, object action) 
     { 
      _registerMethod.MakeGenericMethod(commandType).Invoke(messageRouter, new[] { action }); 
     } 

     private object CreateTheProperAction(Type commandType, object commandHandler) 
     { 
      return _createPublishActionWrappedInTransactionMethod.MakeGenericMethod(commandType, commandHandler.GetType()).Invoke(this, new[] { commandHandler }); 
     } 

     public Action<TCommand> CreatePublishActionWrappedInTransaction<TCommand, TCommandHandler>(TCommandHandler commandHandler) 
      where TCommand : class 
      where TCommandHandler : ICommandHandler<TCommand> 
     { 
      //return command => ObjectFactory.GetInstance<TransactionHandler<TCommand, TCommandHandler>>().Execute(command, commandHandler); 
      return command => ObjectFactory.GetInstance<ICommandHandler<TCommand>>().Execute(command); 
     } 
    } 

이 어디 식별하는 데 실패 아직 내 실수입니다.

참고 : 브라우저에서 호출하는 메서드는 실제로 HTTP POST 메서드이지만 HTTP GET을 사용하는 확인 용도로 사용됩니다.

내 루트 레지스터 클래스는 응용 프로그램을 부트 스트랩하는 동안 모든 경로를 올바르게 등록합니다.

한도 링크 당신은 여전히 ​​경로를 정의해야합니다 https://drive.google.com/file/d/0B1rU7HOTfLweZjFuZlF3M0Z2M28/edit?usp=sharing

답변

0

다음에서 내 작품을 전체적으로 확인할 수 있습니다. 예를 들어 here을 참조하십시오.

_handler = new FirstTestCommandHandler(); 
var messageRouter = new MessageRouter(); 
messageRouter.Register<TestCommand>(x => _handler.Execute(x)); 
DoNotMock.Add(typeof (IRouteMessages), messageRouter);