Similar Posts

Leave a Reply

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

39 Comments

  1. Dapper looks like a great alternative to EF. However, I feel one has to have a great grasp of writing plain SQL statements when using it. Also how will it work with lazyloading and eagerloading? Great article still Mukesh.

    1. I really dont’t think Dapper can be an alternative of EFCore. Neither can EFCore replace Dapper. An Ideal use case would be to use both these ORMs in our applications. Dapper is much faster than EFCore while querying complex and huge joins. EFCore has it’s own set of features where it is the KING. Using both these ORMs side by side would take ASP.NET Core Applications to the next level!

      Thanks for the regular support! 🙂 Regards

    2. We tend to use Dapper for parts where reading needs to be tuned since it’s faster than EF for pulling out data.
      EF is better for writes since it will handle all rollbacks and tracking for you, it’s much less hassle and boilerplate code needed. However, I tend to always go with EF for personal projects since I love the migrations and code first approach.

  2. Thanks for this amazing work. You added so many posts within 2 weeks. I need to go thru everything in this weekend. ? Could you please post articles related to Logging, Caching practices etc

  3. Please make a downloadable version of the article for people who don’t like reading online in real-time. Thanks.

  4. If you like working with SQL, SQL+ is the best way to do what you are doing. It’s nearly twice as fast as Dapper and a lot less work.

  5. Looking at your implementation of the UnitOfWork, what happens when the number of entities increases? Are you going to pass all of them through the constructor in the class?

    1. Hi, it actually depends on the scope of the project. If the project is somewhat a smaller one, Injecting the Repositories to the Constructor is a easier way to get around. But, when the Repository count keeps on increase, it is better to separate the Repository away from the UNIT of work and inject both the UOW and Repository in the constructor of the calling class.

      Thanks and Regards.

      1. Isn’t the point of Unit of Work to keep track of all changes and then do a complete Save call with rollback management?
        Like if you have the repositories for products, orders and customers in the same UoW and need to modifications for all of them inside that scope? EF handles this for you, but with dapper I assume you would manually have to manage transactions and do rollbacks?

        1. Yes, that is correct. I thought that the article would give some interesting ideas on that matter. The UoW in the article is not really an UoW. It just seems to be a collection of repositories.

  6. Hi. This is great. If I want to use EFCore use connect = context.Database.GetConnection(), then use Dapper connect.Query and use UnitOfWork for Dbcontext . Is this okay?

    1. Hi, Thanks for the feedback.
      I wouldnt use it that way. But it depends on the developer and the exact scenario you are at. The basic idea is to decouple everything. With this approach, you are forcing Dapper to depend on EFCore, which isn’t actually needed. Try to make the connection centralized. But, yeah you could still use this if that’s what your application wants and doesn’t cause an issue later on down the road.

      Thanks and regards

  7. I can understand that after the use of the ”using” connections will close. Isnt it better to ensure that you will close every connection in your repository methods or at least use the Dispose Pattern?

  8. how to handle common class and objects in the above layers.(i mean which layer used for common functionality)

  9. Hi Mukesh,

    Thanks for a wonderful article can you please make an article on how to apply paging using dapper same as you have done with entity framework.thanks

  10. Hi @mukesh, is there a way to achieve the audit tracking similar to how we achieve that using EF core(Either via Generic repository or DbContext via tracking). I mean without writing the auditing inside each repository like ProductRepo, SalesRepo etc.

  11. I keep getting an error: System.InvalidOperationException: Invalid operation. The connection is closed. can anyone help

  12. Hello Mukesh,
    I am not able to find the link to the source code on this page. Has it been removed since?
    Please help.
    Thank you!

      1. Thank you very much. Hugely appreciated.
        On github, I went to your repository and searched for ‘Dapper.WebApi’.
        Thanks, nonetheless!

    1. It’s based on your project actually. There is no rule that you have to always use the Generic Repository Pattern everywhere. If the complexity of the project is too small, I would happily use this. In some cases, there might not be a real need to have a Repository pattern all together. For instance, in some of my recent projects, I completely disregarded the idea of writing my own repository class, and instead am using the Ardalis.Specification package.

      Thanks

  13. I working into project with full DAPPER, including CRUD operations, my expirience is not god, we having low performance due the relationated entities, and more cases.
    The acrchitect always excuses “the dapper is fast and EF is slow” .
    Now we have a challenge to trasnform this application in SAS and Multitenancy.

  14. Mukesh.

    Is it possible to get a variable declared in your controller e request it inside ProductRepository like a ViewData or TempData?
    As far i know there is no ViewData, ViewBag or TempData in .NET CORE web api correct? I’ve been searching for days something like.

    CONTROLLER
    [HttpGet]
    public async Task GetAll()
    {
    ===> var Sample = “abc”;
    var data = await unitOfWork.Products.GetAllAsync();
    return Ok (data);
    }

    ProductRepository
    public async Task<IReadOnlyList> GetAllAsync()
    {
    ===> var RequestedSample = Sample;
    var sql = “SELECT * FROM Products”;
    using (var connection = new SqlConnection(configuration.GetConnectionString(“DefaultConnection”)))
    {
    connection.Open();
    var result = await connection.QueryAsync(sql);
    return result.ToList();
    }
    }

  15. Looking forward to seeing this app and blazor hero ported to .NET 7 (or 8). I did this excellent project but found out startup.cs has been sunsetted starting with .net 6 Blazor hero is super awesome btw….

  16. Would it be better to design the repository to take a reference to sqlConnection in the constructor? From the standpoint of unit testing, I thought it was better to pass into the constructor any object that needs to be new’ed up to facilitate mocking…

  17. services.AddInfrastructure();
    hi Thank you for your very good article.
    I did this example with the 2022 version, which does not have the startup.cs file, and this line was not solved for me and I could not continue. Can you guide me?