question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature Request: Add IMyContextProcedures interface class

See original GitHub issue

First 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:closed
  • Created 2 years ago
  • Comments:17 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
nathanh2011commented, Jan 19, 2022

Yea that could work, or maybe as a static class created under the specified namespace from the tool?

0reactions
ErikEJcommented, Jan 21, 2022

@nathanh2011 I have created an issue for that!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Feature Request: add interfaces to all public classes #23
I would have expected to be able to use the dependency injection with this package (in order to create my own wrapper around...
Read more >
When to add an interface to a class
I'm currently revising my book "Principles of Package Design". It covers lots of design principles, like the SOLID principles and the lesser ...
Read more >
php - How to pass different custom requests with common ...
Making Interface class for Crud operations I need in some cases to pass custom requests(ex. CreateTagReques)t and I make it with calling ...
Read more >
Should every class I write adhere to an interface?
I'm writing a game in Typescript, and decided going in that I was going to try to adhere to the idea of "interface...
Read more >
Why does everything have to use interface? : r/dotnet
The interfaces should ADD clarity and commentary to the class declaration, rather than just being a duplicate of the class name with "I"...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found