locked
DI in .NET Core API for ICommandHandler RRS feed

  • Question

  • User299477132 posted

    Hi,

       I have implemented CQS pattern in my project. But i have created individual interfaces for each command handlers which is working. I'm trying to get it generated from one common interface (ICommandHandler) and when i try to configure it through DI in startup.cs the code is failing. Any help is appreciated.

    The below is code

     public interface ICommand
        {
            Guid Id { get; }
        } 
     public abstract class Command : ICommand
        {
            public Guid Id { get; set; }
        
            protected Command()
            {
                Id = Guid.NewGuid();
        
            }
     
        }
      public class CreateAnnouncementDeliveryCommand : Command
        {
            public string DepliveryType { get; set; }
            public string Description { get; set; }
        }
      public interface ICommandHandler<in TCommand, TCommandResult> where TCommand : class, ICommand
        {
            Task<TCommandResult> Execute(TCommand command);
        }
     
     public partial class CreateAnnouncementDeliveryCommandHandler : ICommandHandler<CreateAnnouncementDeliveryCommand, CreateAnnouncementDeliveryCommand>
        {
            private readonly IAnnouncementDeliveryManager _manager;
            private readonly IMapper _mapper;
     
            public CreateAnnouncementDeliveryCommandHandler(IAnnouncementDeliveryManager manager, IMapper mapper)
            {
                _manager = manager;
                _mapper = mapper;
            }
     
            public Task<CreateAnnouncementDeliveryCommand> Execute(CreateAnnouncementDeliveryCommand command)
            {
              
               return not implemented
            }
     
        }
    In Controller
    public class AnnouncementDeliveryController : BaseController
        {
            private readonly ICreateAnnouncementDeliveryCommandHandler _createCommandHandler;
                 private readonly IMapper _mapper;
            private readonly IHttpContextAccessor _httpContextAccessor;
     
            public AnnouncementDeliveryController(ICreateAnnouncementDeliveryCommandHandler createCommandhHndler,
                                           IMapper mapper,
                                           ILoggingService loggingService,
                                           IHttpContextAccessor httpContextAccessor,
                                           IUsersQueryHandler userQueryCommandHandler) : base(loggingService, userQueryCommandHandler, httpContextAccessor)
            {
                _createCommandHandler = createCommandhHndler;
               _httpContextAccessor = httpContextAccessor;
                _mapper = mapper;
            }
    [HttpPost]
            [ActionName("AddAnnouncementDelivery")]
            public async Task<IActionResult> AddAnnouncementDelivery([FromBody] CreateAnnouncementDeliveryCommand _command)
            {
                try
                {
                    var cmd = await _createCommandHandler.Execute(_command);
                    return Ok(cmd);
                }
                catch (Exception e)
                {
                    return BadRequest(e.Message);
                }
            }}
    Adding the below in startup.cs 
    services.AddTransient(typeof(ICommandHandler<CreateAnnouncementDeliveryCommand, CreateAnnouncementDeliveryCommand>), typeof(CreateAnnouncementDeliveryCommandHandler));
     
     
    Friday, February 21, 2020 8:44 PM

Answers

  • User299477132 posted

    Thank you. I figured out the issue. I am sending wrong interface in the constructor.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 24, 2020 3:36 PM

