zoukankan      html  css  js  c++  java
  • .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下的数据库访问,下面是扩展设置

     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();
            }

    执行后数据库添加成功

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

    
    
    
  • 相关阅读:
    实用机器学习 跟李沐学AI
    Explicitly drop temp table or let SQL Server handle it
    dotnettransformxdt and FatAntelope
    QQ拼音输入法 禁用模糊音
    (技术八卦)Java VS RoR
    Ruby on rails开发从头来(windows)(七)创建在线购物页面
    Ruby on rails开发从头来(windows)(十三)订单(Order)
    Ruby on rails开发从头来(windows)(十一)订单(Order)
    新员工自缢身亡,华为又站到了风口浪尖
    死亡汽油弹(Napalm Death)乐队的视频和来中国演出的消息
  • 原文地址:https://www.cnblogs.com/liyouming/p/8303812.html
Copyright © 2011-2022 走看看