Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

43 Comments

  1. You should also run “Install-Package Microsoft.EntityFrameworkCore.Tools” in the Nuget package Manager Console in order to be able to generate the migrations.

  2. Is it necessary to check products for null in GetAllProductsQueryHandler.Handle(…) method?

    Based on the source code of the ToListAsync(…) method, an empty collection will be returned instead of null

    https://bit.ly/2YE5sOA

    1. Hi, Yes I agree. It’s not apt to use Reflection. But since this is just a demonstration of pipeline and it has really nothing to do with logging, I tried to keep things simple. I had also added a small note to not use this in production. Thanks for the feedback.

      Regards.

  3. Hi, great article, I liked the way how you simplify things so everyone can understand. One thing to mention in your code basically the class Product is not model but it should be an entity

  4. Hi,
    The code below show a alert that it hides inherited member. Its really necessary ?

    public async Task SaveChanges()
    {
    return await base.SaveChangesAsync();
    }

  5. @Jose I have this and it’s working fine, don’t forget

    public async Task SaveChanges()
    {
    return await base.SaveChangesAsync();
    }

    BTW love the article, learning loads 🙂

  6. Hi! Thank you for your post! It’s absolutely clear and useful!
    But one thing seems strange to me – duplication of ID in body and in the route of Update request. In my opinion, there is no reason for the Service user to specify ID twice.
    Can you suggest solution to avoid it?

  7. Hi Mukesh,
    This is a well-written article. I am implementing the code in ASP.NET 5. Everything is working fine except there is 1 error in ProductController at the line:
    protected IMediator Mediator => _mediator ??= HttpContext.RequestServices.GetService();
    Error CS0308 The non-generic method ‘IServiceProvider.GetService(Type)’ cannot be used with type arguments
    Do you have any idea how to resolve this issue so that the project builds and runs?

      1. Final changes to get the project running fine is:
        Include in the ProductController class:
        using Microsoft.Extensions.DependencyInjection;
        Then in ProductController class you need to replace:
        protected IMediator Mediator => _mediator ??= HttpContext.RequestServices.GetService();
        with:
        protected IMediator Mediator => _mediator ??= HttpContext.RequestServices.GetService(typeof(IMediator)) as IMediator;

        Then in Startup class: (Register IApplicationContext) : services.AddScoped();
        If anybody requires the full source code I am happy to share it on GitHub.

  8. The correct code for ASP.NET 5 implementation for Handle Method will be :
    You shall need to include, using Microsoft.EntityFrameworkCore;
    public class GetProductByIdQuery : IRequest
    {
    public async Task Handle(GetProductByIdQuery query, CancellationToken cancellationToken)
    {
    var product = _context.Products.Where(a => a.Id == query.Id).FirstOrDefaultAsync();
    if (product == null) return null;
    return product;
    }
    }
    }

  9. How does this relate to your BlazorHero implementation? It seems they are quite different. Thanks.

    1. This is a base implementation of CQRS features which is also implemented in the BlazorHero Project. You need to understand this with MediatR pattern to get the complete knowledge of the CQRS Pattern applied in the Application layer.

      Regards

  10. Hi,
    I am getting this error (Asp.net Core 3.1):-

    Error while validating the service descriptor ‘ServiceType: MediatR.IRequestHandler`2[CQRS.Api.Features.Queries.GetAllProductsQuery,System.Collections.Generic.IEnumerable`1[CQRS.Api.Models.Product]] Lifetime: Transient ImplementationType: CQRS.Api.Features.Queries.GetAllProductsQuery+GetAllProductsQueryHandler’: Unable to resolve service for type ‘CQRS.Api.Context.IApplicationContext’ while attempting to activate ‘CQRS.Api.Features.Queries.GetAllProductsQuery+GetAllProductsQueryHandler’.

    However I have configured EF properly in statup class as mentioned in tutorial

    services.AddDbContext(options =>
    options.UseSqlServer(
    Configuration.GetConnectionString(“DefaultConnection”),
    b => b.MigrationsAssembly(typeof(ApplicationContext).Assembly.FullName)));

  11. I am facing some issues while implementing authentication using JW Token, have you got any documentation for that, it will be very useful, moreover I would like to contribute your Fluent POS OS project, but getting started link is not working, if you can share your idea, I can work on the user interface part which supposed to be in angular

    1. Hi, JWT Authentication is implemented in fluentpos as well. you can refer to it’s source code.
      About getting-started, there is no separate document for it. But you can see the readme – https://github.com/fluentpos/fluentpos#readme. All the details are mentioned here.
      If you are interested in contributing to fluentpos UI, could be reach me out on Discord. The links are on the readme as well.

      Regards

  12. why Iam getting this warnings? new in this

    1>C:\Users\001733\source\projects\CQRSwithMediatR\CQRSwithMediatR\Context\ApplicationContext.cs(16,32,16,43): warning CS0114: ‘ApplicationContext.SaveChanges()’ hides inherited member ‘DbContext.SaveChanges()’. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
    1>C:\Users\001733\source\projects\CQRSwithMediatR\CQRSwithMediatR\Features\Queries\GetProductByIdQuery.cs(22,40,22,46): warning CS1998: This async method lacks ‘await’ operators and will run synchronously. Consider using the ‘await’ operator to await non-blocking API calls, or ‘await Task.Run(…)’ to do CPU-bound work on a background thread.
    1>CQRSwithMediatR -> C:\Users\001733\source\projects\CQRSwithMediatR\CQRSwithMediatR\bin\Debug\net5.0\CQRSwithMediatR.dll

  13. Addition of Microsoft.EntityFrameworkCore.Tools as additional nuget package to add to the solution. This is to alllow Migrations. Without it Migration command like “add-migration” and “Update-database” will not be recognized

  14. I am new for CQRS. I do not understand why MediatR can implement CQRS, should this be done by separated database?

  15. Your article gave me a lot of inspiration, I hope you can explain your point of view in more detail, because I have some doubts, thank you.