[英]Where should IDBConnection be put to reduce repeated code?
Most Dapper tutorials use a private IDBConnection
object to call methods ie 大多数Dapper教程使用私有
IDBConnection
对象来调用方法,即
private IDbConnection db = new SqlConnection(...)
When using ASP.NET and MVC 5, where should I put this so I do not have to repeat it in every controller/repository using Dapper? 当使用ASP.NET和MVC 5时,应该放在哪里,这样就不必在使用Dapper的每个控制器/存储库中都重复它? For instance, is there a way to put this in a startup class and use dependency injection like in ASP.NET Core, or some other technique to access it throughout the application?
例如,是否可以将其放在启动类中并像ASP.NET Core中那样使用依赖项注入,或通过其他方法在整个应用程序中访问它?
Best connection creation mechanism as per my experience is the combination of DependencyInjection
and ConnectionFactory
. 根据我的经验,最好的连接创建机制是
DependencyInjection
和ConnectionFactory
的组合。
Advantages are Multi fold: 多重优势:
What you shall do (in Code): 您应该做什么(用Code):
Declare the IDBConnection
object in the Data access Layer: 在数据访问层中声明
IDBConnection
对象:
[Inject] // Property Injection
public IDBConnection Connection {get; set;}
Declare the binding using a DI framework like Ninject: 使用像Ninject这样的DI框架声明绑定:
Bind<IDBConnection>().ToMethod(ctx =>
ConnectionFactory.CreateDbConnection("DefaultConnection"));
Create the DBConnection Factory as follows: 创建DBConnection工厂,如下所示:
Connection factory fetches the Connection provider and connection string from the config file as follows: 连接工厂从配置文件中获取连接提供程序和连接字符串,如下所示:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=<Value>;Initial Catalog=<Value>;User Id=<Value>;Password=<Value>" providerName="System.Data.SqlClient" />
</connectionStrings>
Identifier is DefaultConnection
, which is using the SqlClient provider, but at run time can be changed to the different client like Oracle, MySql
标识符是
DefaultConnection
,它使用SqlClient提供程序,但在运行时可以更改为其他客户端,例如Oracle, MySql
using System;
using System.Data.Common;
public static class ConnectionFactory
{
/// <summary>
/// Create DBConnection type based on provider name and connection string
/// </summary>
/// <param name="connectionIdentifier"></param>
/// <returns></returns>
public static DbConnection CreateDbConnection(string connectionIdentifier)
{
// Provider name setting
var providerNameValue = ConfigurationManager.ConnectionStrings[connectionIdentifier].ProviderName;
// Connection string setting
var connectionStringValue = ConfigurationManager.ConnectionStrings[connectionIdentifier].ConnectionString;
// Assume failure.
DbConnection connection;
// Null connection string cannot be accepted
if (connectionStringValue == null) return null;
// Create the DbProviderFactory and DbConnection.
try
{
// Fetch provider factory
var factory = DbProviderFactories.GetFactory(providerNameValue);
// Create Connection
connection = factory.CreateConnection();
// Assign connection string
if (connection != null)
connection.ConnectionString = connectionStringValue;
}
catch (Exception ex)
{
connection = null;
}
// Return the connection.
return connection;
}
}
How to use it: 如何使用它:
For a single call and dispose 只需一个电话即可处理
using(Connection)
{
...
}
For a Transaction context, use as-is, no using
required 对于事务上下文,请按原样
using
,而无需using
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.