All replies

  • User1120430333 posted

    Hi,

       I have implemented CQS pattern in my project. But i have created individual interfaces for each command handlers which is working. I'm trying to get it generated from one common interface (ICommandHandler) and when i try to configure it through DI in startup.cs the code is failing. Any help is appreciated.

    The below is code

    <div> public interface ICommand</div> <div>    {</div> <div>        Guid Id { get; }</div> <div>    } </div> <div> public abstract class Command : ICommand</div> <div>    {</div> <div>        public Guid Id { get; set; }</div> <div>    </div> <div>        protected Command()</div> <div>        {</div> <div>            Id = Guid.NewGuid();</div> <div>    </div> <div>        }</div> <div> </div> <div>    }</div> <div>  public class CreateAnnouncementDeliveryCommand : Command</div> <div>    {</div> <div>        public string DepliveryType { get; set; }</div> <div>        public string Description { get; set; }</div> <div>    }</div> <div>  public interface ICommandHandler<in TCommand, TCommandResult> where TCommand : class, ICommand</div> <div>    {</div> <div>        Task<TCommandResult> Execute(TCommand command);</div> <div>    }</div> <div> </div> <div> public partial class CreateAnnouncementDeliveryCommandHandler : ICommandHandler<CreateAnnouncementDeliveryCommand, CreateAnnouncementDeliveryCommand></div> <div>    {</div> <div>        private readonly IAnnouncementDeliveryManager _manager;</div> <div>        private readonly IMapper _mapper;</div> <div> </div> <div>        public CreateAnnouncementDeliveryCommandHandler(IAnnouncementDeliveryManager manager, IMapper mapper)</div> <div>        {</div> <div>            _manager = manager;</div> <div>            _mapper = mapper;</div> <div>        }</div> <div> </div> <div>        public Task<CreateAnnouncementDeliveryCommand> Execute(CreateAnnouncementDeliveryCommand command)</div> <div>        {</div> <div>          </div> <div>           return not implemented</div> <div>        }</div> <div> </div> <div>    }</div> <div> </div> <div> In Controller</div> <div> </div> <div> public class AnnouncementDeliveryController : BaseController</div> <div>    {</div> <div>        private readonly ICreateAnnouncementDeliveryCommandHandler _createCommandHandler;</div> <div>             private readonly IMapper _mapper;</div> <div>        private readonly IHttpContextAccessor _httpContextAccessor;</div> <div> </div> <div>        public AnnouncementDeliveryController(ICreateAnnouncementDeliveryCommandHandler createCommandhHndler,</div> <div>                                       IMapper mapper,</div> <div>                                       ILoggingService loggingService,</div> <div>                                       IHttpContextAccessor httpContextAccessor,</div> <div>                                       IUsersQueryHandler userQueryCommandHandler) : base(loggingService, userQueryCommandHandler, httpContextAccessor)</div> <div>        {</div> <div>            _createCommandHandler = createCommandhHndler;</div> <div>           _httpContextAccessor = httpContextAccessor;</div> <div>            _mapper = mapper;</div> <div>        }</div> <div> [HttpPost]</div> <div>        [ActionName("AddAnnouncementDelivery")]</div> <div>        public async Task<IActionResult> AddAnnouncementDelivery([FromBody] CreateAnnouncementDeliveryCommand _command)</div> <div>        {</div> <div>            try</div> <div>            {</div> <div>                var cmd = await _createCommandHandler.Execute(_command);</div> <div>                return Ok(cmd);</div> <div>            }</div> <div>            catch (Exception e)</div> <div>            {</div> <div>                return BadRequest(e.Message);</div> <div>            }</div> <div>        }}</div> <div> Adding the below in startup.cs </div> <div> services.AddTransient(typeof(ICommandHandler<CreateAnnouncementDeliveryCommand, CreateAnnouncementDeliveryCommand>), typeof(CreateAnnouncementDeliveryCommandHandler));</div> <div>  </div> <div>  </div>

    Your post is unreadable.

    Sunday, February 23, 2020 7:18 PM
  • User299477132 posted

    Hi,


       I have implemented CQS pattern in my project. But i have created individual interfaces for each command handlers which is working. I'm trying to get it generated from one common interface (ICommandHandler) and when i try to configure it through DI in startup.cs the code is failing. The handlers are initialized. Any help is appreciated.


    The below is code


     public interface ICommand
        {
            Guid Id { get; }
        } 
     public abstract class Command : ICommand
        {
            public Guid Id { get; set; }
        
            protected Command()
            {
                Id = Guid.NewGuid();
        
            }


        }
      public class CreateAnnouncementDeliveryCommand : Command
        {
            public string DepliveryType { get; set; }
            public string Description { get; set; }
        }
      public interface ICommandHandler<in TCommand, TCommandResult> where TCommand : class, ICommand
        {
            Task<TCommandResult> Execute(TCommand command);
        }
     
     public partial class CreateAnnouncementDeliveryCommandHandler : ICommandHandler<CreateAnnouncementDeliveryCommand, CreateAnnouncementDeliveryCommand>
        {
            private readonly IAnnouncementDeliveryManager _manager;
            private readonly IMapper _mapper;


            public CreateAnnouncementDeliveryCommandHandler(IAnnouncementDeliveryManager manager, IMapper mapper)
            {
                _manager = manager;
                _mapper = mapper;
            }


            public Task<CreateAnnouncementDeliveryCommand> Execute(CreateAnnouncementDeliveryCommand command)
            {
              
               return not implemented
            }


        }


    In Controller


    public class AnnouncementDeliveryController : BaseController
        {
            private readonly ICreateAnnouncementDeliveryCommandHandler _createCommandHandler;
                 private readonly IMapper _mapper;
            private readonly IHttpContextAccessor _httpContextAccessor;


            public AnnouncementDeliveryController(ICreateAnnouncementDeliveryCommandHandler createCommandhHndler,
                                           IMapper mapper,
                                           ILoggingService loggingService,
                                           IHttpContextAccessor httpContextAccessor,
                                           IUsersQueryHandler userQueryCommandHandler) : base(loggingService, userQueryCommandHandler, httpContextAccessor)
            {
                _createCommandHandler = createCommandhHndler;
               _httpContextAccessor = httpContextAccessor;
                _mapper = mapper;
            }
    [HttpPost]
            [ActionName("AddAnnouncementDelivery")]
            public async Task<IActionResult> AddAnnouncementDelivery([FromBody] CreateAnnouncementDeliveryCommand _command)
            {
                try
                {
                    var cmd = await _createCommandHandler.Execute(_command);
                    return Ok(cmd);
                }
                catch (Exception e)
                {
                    return BadRequest(e.Message);
                }
            }}


    Adding the below in startup.cs 
    services.AddTransient(typeof(ICommandHandler<CreateAnnouncementDeliveryCommand, CreateAnnouncementDeliveryCommand>), typeof(CreateAnnouncementDeliveryCommandHandler));
     
    Thanks
    Roopesh
     

    Monday, February 24, 2020 2:06 PM
  • User475983607 posted

    The shared code is incomplete and you have not provided the actual error message.

    Monday, February 24, 2020 2:31 PM
  • User299477132 posted

    Thank you. I figured out the issue. I am sending wrong interface in the constructor.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 24, 2020 3:36 PM