我发现了很多存储库模式的示例,所有这些示例都显示存储库管理它自己的连接生命周期。我想知道人们如何处理他们想要跨多个存储库共享单个连接的情况?
我问的主要原因是因为当我使用 TransactionScope 创建事务时,我真的不希望它升级为 DTC 事务。虽然设置简单,但它似乎有点重量级。
我在想的是使用类似于 TransactionScope 的东西来管理连接生命周期。但是,似乎将数据访问与业务流程联系了一点。我的意思的一个例子是:
//As DataScope will handle connections, then repositories will be created from them
//in order to share the connection.
using(DataScope scope = new DataScope())
{
scope.GetRepository<CustomerRepository>.FindById(10)
}