如何在 .NET 中使用 Dapper 处理数据库连接?
我一直在使用 Dapper,但我不确定处理数据库连接的最佳方法。
大多数示例显示在示例类中甚至在每个方法中创建的连接对象。但我觉得在每个类中引用连接字符串是错误的,即使它是从 web.config
中提取的。
我的经验是将 DbDataContext
或 DbContext
与 LINQ to SQL 或实体框架结合使用,因此这对我来说是新的。
使用 Dapper 作为我的数据访问策略时,如何构建我的 Web 应用程序?
I've been playing with Dapper, but I'm not sure of the best way to handle the database connection.
Most examples show the connection object being created in the example class, or even in each method. But it feels wrong to me to reference a connection string in every class, even if it's pulling from the web.config
.
My experience has been with using a DbDataContext
or DbContext
with LINQ to SQL or Entity Framework, so this is new to me.
How do I structure my web apps when using Dapper as my Data Access strategy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
更新:MarredCheese 评论的澄清:
Microsoft.AspNetCore.All:v2.0.3 | Dapper:v1.50.2
我不确定我是否正确使用了最佳实践,但我这样做是为了处理多个连接字符串。
如果你只有 1 个连接字符串,这很容易
Startup.cs
DiameterRepository.cs
如果你有超过 1 个连接字符串,就会出现问题
由于
Dapper
使用IDbConnection
,你需要想办法区分不同的数据库连接。我尝试创建多个接口,“继承”自
IDbConnection
,对应不同的数据库连接,并在Startup
上注入具有不同数据库连接字符串的SqlConnection
。失败是因为
SqlConnection
继承自DbConnection
,并且DbConnection
不仅实现了IDbConnection
,还实现了IComponent
代码>接口。因此,您的自定义接口将无法仅使用SqlConnection
实现。我还尝试创建自己的
DbConnection
类,该类采用不同的连接字符串。这太复杂了,因为您必须实现 DbConnection 类中的所有方法。您失去了SqlConnection
的帮助。我最终做了什么
启动
期间,我将所有连接字符串值加载到字典中。我还为所有数据库连接名称创建了一个enum
以避免魔术字符串。IDbConnection
,而是创建了IDbConnectionFactory
并将其作为所有存储库的 Transient 注入。现在,所有存储库都采用IDbConnectionFactory
而不是IDbConnection
。DatabaseConnectionName.cs
IDbConnectionFactory.cs
DapperDbConenctionFactory - 我自己的工厂实现
Startup.cs
DiameterRepository.cs
DbConnection1RepositoryBase.cs
然后,对于需要与其他连接通信的其他存储库,您可以为它们创建不同的存储库基类。
希望所有这些有所帮助。
Update: clarification from MarredCheese's comment:
Microsoft.AspNetCore.All: v2.0.3 | Dapper: v1.50.2
I am not sure if I am using the best practices correctly or not, but I am doing it this way, in order to handle multiple connection strings.
It's easy if you have only 1 connection string
Startup.cs
DiameterRepository.cs
Problems if you have more than 1 connection string
Since
Dapper
utilizesIDbConnection
, you need to think of a way to differentiate different database connections.I tried to create multiple interfaces, 'inherited' from
IDbConnection
, corresponding to different database connections, and injectSqlConnection
with different database connection strings onStartup
.That failed because
SqlConnection
inherits fromDbConnection
, andDbConnection
implements not onlyIDbConnection
but alsoIComponent
interface. So your custom interfaces won't be able to use just theSqlConnection
implementation.I also tried to create my own
DbConnection
class that takes different connection strings. That's too complicated because you have to implement all the methods from theDbConnection
class. You lost the help fromSqlConnection
.What I end up doing
Startup
, I loaded all connection string values into a dictionary. I also created anenum
for all the database connection names to avoid magic strings.IDbConnection
, I createdIDbConnectionFactory
and injected that as Transient for all repositories. Now all repositories takeIDbConnectionFactory
instead ofIDbConnection
.DatabaseConnectionName.cs
IDbConnectionFactory.cs
DapperDbConenctionFactory - my own factory implementation
Startup.cs
DiameterRepository.cs
DbConnection1RepositoryBase.cs
Then for other repositories that need to talk to the other connections, you can create a different repository base class for them.
Hope all these helps.
大约 4 年前有人问过……但无论如何,也许答案对这里的某人有用:
我在所有项目中都这样做。
首先,我创建一个基类,其中包含一些像这样的帮助器方法:
并且拥有这样的基类,我可以轻松创建真正的存储库,而无需任何样板代码:
因此,与 Dapper、SqlConnection-s 和其他数据库访问内容相关的所有代码都是位于一处(BaseRepository)。所有真实的存储库都是干净且简单的 1 行方法。
我希望它能帮助某人。
It was asked about 4 years ago... but anyway, maybe the answer will be useful to someone here:
I do it like this in all the projects.
First, I create a base class which contains a few helper methods like this:
And having such a base class I can easily create real repositories without any boilerplate code:
So all the code related to Dapper, SqlConnection-s and other database access stuff is located in one place (BaseRepository). All real repositories are clean and simple 1-line methods.
I hope it will help someone.
我创建了带有从配置中检索连接字符串的属性的扩展方法。这让调用者不必知道有关连接的任何信息,无论是打开还是关闭等。此方法确实会限制您,因为您隐藏了一些 Dapper 功能,但在我们相当简单的应用程序中,它对我们来说工作得很好,如果我们需要 Dapper 的更多功能,我们总是可以添加一个新的扩展方法来公开它。
I created extension methods with a property that retrieves the connection string from configuration. This lets the callers not have to know anything about the connection, whether it's open or closed, etc. This method does limit you a bit since you're hiding some of the Dapper functionality, but in our fairly simple app it's worked fine for us, and if we needed more functionality from Dapper we could always add a new extension method that exposes it.
我这样做:
然后,无论我在何处连接依赖项(例如:Global.asax.cs 或 Startup.cs),我都会执行以下操作:
I do it like this:
Then, wherever I wire-up my dependencies (ex: Global.asax.cs or Startup.cs), I do something like:
最佳实践是一个真正负载的术语。我喜欢
DbDataContext
样式容器,例如 Dapper .Rainbow 推广。它允许您耦合 CommandTimeout、事务和其他帮助程序。例如:
Best practice is a real loaded term. I like a
DbDataContext
style container like Dapper.Rainbow promotes. It allows you to couple theCommandTimeout
, transaction and other helpers.For example:
每个人似乎都太早地开放了他们的联系?我有同样的问题,在挖掘了这里的源代码之后 - https://github.com/StackExchange/dapper-dot-net/blob/master/Dapper/SqlMapper.cs
你会发现每次与数据库的交互都会检查连接是否是关闭,并根据需要打开它。因此,我们只需使用上面的 using 语句,而不使用 conn.open()。这样,连接的打开就尽可能接近交互。如果您注意到,它也会立即关闭连接。这也比在处理过程中自动关闭要快。
上面的存储库中的许多示例之一:
下面是一个小示例,说明我们如何使用名为 DapperWrapper 的 Dapper 的 Wrapper。这使我们能够包装所有 Dapper 和 Simple Crud 方法来管理连接、提供安全性、日志记录等。
Everyone appears to be opening their connections entirely too early? I had this same question, and after digging through the Source here - https://github.com/StackExchange/dapper-dot-net/blob/master/Dapper/SqlMapper.cs
You will find that every interaction with the database checks the connection to see if it is closed, and opens it as necessary. Due to this, we simply utilize using statements like above without the conn.open(). This way the connection is opened as close to the interaction as possible. If you notice, it also immediately closes the connection. This will also be quicker than it closing automatically during disposal.
One of the many examples of this from the repo above:
Below is a small example of how we use a Wrapper for Dapper called the DapperWrapper. This allows us to wrap all of the Dapper and Simple Crud methods to manage connections, provide security, logging, etc.
试试这个:
Try this:
我用助手类包装连接:
使用示例:
所以我不必每次都显式打开连接。
另外,也可以这样使用,方便以后重构:
TableName()
是什么可以在 这个答案。I wrap connection with the helper class:
Examples of usage:
So I don't have to explicitly open the connection every time.
Additionally, you can use it this way for the convenience' sake of the future refactoring:
What is
TableName<T>()
can be found in this answer.嗨@donaldhughes,我也是新手,我经常这样做:
1 - 创建一个类来获取我的连接字符串
2 - 在使用外观中调用连接字符串类
:
DapperConnection.cs
DapperRepository.cs
它工作正常。
Hi @donaldhughes I'm new on it too, and I use to do this:
1 - Create a class to get my Connection String
2 - Call the connection string class in a Using
Look:
DapperConnection.cs
DapperRepository.cs
And it works fine.