生活随笔
收集整理的這篇文章主要介紹了
                                
让Dapper在一个项目中支持多种库
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
 
                                
                            
                            
                            如果想在一個(gè)項(xiàng)目中,用DapperPlus支持多種數(shù)據(jù)庫該怎么做?
在《讓Dapper支持Mock》中我們定義了DapperPlus,可以基于這個(gè)類,實(shí)現(xiàn)兩個(gè)子類:MySqlDapperPlus,MsSqlDapperPls,在這兩個(gè)子類的構(gòu)造中適配對(duì)應(yīng)的數(shù)據(jù)庫類型,從注放容器中,獲取IDbConnection實(shí)例,根據(jù)實(shí)例的類型來選取配置中的對(duì)應(yīng)連接字符串,這里用到的是根據(jù)數(shù)據(jù)類型來配置,也是一種約定。
MySqlDapperPlus.cs
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;namespace WebDemo01.Services
{public class MySqlDapperPlus : DapperPlus{public MySqlDapperPlus(IEnumerable<IDbConnection> connections, IConfiguration configuration){var connectionStrings = configuration.GetSection("ConnectionStrings").Get<Dictionary<string, string>>();          _connection = connections.FirstOrDefault(c => c.GetType().Name == "MySqlConnection");_connection.ConnectionString = connectionStrings.Where(s => s.Key.ToLower().Contains("mysql")).FirstOrDefault().Value;}}
}
MsSqlDapperPlus.cs
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;namespace WebDemo01.Services
{public class MsSqlDapperPlus : DapperPlus{public MsSqlDapperPlus(IEnumerable<IDbConnection> connections, IConfiguration configuration){var connectionStrings = configuration.GetSection("ConnectionStrings").Get<Dictionary<string, string>>();          _connection = connections.FirstOrDefault(c => c.GetType().Name == "SqlConnection");_connection.ConnectionString = connectionStrings.Where(s => s.Key.ToLower().Contains("mssql")).FirstOrDefault().Value;}}
}
這時(shí),會(huì)有問題,DapperPlus沒有無參構(gòu)造,_connection訪問級(jí)別也太低,所以要改造一下DapperPlus。
    /// <summary>/// DappePlusr類/// </summary>public class DapperPlus : IDapperPlus{protected IDbConnection _connection;/// <summary>/// 無參構(gòu)造函數(shù)/// </summary>public DapperPlus(){}//下面和原來的一樣}
public?void?ConfigureServices(IServiceCollection?services)
{services.AddControllers();services.AddScoped<IDbConnection,?MySqlConnection>();services.AddScoped<IDbConnection,?SqlConnection>();services.AddScoped<IDapperPlus,?MySqlDapperPlus>();services.AddScoped<IDapperPlus,?MsSqlDapperPlus>();
}
appsettings.json
??"ConnectionStrings":?{"MySqlConnectionString":?"server=127.0.0.1;uid=root;pwd=root;database=mysql_testdb","MsSqlConnectionString":?"server=127.0.0.1;uid=root;pwd=root;database=mssql_testdb"}
如果是多個(gè)庫,還要讀寫分離該怎么實(shí)現(xiàn)?其實(shí)和不分離是一樣的,要改造《讓Dapper讀寫分離》中的DapperPlusWrite和DapperPlusRead兩個(gè)類,分別增加無參構(gòu)造函數(shù),和把_connection改成protected,方便子類中參訪問到。
然后定義三個(gè)類:MySqlDapperPlusRead和MsSqlDapperPlusRead繼承DapperPlusRead;MySqlDapperPlusWrite和MsSqlDapperPlusWrite繼承DapperPlusWrite。在四個(gè)類的構(gòu)造函數(shù)中,按照自己數(shù)據(jù)庫的類型,Read或Write類型來取配置文件中的連接字符串即可。
Startup.cs
public?void?ConfigureServices(IServiceCollection?services)
{services.AddControllers();services.AddScoped<IDbConnection,?MySqlConnection>();services.AddScoped<IDbConnection, SqlConnection>();services.AddScoped<IDapperPlusRead,?MySqlDapperPlusRead>();services.AddScoped<IDapperPlusRead,?MsSqlDapperPlusRead>();services.AddScoped<IDapperPlusWrite,?MySqlDapperPlusWrite>();services.AddScoped<IDapperPlusWrite,?MsSqlDapperPlusWrite>();
}
appsettings.json
  "ConnectionStrings": {"MySqlReadConnectionString": "server=127.0.0.1;uid=root;pwd=root;database=read_mysql_testdb","MySqlWriteConnectionString": "server=127.0.0.1;uid=root;pwd=root;database=write_mysql_testdb","MsSqlReadConnectionString": "server=127.0.0.1;uid=root;pwd=root;database=read_mssql_testdb","MsSqlWriteConnectionString": "server=127.0.0.1;uid=root;pwd=root;database=write_mssql_testdb"}
最后,在業(yè)務(wù)的Service中,讀有兩個(gè),按類型區(qū)分,寫有兩個(gè),按類型區(qū)分,代碼如下:
????public?class?GoodsService?:?IGoodsService{private readonly IDapperPlusWrite _mySqlDapperWrite;private readonly IDapperPlusWrite _msSqlDapperWrite;private readonly IDapperPlusRead _mySqlDapperRead;private readonly IDapperPlusRead _msSqlDapperRead;public ShopService(IEnumerable<IDapperPlusWrite> dapperWrites, IEnumerable<IDapperPlusRead> dapperReads){foreach (var dapperWrite in dapperWrites){switch (dapperWrite){case MySqlDapperPlusWrite mySqlDapperPlusWrite:_mySqlDapperWrite = mySqlDapperPlusWrite;break;case MsSqlDapperPlusWrite msSqlDapperPlusWrite:_msSqlDapperWrite = msSqlDapperPlusWrite;break;}}foreach (var dapperRead in dapperReads){switch (dapperRead){case MySqlDapperPlusRead mySqlDapperPlusRead:_mySqlDapperRead = mySqlDapperPlusRead;break;case MsSqlDapperPlusRead msSqlDapperPlusRead:_msSqlDapperRead = msSqlDapperPlusRead;break;}}}}
                            
總結(jié)
                            
                                以上是生活随笔為你收集整理的让Dapper在一个项目中支持多种库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                            
                                如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。