Dozens of stored procedures for CRUD activities, reports, imports, exports, and business logic are frequently encountered in large enterprise applications. In the past, developers would manually map parameters, read DataTables, and transform results into models in a distinct C# function for every process. However, this results in excessive connection between your C# layer and SQL, repetitive code, and difficult-to-maintain repositories.
Making your repository dynamic, which uses Reflection to automatically convert any stored procedure to a C# model, is a better strategy. This article will teach you how to use Reflection inside a Repository Pattern to construct a Dynamic Stored Procedure Mapper with complete control, robust typing, and clear architecture.
1. Why Reflection-Based Mapping?
Let’s understand why this approach is useful.
Problems with Traditional Repository Methods
- You manually create commands for each stored procedure.
- Each method repeats parameter setup and mapping.
- Any DB schema change requires code changes everywhere.
- Adding new stored procedures increases maintenance.
Goal
Create a single generic method like this:
…and it should automatically:
- Create a SQL Command
- Execute the stored procedure
- Read results
- Map columns to the model using property names
That’s the power of Reflection.
2. High-Level Architecture
Let’s visualize the flow of this approach.
Flowchart – Dynamic Stored Procedure Mapping
3. Step-by-Step Implementation
Let’s now build this dynamically.
Step 1: Create a Base Repository
Step 2: Add a Helper Extension for Column Checking
This ensures that the code doesn’t break even if the stored procedure returns fewer or renamed columns.
Step 3: Create Your Repository
Step 4: Your Model Class
The property names must match the column names in your stored procedure result set.
Step 5: Example Stored Procedure
4. How Reflection Helps Here
Reflection allows us to:
- Inspect properties of the model at runtime
- Match them dynamically with SQL result columns
- Assign values safely without writing manual mapping logic
So even if you add a new model tomorrow, you don’t have to write a new mapper — it just works automatically.
5. Error Handling and Logging
You can easily extend the base repository for:
- Exception handling using
try-catch - Logging errors using Serilog or NLog
- Handling
DBNullvalues safely
Example
6. Advantages of This Approach
| Feature | Benefit |
|---|---|
| Reusable | One method for all stored procedures |
| Dynamic | Automatically maps models |
| Strongly Typed | No manual DataTable parsing |
| Clean | Keeps repository lean and focused |
| Scalable | Works across modules and entities |
7. When to Use and When Not To
Use it when:
- You have many stored procedures returning tabular data.
- You follow repository/service architecture.
- You want to reduce mapping boilerplate.
Avoid it when:
- You use Entity Framework with LINQ and tracking.
- You have dynamic column structures that frequently change.
8. Advanced Enhancements
You can extend this further by:
- Returning DataSet for multi-table results.
- Mapping to dynamic objects.
- Adding caching for stored procedure results.
- Using Dapper underneath for performance boost.
Conclusion
By combining the Repository Pattern with Reflection, you can turn your database access layer into a powerful, dynamic engine. You no longer need dozens of repetitive stored procedure methods — one reusable method can handle them all. This approach keeps your architecture clean, flexible, and future-ready, especially in projects with heavy SQL-based business logic.
Best ASP.NET Core 10.0 Hosting
The feature and reliability are the most important things when choosing a good ASP.NET Core 10.0 hosting. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core , their servers are optimized for PHP web applications such as the latest ASP.NET Core 10.0 version. The performance and the uptime of the ASP.NET Core hosting service are excellent, and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. At HostForLIFE.eu, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its data centers are equipped with top equipment like cooling system, fire detection, high-speed Internet connection, and so on. That is why HostForLIFE.eu guarantees 99.9% uptime for ASP.NET Core . And the engineers do regular maintenance and monitoring works to assure its ASP.NET Core hosting are security and always up.

