日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

让Dapper在一个项目中支持多种库

發(fā)布時間:2023/12/4 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 让Dapper在一个项目中支持多种库 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

如果想在一個項目中,用DapperPlus支持多種數(shù)據(jù)庫該怎么做?

在《讓Dapper支持Mock》中我們定義了DapperPlus,可以基于這個類,實現(xiàn)兩個子類:MySqlDapperPlus,MsSqlDapperPls,在這兩個子類的構(gòu)造中適配對應(yīng)的數(shù)據(jù)庫類型,從注放容器中,獲取IDbConnection實例,根據(jù)實例的類型來選取配置中的對應(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;}} }

這時,會有問題,DapperPlus沒有無參構(gòu)造,_connection訪問級別也太低,所以要改造一下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"}

如果是多個庫,還要讀寫分離該怎么實現(xiàn)?其實和不分離是一樣的,要改造《讓Dapper讀寫分離》中的DapperPlusWrite和DapperPlusRead兩個類,分別增加無參構(gòu)造函數(shù),和把_connection改成protected,方便子類中參訪問到。

然后定義三個類:MySqlDapperPlusRead和MsSqlDapperPlusRead繼承DapperPlusRead;MySqlDapperPlusWrite和MsSqlDapperPlusWrite繼承DapperPlusWrite。在四個類的構(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中,讀有兩個,按類型區(qū)分,寫有兩個,按類型區(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)容還不錯,歡迎將生活随笔推薦給好友。