SQLServer数据库之.NETCore Sqlserver下对Dapper的扩展支持
小标 2018-08-31 来源 : 阅读 1749 评论 0

摘要:本文主要向大家介绍了SQLServer数据库之.NETCore Sqlserver下对Dapper的扩展支持,通过具体的内容向大家展现,希望对大家学习SQLServer数据库有所帮助。

本文主要向大家介绍了SQLServer数据库之.NETCore Sqlserver下对Dapper的扩展支持,通过具体的内容向大家展现,希望对大家学习SQLServer数据库有所帮助。

这里我们自定义一个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下的数据库访问,下面是扩展设置

 1  public static class DapperMiddlewareExtension
 2     {
 3 
 4         public static IServiceCollection AddDapperContext(this IServiceCollection serviceCollection, Action optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TDapperContext : DapperContext
 5         {
 6             serviceCollection.Configure(optionsAction);
 7             serviceCollection.AddTransient();
 8             serviceCollection.AddSingleton();
 9             return serviceCollection;
10         }
11         /// 


12         /// 添加服务
13         /// 


14         /// 


15         /// 


16         /// 


17         /// 


18         /// 


19         public static IServiceCollection AddDapperContext(this IServiceCollection serviceCollection, Action

 optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) 
20         {
21             serviceCollection.Configure(optionsAction);
22             serviceCollection.AddTransient();
23             serviceCollection.AddSingleton();
24             return serviceCollection;
25         }

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

 public class DapperContext 
    {
        DapperOptions _dapperOptions;
        IDataProvider _dataProvider;
        public DapperContext(IOptions 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();
        }

执行后数据库添加成功

 
 
下面是我自定义的中间件的相关类 

本文由职坐标整理并发布,希望对同学们学习SQL Server有所帮助,更多内容请关注职坐标数据库SQL Server数据库频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 0 不喜欢 | 0
看完这篇文章有何感觉?已经有0人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved