讓Dapper在一個項目中支持多種庫

如果想在一個項目中,用DapperPlus支持多種數(shù)據(jù)庫該怎么做?
在《讓Dapper支持Mock》中我們定義了DapperPlus,可以基于這個類,實現(xiàn)兩個子類:MySqlDapperPlus,MsSqlDapperPls,在這兩個子類的構造中適配對應的數(shù)據(jù)庫類型,從注放容器中,獲取IDbConnection實例,根據(jù)實例的類型來選取配置中的對應連接字符串,這里用到的是根據(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沒有無參構造,_connection訪問級別也太低,所以要改造一下DapperPlus。
/// <summary>/// DappePlusr類/// </summary>public class DapperPlus : IDapperPlus{protected IDbConnection _connection;/// <summary>/// 無參構造函數(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兩個類,分別增加無參構造函數(shù),和把_connection改成protected,方便子類中參訪問到。
然后定義三個類:MySqlDapperPlusRead和MsSqlDapperPlusRead繼承DapperPlusRead;MySqlDapperPlusWrite和MsSqlDapperPlusWrite繼承DapperPlusWrite。在四個類的構造函數(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è)務的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;}}}}
【推薦】.NET Core開發(fā)實戰(zhàn)視頻課程 ★★★
.NET Core實戰(zhàn)項目之CMS 第一章 入門篇-開篇及總體規(guī)劃
【.NET Core微服務實戰(zhàn)-統(tǒng)一身份認證】開篇及目錄索引
Redis基本使用及百億數(shù)據(jù)量中的使用技巧分享(附視頻地址及觀看指南)
.NET Core中的一個接口多種實現(xiàn)的依賴注入與動態(tài)選擇看這篇就夠了
用abp vNext快速開發(fā)Quartz.NET定時任務管理界面
在ASP.NET Core中創(chuàng)建基于Quartz.NET托管服務輕松實現(xiàn)作業(yè)調(diào)度
