Description
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.
Activity