-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: How does this work for DI and also IDisposable? #5
Comments
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. |
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 |
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. |
There are two main reasons why I choose to implement a factory pattern in my designs:
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 |
We currently use this library to be able to unit test our repository classes but now we are in a situation where we are not utilising connection pooling properly because the connection is already created (and injected) when the repository is invoked, you cannot use the normal
using
syntax when creating a connection inside an individual method.I can't think of the correct pattern:
Use the normal
using ( var connection = new DataAccessor(...) )
which disposes and pools correctly but prevents constructor injection being possible so unit tests won't work.Inject IDataAccessor into the constructor as we currently do. This allows unit tests but does not permit the use of using, which means we should probably open the connection in the constructor and ensure our DI services are created in a liftetime scope so they don't tie up the connection for too long.
Inject the IDataAccessor into the constructor but don't open it and then in every method, call Open() and Close(). This would generate more code but would ensure we are not holding onto connections and not opening the connection if we don't need it (since we are using EF for some parts of the repositories)
Have constructor overloads one that takes IDataAccessor for unit tests and one that doesn't but then how would a method use the correct connection e.g. if the shared conn from the unit tests is available, use that, otherwise use the
using
syntax. This sounds messy.Do you have any ideas? Thanks.
The text was updated successfully, but these errors were encountered: