.NETCore Sqlserver下对Dapper的扩展支持

我自横刀向天笑,去留肝胆两昆仑。这篇文章主要讲述.NETCore Sqlserver下对Dapper的扩展支持相关的知识,希望能为你提供帮助。
这里我们自定义一个IServiceCollection的扩展,例如下面我的扩展

services.AddDapperContext(dapperoptions => { dapperoptions.ConnectionString = "Data Source=192.168.0.42; Initial Catalog=NET.Core; User ID=sa; password=lym123!@#; Integrated Security=false"; });

添加了对数据库连接字符串设置,当然你也可以设置更多的参数,委托等等,这里简单演示下自定义dapper下的数据库访问,下面是扩展设置
1public static class DapperMiddlewareExtension 2{ 3 4public static IServiceCollection AddDapperContext< TDapperContext> (this IServiceCollection serviceCollection, Action< DapperOptions> optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TDapperContext : DapperContext 5{ 6serviceCollection.Configure(optionsAction); 7serviceCollection.AddTransient< IDataProvider, SqlServerDataProvider> (); 8serviceCollection.AddSingleton< TDapperContext> (); 9return serviceCollection; 10} 11/// < summary> 12/// 添加服务 13/// < /summary> 14/// < param name="serviceCollection"> < /param> 15/// < param name="optionsAction"> < /param> 16/// < param name="contextLifetime"> < /param> 17/// < param name="optionsLifetime"> < /param> 18/// < returns> < /returns> 19public static IServiceCollection AddDapperContext(this IServiceCollection serviceCollection, Action< DapperOptions> optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) 20{ 21serviceCollection.Configure(optionsAction); 22serviceCollection.AddTransient< IDataProvider, SqlServerDataProvider> (); 23serviceCollection.AddSingleton< DapperContext> (); 24return serviceCollection; 25}

这里DI相关的数据库访问类,这里最终要的一点就是我们在startup中设置的连接的字符串,在数据库DI类中怎么得到来访问数据库呢?
serviceCollection.Configure(optionsAction); 将委托Action配置到IOptions接口中
下面来看下我们的DapperContext

public class DapperContext { DapperOptions _dapperOptions; IDataProvider _dataProvider; public DapperContext(IOptions< DapperOptions> options, IDataProvider dataProvider) { _dapperOptions = options.Value; _dataProvider = dataProvider; }#region 创建Dapper相关连接private IDbConnection CreateConnection(bool ensureClose = true) {var conn = _dataProvider.CreateConnection(); conn.ConnectionString = _dapperOptions.ConnectionString; conn.Open(); return conn; } private IDbConnection _connection; private IDbConnection Connection { get { if (_connection == null || _connection.State != ConnectionState.Open) { _connection = CreateConnection(); }return _connection; } }public void insertTest(string sql) {var conn = Connection; try { conn.Execute(sql); }finally { if (_connection != null) { _connection.Close(); _connection = null; } }}

 
在写好相关的数据库访问连接类处理
建立自己的业务服务,这里写的比较简单

public interface ICustomDapperContext {void Insert(string sql); }

 
public class CustomDapperContext : ICustomDapperContext { DapperContext _dapperContext; public CustomDapperContext(DapperContext dapperContext) { _dapperContext = dapperContext; } public void Insert(string sql) { _dapperContext.insertTest(sql); } }

 
然后在Controller层DI下

ICustomDapperContext _context; public HomeController(ICustomDapperContext context) { _context = context; } public IActionResult Index() { _context.Insert("insert into Tb_UserLogin(UserName,UserPwd,[Order],IsDelete) values (\'UserName\',\'UserName\',0,0)"); return View(); }

执行后数据库添加成功
.NETCore Sqlserver下对Dapper的扩展支持

文章图片

【.NETCore Sqlserver下对Dapper的扩展支持】 
 
下面是我自定义的中间件的相关类

.NETCore Sqlserver下对Dapper的扩展支持

文章图片

 

 

    推荐阅读