Where to put SQL Parameters in a layered application?

Hamster

Resident Rodent
Joined
Aug 22, 2006
Messages
42,920
Is Dapper only for ASP.NET?

Also if someone could suggest me a resource to get started else I am just going to smash codeproject and see what I can find.

eg. https://www.codeproject.com/articles/212274/a-look-at-dapper-net

Just for .NET unless there was a port (developed by StackOverflow). http://www.infoworld.com/article/3025784/application-development/how-to-work-with-dapper-in-c.html <- seems to be allright.

Dapper basically adds two extension methods on your IDbConnection object (ok, it does whole lot more but bare with me). You carry on exactly as you were by opening SqlConnections and commands, but to execute or query you'll use conn.Query<T> or conn.Execute<T>. Added benefit, it autmatically maps the results to your class if the property names match the column names (there are overrides available).

Not difficult.
 

Kosmik

Honorary Master
Joined
Sep 21, 2007
Messages
25,652
Dapper works very well, light and still object wrapping. Also look at Dapper.Contrib.
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,885
Just for .NET unless there was a port (developed by StackOverflow). http://www.infoworld.com/article/3025784/application-development/how-to-work-with-dapper-in-c.html <- seems to be allright.

Dapper basically adds two extension methods on your IDbConnection object (ok, it does whole lot more but bare with me). You carry on exactly as you were by opening SqlConnections and commands, but to execute or query you'll use conn.Query<T> or conn.Execute<T>. Added benefit, it autmatically maps the results to your class if the property names match the column names (there are overrides available).

Not difficult.

OK that is awesome. Thansk!
 

animal531

Expert Member
Joined
Nov 12, 2013
Messages
2,728
For a learning experience it's good to write your own. But from a production/speed point of view it's usually far better to use an existing code base.

Dapper is an object mapper, you can use it in any .NET project type.
Just import from Nuget. I usually use a variety of the following, depending on what I want to do:
Dapper
Dapper.Extensions.Linq
Dapper.Mapper
Dapper.SimpleCRUD
Dapper.SimpleSave
Dapper.SimpleLoad

If you google the names you should get the hosted links on github with examples.
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,885
For a learning experience it's good to write your own. But from a production/speed point of view it's usually far better to use an existing code base.

Dapper is an object mapper, you can use it in any .NET project type.
Just import from Nuget. I usually use a variety of the following, depending on what I want to do:
Dapper
Dapper.Extensions.Linq
Dapper.Mapper
Dapper.SimpleCRUD
Dapper.SimpleSave
Dapper.SimpleLoad

If you google the names you should get the hosted links on github with examples.

Much appreciate. Busy getting my feet wet now with Dapper. It's.......not bad!
 

Spacerat

Expert Member
Joined
Jul 29, 2015
Messages
1,328
Def use some kind of ORM. All that mapping code WILL drive you insane. But it is good to know how it works under the hood.

To answer the original question, SqlParameter has no place in both the BusinessLayer and interface to the DataLayer. If you SqlParameter is DataLayer implementation specific.
 

etienne_marais

Honorary Master
Joined
Mar 16, 2008
Messages
15,093
Def use some kind of ORM. All that mapping code WILL drive you insane. But it is good to know how it works under the hood.

To answer the original question, SqlParameter has no place in both the BusinessLayer and interface to the DataLayer. If you SqlParameter is DataLayer implementation specific.

What she did is (fairly) ok though ? Passing criteria (custom object as opposed to sqlparameters) which can just as easily be used for a different datalayer implementation. Best to implement an interface (or perhaps abstract class derived) for switching between datalayer implementations if not already that, though.
 
Last edited:

Spacerat

Expert Member
Joined
Jul 29, 2015
Messages
1,328
What she did is (fairly) ok though ? Passing criteria (custom object as opposed to sqlparameters) which can just as easily be used for a different datalayer implementation. Best to implement an interface (or perhaps abstract class derived) for switching between datalayer implementations if not already that, though.

Absolutely correct... The use of a criteria object is also better than using individual arguments because extending the criteria then simply means adding the property to the criteria class. This does not change the interface whereas adding an argument would.

I would also implement an interface as you suggest and then do the implementation for Sql and also mocks. I use the mock implementations extensively when i create apps, especially when i know that the particular interface will talk to a webservice (or DB for that matter)
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,885
What she did is (fairly) ok though ? Passing criteria (custom object as opposed to sqlparameters) which can just as easily be used for a different datalayer implementation. Best to implement an interface (or perhaps abstract class derived) for switching between datalayer implementations if not already that, though.

Absolutely correct... The use of a criteria object is also better than using individual arguments because extending the criteria then simply means adding the property to the criteria class. This does not change the interface whereas adding an argument would.

I would also implement an interface as you suggest and then do the implementation for Sql and also mocks. I use the mock implementations extensively when i create apps, especially when i know that the particular interface will talk to a webservice (or DB for that matter)

+1 thanks guys especially with the use of an Interface that will be the next task.
 

Solarion

Honorary Master
Joined
Nov 14, 2012
Messages
21,885
I did not want to start a new thread but what is the latest .NET Framework? 4.6.1?
 
Top