码迷,mamicode.com
首页 > 移动开发 > 详细

.NETCore Sqlserver下对Dapper的扩展支持

时间:2018-01-17 18:28:01      阅读:1295      评论:0      收藏:0      [点我收藏+]

标签:演示   time   figure   life   interface   业务   conf   tap   ansi   

这里我们自定义一个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<TDapperContext>(this IServiceCollection serviceCollection, Action<DapperOptions> optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TDapperContext : DapperContext
 5         {
 6             serviceCollection.Configure(optionsAction);
 7             serviceCollection.AddTransient<IDataProvider, SqlServerDataProvider>();
 8             serviceCollection.AddSingleton<TDapperContext>();
 9             return 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>
19         public static IServiceCollection AddDapperContext(this IServiceCollection serviceCollection, Action<DapperOptions> optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) 
20         {
21             serviceCollection.Configure(optionsAction);
22             serviceCollection.AddTransient<IDataProvider, SqlServerDataProvider>();
23             serviceCollection.AddSingleton<DapperContext>();
24             return 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的扩展支持

标签:演示   time   figure   life   interface   业务   conf   tap   ansi   

原文地址:https://www.cnblogs.com/liyouming/p/8303812.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!