Feature Request: Add IMyContextProcedures interface class
See original GitHub issueFirst of all, thank you for this tool as it is simply amazing and I use it for all my projects now.
I was wondering, in line with the POCO class generation, if you could add a checkbox option to generate another class of stored procedures (SP) for use with Dapper. These functions would have a similar strongly typed parameter list as per the current SP functions (minus the added output return value), use the existing database connection string from the context and return the result set.
For example, if I had two SPs (GetUsers and AddUser) that gets a list of users based on some filters and adds a new user and returns the newly created primary key, your tool could generate something like the following:
// Stored Proc GetUsers - takes two optional params UserType and AccountID and returns a list of user objects
public List<GetUsersResult> GetUsers(int? userType, int accountID) {
using (SqlConnection conn = new SqlConnection(context.Database.GetConnectionString())) {
string stpName = "GetUsers";
DynamicParameters stpParams = new DynamicParameters();
stpParams.Add("UserType", dbType: DbType.Int32, direction: ParameterDirection.Input);
stpParams.Add("ReturnDeviceID", dbType: DbType.Int32, direction: ParameterDirection.Input);
var result = conn.Query<GetUSersResult>(stpName, stpParams, commandType: CommandType.StoredProcedure);
return result;
}
}
// Stored Proc AddUser - takes two params Email and Name and creates a new user and returns the ID of the new record
public int AddUser(string email, string name) {
using (SqlConnection conn = new SqlConnection(context.Database.GetConnectionString())) {
string stpName = "GetUsers";
DynamicParameters stpParams = new DynamicParameters();
stpParams.Add("Email", dbType: DbType.String, direction: ParameterDirection.Input);
stpParams.Add("Name", dbType: DbType.String, direction: ParameterDirection.Input);
stpParams.Add("ReturnID", dbType: DbType.Int32, direction: ParameterDirection.Output);
var result = conn.Execute(stpName, stpParams, commandType: CommandType.StoredProcedure);
return result;
}
}
The reason for this is that it would allow us to keep a strongly typed Dapper SP call so that if we update the database it would update all the functions, therefore adding/removing parameters to our procedures would be reflected in every function call throughout our projects and ensure it gets covered when building.
I use a mix of both EF and Dapper in my projects and this would be an awesome addition to simply use this tool to generate the code and leverage the already established classes and database context.
Issue Analytics
- State:
- Created 2 years ago
- Comments:17 (11 by maintainers)
Top GitHub Comments
Yea that could work, or maybe as a static class created under the specified namespace from the tool?
@nathanh2011 I have created an issue for that!