Comments (5)
My understanding of Dapper was that a supplied connection, if originally closed, will be opened and subsequently closed when an operation is complete. If you are injecting an IDataAccessor
, the connection should not be opened by default, correct?
Here's a snippet from the Dapper library: https://github.com/StackExchange/Dapper/blob/master/Dapper/SqlMapper.cs#L528
from dataabstractions.dapper.
There are two main reasons why I choose to implement a factory pattern in my designs:
- To support multi-threaded scenarios. There are cases when I want to execute multiple queries concurrently, and you are not able to do that safely with a single database connection.
- The ability resume execution when the connection object is in a "bad state". This is an rare scenario, but if the db connection becomes corrupted and is irrecoverable, having a factory allows me to recover and continue execution or even attempt retry logic if desired.
Also, it is generally "best practice" to dispose of connection objects per query as a safety measure. Especially since there is no performance impacts with creating connection objects because of database connection pooling.
from dataabstractions.dapper.
In my designs, I typically create a factory class that creates instances of IDataAccessor. You can then inject the factory into your constructor and create an IDataAccessor as needed in your individual methods with using statements. In your tests, you can then mock out the factory and IDataAccessor as well. One alternative is to use a factory method with your DI (if your DI container supports it), instead of creating your own factory class.
Another option... If its a web application, you can use constructor injection and register the IDataAccessor under a request scope lifecycle with your DI. Then use your DI lifecycle hooks/interceptors to create, open, close, and dispose the IDataAccessor automatically before and after the request. That way you don't have to manage the connection in your code.
from dataabstractions.dapper.
If you didn't need to use a factory, and could just inject this service by having the service deal with the disposal of the connection, this would be a pretty elegant way to inject SQL commands into a service and still allowing the service to be thoroughly unit tested, I'm not sure why you are abstracting it out by adding a really unneeded factory in the middle.
from dataabstractions.dapper.
There are two main reasons why I choose to implement a factory pattern in my designs:
- To support multi-threaded scenarios. There are cases when I want to execute multiple queries concurrently, and you are not able to do that safely with a single database connection.
- The ability resume execution when the connection object is in a "bad state". This is an rare scenario, but if the db connection becomes corrupted and is irrecoverable, having a factory allows me to recover and continue execution or even attempt retry logic if desired.
Also, it is generally "best practice" to dispose of connection objects per query as a safety measure. Especially since there is no performance impacts with creating connection objects because of database connection pooling.
We used a factory for these exact reasons, plus it also give us the opportunity to handle token management for Azure SQL managed identity connections.
p.s. Love the library, thanks so much @codeapologist
from dataabstractions.dapper.
Related Issues (5)
- QueryMultiple HOT 1
- DbConnection vs IDbConnection HOT 2
- IDataReader parsing HOT 2
- Update to latest version of Dapper HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dataabstractions.dapper.