To create a new object, I'm using.NET Core Dependency Injection.
object during application launch and intend to inject it into my repository afterwards. This
will be used by Dapper to read and write information from the database within my repository implementation. I'm going to employ
Should I administer the injection?
as a singleton or as a transient? Taking into account that I want to utilize
Transient would be my choice until Dapper supports isolation containers internally, in which case my singleton's scope would still be contained to the scope that Dapper employs internally.
Are there any suggestions or best practices for how long the SqlConnection object should last while using Dapper? Are there any warnings that I could be overlooking?
I appreciate it.
If you supply a single SQL connection, you cannot fulfill several requests concurrently unless you activate MARS, which has its own restrictions. Using temporary SQL connections and making sure they are appropriately disposed of is recommended.
In my applications, I provide unique
to the repositories used to establish connections within
statement. In this situation, the repository itself may be a singleton to save memory allocations.
In both the response and the reply, I agree with @Andrii Litvinov.
In this case I would go with approach of data-source specific connection factory.
I'll discuss a another method using the same methodology: UnitOfWork.
respond from zzz-21 zzz. This deals with connections.
respond from zzz-31 zzz. This is how I've done it.
UnitOfWorkis administered as a brief period.
DalSessionevery data source.
Are there any recommendations/best practices regarding the lifetime of the SqlConnection object when working with Dapper?
The majority of developers agree that connections should be made as quickly as feasible. Here, I see two strategies:
usingfor each action, a block. As long as you don't wish to group the activities, this strategy is good. Most of the time, you can utilize transactions even when you wish to aggregate the operations.
usingblock this. The UnitOfWork solution is as follows.