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.
To get the most out of RS.Dapper.Utility
, there are just a few conventions to follow.
Your database tables must have a primary key column named exactly Id
(case-sensitive).
The Id
column should be auto-incrementing.
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.
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
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
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.
Hire me on Linkedin
My portfolio