How to Use RS.Dapper.Utility in C Sharp

How to Use RS.Dapper.Utility in C Sharp

If you're tired of writing stored procedures or repeating boilerplate SQL for every CRUD operation, then RS.Dapper.Utility is the productivity tool you need. It lets you generate dynamic SQL queries on the fly, just by defining table names, filters, and other options at runtime. The heart of this library is the SqlBuilder—a flexible and type-safe way to construct INSERT, UPDATE, DELETE, SELECT, and pagination queries.

This utility is built for developers who want to code less but build more—saving hours of repetitive SQL and making your C# projects cleaner and more maintainable.

🔧 Before You Start: A Few Simple Rules

To get the most out of RS.Dapper.Utility, there are just a few conventions to follow.

🗄️ Database Rules

  • Your database tables must have a primary key column named exactly Id (case-sensitive).

  • The Id column should be auto-incrementing.

  • C# models and table columns names should be same(I am thinking to make this more flexible but soon)
  • The utility has been tested with int as the primary key type. You can use GUID or other types, but that will require minimal changes and testing in the C# logic.

✅ That's it—no complicated migrations or stored procedure writing needed. Just keep your schema clean and consistent. But we can usen PROC also 😊if our SQL query are to complex etc.

🧱 Setting Up in Your C# Project

This is where the real work happens—but don’t worry, the setup is straightforward. You can find a complete working implementation here:
👉 GitHub: ProjectArchitecture (main branch)

1️⃣ Add Connection and Database Type to appsettings.json

"ConnectionStrings": {
  "DefaultConnection": "Server=DESKTOP-FFBIFD1\\SQLEXPRESS;Database=AppFoundationDb;Trusted_Connection=True;TrustServerCertificate=True;"
},
"DatabaseSettings": {
  "Type": "SqlServer" // Options: SqlServer, PostgreSql, MySql
}

This informs the utility about your target database type so it can generate the correct SQL syntax accordingly.

2️⃣ Inject Dependencies in Program.cs (API or Blazor Project)

// Start-RS.Dapper.Utility
DbSchema.Initialize(builder.Configuration);
builder.Services.AddSingleton<DapperContext>(); // Or Scoped/Transient based on your design
builder.Services.AddScoped<IDapperRepository, DapperRepository>();
// End-RS.Dapper.Utility

This enables dependency injection for Dapper operations and initializes the schema and database configuration at runtime.

3️⃣ Create Your Repositories (Example: CategoryRepository)

Now add your business-specific repositories like ICategoryRepository and CategoryRepository, implementing standard CRUD operations. Thanks to RS.Dapper.Utility, you won’t have to write raw SQL for each action—it’ll be handled dynamically.

Here’s a simple pattern to follow:

public class CategoryRepository : ICategoryRepository
{
    private readonly IDapperRepository _repository;

    public CategoryRepository(IDapperRepository repository)
    {
        _repository = repository;
    }

    public async Task<IEnumerable<Category>> GetAllAsync()
    {
        return await _repository.GetAllAsync<Category>("Category");
    }

    public async Task<Category?> GetByIdAsync(int id)
    {
        return await _repository.GetByIdAsync<Category>("Category", id);
    }

    public async Task<int> InsertAsync(Category model)
    {
        return await _repository.InsertAsync("Category", model);
    }

    public async Task<bool> UpdateAsync(Category model)
    {
        return await _repository.UpdateAsync("Category", model);
    }

    public async Task<bool> DeleteAsync(int id)
    {
        return await _repository.DeleteAsync("Category", id);
    }
}

This structure drastically reduces the amount of manual SQL code you’d otherwise have to write and maintain.
Note: I am using more clean code for this, please check CategoryRepository class

📖 Want to Learn More?

If you're curious about how this utility evolved and what problems it solves under the hood, be sure to read:

👉 My Journey to a Smarter SQL Builder with Dapper 

🚀 Summary

With RS.Dapper.Utility, you can:

  • Skip repetitive stored procedures

  • Use strongly-typed C# models for database interaction

  • Switch between SQL Server, MySQL, and PostgreSQL with minimal changes

  • Build scalable data access layers faster

Start using it today to streamline your database development in .NET projects.

📁 Download the code and give it a try!

 


Thanks, for reading the blog, I hope it helps you. Please share this link on your social media accounts so that others can read our valuable content. Share your queries with our expert team and get Free Expert Advice for Your Business today.


About Writer

Ravinder Singh

Full Stack Developer
I have 15+ years of experience in commercial software development. I write this blog as a kind of knowledge base for myself. When I read about something interesting or learn anything I will write about it. I think when writing about a topic you concentrate more and therefore have better study results. The second reason why I write this blog is, that I love teaching and I hope that people find their way on here and can benefit from my content.

Hire me on Linkedin

My portfolio

Ravinder Singh Full Stack Developer