Skip to content

Commit 52600e8

Browse files
author
hchen2020
committed
Support multiple database.
1 parent d8ec3ee commit 52600e8

File tree

12 files changed

+67
-59
lines changed

12 files changed

+67
-59
lines changed

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public partial class AgentService
66
{
77
public async Task<Agent> CreateAgent(Agent agent)
88
{
9-
var db = _services.GetRequiredService<AgentDbContext>();
9+
var db = _services.GetRequiredService<BotSharpDbContext>();
1010
var record = db.Agent.FirstOrDefault(x => x.OwnerId == _user.Id && x.Name == agent.Name);
1111
if (record != null)
1212
{
@@ -19,9 +19,9 @@ record = AgentRecord.FromAgent(agent);
1919
record.CreatedDateTime = DateTime.UtcNow;
2020
record.UpdatedDateTime = DateTime.UtcNow;
2121

22-
db.Transaction<IAgentTable>(delegate
22+
db.Transaction<IBotSharpTable>(delegate
2323
{
24-
db.Add<IAgentTable>(record);
24+
db.Add<IBotSharpTable>(record);
2525
});
2626

2727
return record.ToAgent();

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public partial class AgentService
77
{
88
public async Task<List<Agent>> GetAgents()
99
{
10-
var db = _services.GetRequiredService<AgentDbContext>();
10+
var db = _services.GetRequiredService<BotSharpDbContext>();
1111
var query = from agent in db.Agent
1212
where agent.OwnerId == _user.Id
1313
select agent.ToAgent();
@@ -16,7 +16,7 @@ public async Task<List<Agent>> GetAgents()
1616

1717
public async Task<Agent> GetAgent(string id)
1818
{
19-
var db = _services.GetRequiredService<AgentDbContext>();
19+
var db = _services.GetRequiredService<BotSharpDbContext>();
2020
var query = from agent in db.Agent
2121
where agent.Id == id
2222
select agent.ToAgent();

src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.UpdateAgent.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ public partial class AgentService
77
{
88
public async Task UpdateAgent(Agent agent)
99
{
10-
var db = _services.GetRequiredService<AgentDbContext>();
10+
var db = _services.GetRequiredService<BotSharpDbContext>();
1111

12-
db.Transaction<IAgentTable>(delegate
12+
db.Transaction<IBotSharpTable>(delegate
1313
{
1414
var record = db.Agent.FirstOrDefault(x => x.OwnerId == agent.OwerId && x.Id == agent.Id);
1515

src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using BotSharp.Abstraction.Conversations.Settings;
22
using Microsoft.AspNetCore.Builder;
33
using Microsoft.Extensions.Configuration;
4-
54
namespace BotSharp.Core;
65

76
public static class BotSharpServiceCollectionExtensions
@@ -20,15 +19,29 @@ public static IServiceCollection AddBotSharp(this IServiceCollection services, I
2019
services.AddScoped<IConversationStorage, ConversationStorage>();
2120
services.AddScoped<IConversationService, ConversationService>();
2221

23-
RegisterRepository(services, config);
24-
2522
RegisterPlugins(services, config);
2623

2724
return services;
2825
}
2926

30-
public static void ConfigureBotSharp(this IServiceCollection services)
27+
public static IServiceCollection ConfigureBotSharpRepository<Tdb>(this IServiceCollection services, IConfiguration config)
28+
where Tdb : DataContext
3129
{
30+
var databaseSettings = new DatabaseSettings();
31+
config.Bind("Database", databaseSettings);
32+
services.AddSingleton((IServiceProvider x) => databaseSettings);
33+
34+
var myDatabaseSettings = new MyDatabaseSettings();
35+
config.Bind("Database", myDatabaseSettings);
36+
services.AddSingleton((IServiceProvider x) => databaseSettings);
37+
38+
services.AddScoped((IServiceProvider x)
39+
=> DataContextHelper.GetDbContext<MongoDbContext, Tdb>(myDatabaseSettings, x));
40+
41+
services.AddScoped((IServiceProvider x)
42+
=> DataContextHelper.GetDbContext<BotSharpDbContext, Tdb>(myDatabaseSettings, x));
43+
44+
return services;
3245
}
3346

3447
public static IApplicationBuilder UseBotSharp(this IApplicationBuilder app)
@@ -43,27 +56,6 @@ public static IApplicationBuilder UseBotSharp(this IApplicationBuilder app)
4356
return app;
4457
}
4558

46-
public static void RegisterRepository(IServiceCollection services, IConfiguration config)
47-
{
48-
var databaseSettings = new DatabaseSettings();
49-
config.Bind("Database", databaseSettings);
50-
services.AddSingleton((IServiceProvider x) => databaseSettings);
51-
52-
var myDatabaseSettings = new MyDatabaseSettings();
53-
config.Bind("Database", myDatabaseSettings);
54-
services.AddSingleton((IServiceProvider x) => databaseSettings);
55-
56-
services.AddScoped((IServiceProvider x) =>
57-
{
58-
return DataContextHelper.GetDbContext<MongoDbContext>(myDatabaseSettings, x);
59-
});
60-
61-
services.AddScoped((IServiceProvider x) =>
62-
{
63-
return DataContextHelper.GetDbContext<AgentDbContext>(myDatabaseSettings, x);
64-
});
65-
}
66-
6759
public static void RegisterPlugins(IServiceCollection services, IConfiguration config)
6860
{
6961
var pluginSettings = new PluginLoaderSettings();

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public Task DeleteConversation(string id)
3030

3131
public async Task<Conversation> GetConversation(string id)
3232
{
33-
var db = _services.GetRequiredService<AgentDbContext>();
33+
var db = _services.GetRequiredService<BotSharpDbContext>();
3434
var query = from sess in db.Conversation
3535
where sess.Id == id
3636
orderby sess.CreatedTime descending
@@ -40,7 +40,7 @@ orderby sess.CreatedTime descending
4040

4141
public async Task<List<Conversation>> GetConversations()
4242
{
43-
var db = _services.GetRequiredService<AgentDbContext>();
43+
var db = _services.GetRequiredService<BotSharpDbContext>();
4444
var query = from sess in db.Conversation
4545
where sess.UserId == _user.Id
4646
orderby sess.CreatedTime descending
@@ -50,16 +50,16 @@ orderby sess.CreatedTime descending
5050

5151
public async Task<Conversation> NewConversation(Conversation sess)
5252
{
53-
var db = _services.GetRequiredService<AgentDbContext>();
53+
var db = _services.GetRequiredService<BotSharpDbContext>();
5454

5555
var record = ConversationRecord.FromConversation(sess);
5656
record.Id = sess.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString());
5757
record.UserId = sess.UserId.IfNullOrEmptyAs(_user.Id);
5858
record.Title = "New Conversation";
5959

60-
db.Transaction<IAgentTable>(delegate
60+
db.Transaction<IBotSharpTable>(delegate
6161
{
62-
db.Add<IAgentTable>(record);
62+
db.Add<IBotSharpTable>(record);
6363
});
6464

6565
_storage.InitStorage(sess.AgentId, record.Id);
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
namespace BotSharp.Core.Repository.Abstraction;
22

3-
public interface IAgentTable
3+
public interface IBotSharpTable
44
{
55
}

src/Infrastructure/BotSharp.Core/Repository/AgentDbContext.cs renamed to src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace BotSharp.Core.Repository;
22

3-
public class AgentDbContext : Database
3+
public class BotSharpDbContext : Database
44
{
55
public IQueryable<UserRecord> User => Table<UserRecord>();
66
public IQueryable<AgentRecord> Agent => Table<AgentRecord>();

src/Infrastructure/BotSharp.Core/Repository/DataContextHelper.cs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
using BotSharp.Core.Repository.Abstraction;
2-
using EntityFrameworkCore.BootKit;
31
using Microsoft.Data.SqlClient;
2+
using MySqlConnector;
43
using System.Data.Common;
54

65
namespace BotSharp.Core.Repository;
76

87
public static class DataContextHelper
98
{
10-
public static T GetDbContext<T>(MyDatabaseSettings settings, IServiceProvider serviceProvider)
9+
public static T GetDbContext<T, Tdb>(MyDatabaseSettings settings, IServiceProvider serviceProvider)
1110
where T : Database, new()
11+
where Tdb : DataContext
1212
{
1313
if (settings.Assemblies == null)
1414
throw new Exception("Please set assemblies.");
@@ -25,17 +25,33 @@ public static T GetDbContext<T>(MyDatabaseSettings settings, IServiceProvider se
2525
IsRelational = false
2626
});
2727
}
28-
else if (typeof(T) == typeof(AgentDbContext))
28+
else if (typeof(T) == typeof(BotSharpDbContext))
2929
{
30-
dc.BindDbContext<IAgentTable, DbContext4SqlServer2>(new DatabaseBind
30+
if (typeof(Tdb).Name.StartsWith("DbContext4SqlServer"))
3131
{
32-
ServiceProvider = serviceProvider,
33-
MasterConnection = new SqlConnection(settings.BotSharp.Master),
34-
SlaveConnections = settings.BotSharp.Slavers.Length == 0 ?
35-
new List<DbConnection> { new SqlConnection(settings.BotSharp.Master) } :
36-
settings.BotSharp.Slavers.Select(x => new SqlConnection(x) as DbConnection).ToList(),
37-
CreateDbIfNotExist = true
38-
});
32+
dc.BindDbContext<IBotSharpTable, Tdb>(new DatabaseBind
33+
{
34+
ServiceProvider = serviceProvider,
35+
MasterConnection = new SqlConnection(settings.BotSharp.Master),
36+
SlaveConnections = settings.BotSharp.Slavers.Length == 0 ?
37+
new List<DbConnection> { new SqlConnection(settings.BotSharp.Master) } :
38+
settings.BotSharp.Slavers.Select(x => new SqlConnection(x) as DbConnection)
39+
.ToList(),
40+
CreateDbIfNotExist = true
41+
});
42+
}
43+
else if (typeof(Tdb).Name.StartsWith("DbContext4Aurora") ||
44+
typeof(Tdb).Name.StartsWith("DbContext4MySql"))
45+
{
46+
dc.BindDbContext<IBotSharpTable, Tdb>(new DatabaseBind
47+
{
48+
ServiceProvider = serviceProvider,
49+
MasterConnection = new MySqlConnection(settings.BotSharp.Master),
50+
SlaveConnections = settings.BotSharp.Slavers
51+
.Select(x => new MySqlConnection(x) as DbConnection).ToList(),
52+
CreateDbIfNotExist = true
53+
});
54+
}
3955
}
4056
return dc;
4157
}

src/Infrastructure/BotSharp.Core/Repository/DbTables/AgentRecord.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace BotSharp.Core.Repository.DbTables;
66

77
[Table("Agent")]
8-
public class AgentRecord : DbRecord, IAgentTable
8+
public class AgentRecord : DbRecord, IBotSharpTable
99
{
1010
[Required]
1111
[MaxLength(64)]

src/Infrastructure/BotSharp.Core/Repository/DbTables/ConversationRecord.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace BotSharp.Core.Repository.DbTables;
66

77
[Table("Conversation")]
8-
public class ConversationRecord : DbRecord, IAgentTable
8+
public class ConversationRecord : DbRecord, IBotSharpTable
99
{
1010
[Required]
1111
[MaxLength(36)]

0 commit comments

Comments
 (0)