Support multiple database.

This commit is contained in:
hchen2020 2023-07-21 16:56:14 -05:00
parent d8ec3ee198
commit 52600e804c
12 changed files with 67 additions and 59 deletions

View file

@ -6,7 +6,7 @@ public partial class AgentService
{
public async Task<Agent> CreateAgent(Agent agent)
{
var db = _services.GetRequiredService<AgentDbContext>();
var db = _services.GetRequiredService<BotSharpDbContext>();
var record = db.Agent.FirstOrDefault(x => x.OwnerId == _user.Id && x.Name == agent.Name);
if (record != null)
{
@ -19,9 +19,9 @@ public partial class AgentService
record.CreatedDateTime = DateTime.UtcNow;
record.UpdatedDateTime = DateTime.UtcNow;
db.Transaction<IAgentTable>(delegate
db.Transaction<IBotSharpTable>(delegate
{
db.Add<IAgentTable>(record);
db.Add<IBotSharpTable>(record);
});
return record.ToAgent();

View file

@ -7,7 +7,7 @@ public partial class AgentService
{
public async Task<List<Agent>> GetAgents()
{
var db = _services.GetRequiredService<AgentDbContext>();
var db = _services.GetRequiredService<BotSharpDbContext>();
var query = from agent in db.Agent
where agent.OwnerId == _user.Id
select agent.ToAgent();
@ -16,7 +16,7 @@ public partial class AgentService
public async Task<Agent> GetAgent(string id)
{
var db = _services.GetRequiredService<AgentDbContext>();
var db = _services.GetRequiredService<BotSharpDbContext>();
var query = from agent in db.Agent
where agent.Id == id
select agent.ToAgent();

View file

@ -7,9 +7,9 @@ public partial class AgentService
{
public async Task UpdateAgent(Agent agent)
{
var db = _services.GetRequiredService<AgentDbContext>();
var db = _services.GetRequiredService<BotSharpDbContext>();
db.Transaction<IAgentTable>(delegate
db.Transaction<IBotSharpTable>(delegate
{
var record = db.Agent.FirstOrDefault(x => x.OwnerId == agent.OwerId && x.Id == agent.Id);

View file

@ -1,7 +1,6 @@
using BotSharp.Abstraction.Conversations.Settings;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
namespace BotSharp.Core;
public static class BotSharpServiceCollectionExtensions
@ -20,15 +19,29 @@ public static class BotSharpServiceCollectionExtensions
services.AddScoped<IConversationStorage, ConversationStorage>();
services.AddScoped<IConversationService, ConversationService>();
RegisterRepository(services, config);
RegisterPlugins(services, config);
return services;
}
public static void ConfigureBotSharp(this IServiceCollection services)
public static IServiceCollection ConfigureBotSharpRepository<Tdb>(this IServiceCollection services, IConfiguration config)
where Tdb : DataContext
{
var databaseSettings = new DatabaseSettings();
config.Bind("Database", databaseSettings);
services.AddSingleton((IServiceProvider x) => databaseSettings);
var myDatabaseSettings = new MyDatabaseSettings();
config.Bind("Database", myDatabaseSettings);
services.AddSingleton((IServiceProvider x) => databaseSettings);
services.AddScoped((IServiceProvider x)
=> DataContextHelper.GetDbContext<MongoDbContext, Tdb>(myDatabaseSettings, x));
services.AddScoped((IServiceProvider x)
=> DataContextHelper.GetDbContext<BotSharpDbContext, Tdb>(myDatabaseSettings, x));
return services;
}
public static IApplicationBuilder UseBotSharp(this IApplicationBuilder app)
@ -43,27 +56,6 @@ public static class BotSharpServiceCollectionExtensions
return app;
}
public static void RegisterRepository(IServiceCollection services, IConfiguration config)
{
var databaseSettings = new DatabaseSettings();
config.Bind("Database", databaseSettings);
services.AddSingleton((IServiceProvider x) => databaseSettings);
var myDatabaseSettings = new MyDatabaseSettings();
config.Bind("Database", myDatabaseSettings);
services.AddSingleton((IServiceProvider x) => databaseSettings);
services.AddScoped((IServiceProvider x) =>
{
return DataContextHelper.GetDbContext<MongoDbContext>(myDatabaseSettings, x);
});
services.AddScoped((IServiceProvider x) =>
{
return DataContextHelper.GetDbContext<AgentDbContext>(myDatabaseSettings, x);
});
}
public static void RegisterPlugins(IServiceCollection services, IConfiguration config)
{
var pluginSettings = new PluginLoaderSettings();

View file

@ -30,7 +30,7 @@ public class ConversationService : IConversationService
public async Task<Conversation> GetConversation(string id)
{
var db = _services.GetRequiredService<AgentDbContext>();
var db = _services.GetRequiredService<BotSharpDbContext>();
var query = from sess in db.Conversation
where sess.Id == id
orderby sess.CreatedTime descending
@ -40,7 +40,7 @@ public class ConversationService : IConversationService
public async Task<List<Conversation>> GetConversations()
{
var db = _services.GetRequiredService<AgentDbContext>();
var db = _services.GetRequiredService<BotSharpDbContext>();
var query = from sess in db.Conversation
where sess.UserId == _user.Id
orderby sess.CreatedTime descending
@ -50,16 +50,16 @@ public class ConversationService : IConversationService
public async Task<Conversation> NewConversation(Conversation sess)
{
var db = _services.GetRequiredService<AgentDbContext>();
var db = _services.GetRequiredService<BotSharpDbContext>();
var record = ConversationRecord.FromConversation(sess);
record.Id = sess.Id.IfNullOrEmptyAs(Guid.NewGuid().ToString());
record.UserId = sess.UserId.IfNullOrEmptyAs(_user.Id);
record.Title = "New Conversation";
db.Transaction<IAgentTable>(delegate
db.Transaction<IBotSharpTable>(delegate
{
db.Add<IAgentTable>(record);
db.Add<IBotSharpTable>(record);
});
_storage.InitStorage(sess.AgentId, record.Id);

View file

@ -1,5 +1,5 @@
namespace BotSharp.Core.Repository.Abstraction;
public interface IAgentTable
public interface IBotSharpTable
{
}

View file

@ -1,6 +1,6 @@
namespace BotSharp.Core.Repository;
public class AgentDbContext : Database
public class BotSharpDbContext : Database
{
public IQueryable<UserRecord> User => Table<UserRecord>();
public IQueryable<AgentRecord> Agent => Table<AgentRecord>();

View file

@ -1,14 +1,14 @@
using BotSharp.Core.Repository.Abstraction;
using EntityFrameworkCore.BootKit;
using Microsoft.Data.SqlClient;
using MySqlConnector;
using System.Data.Common;
namespace BotSharp.Core.Repository;
public static class DataContextHelper
{
public static T GetDbContext<T>(MyDatabaseSettings settings, IServiceProvider serviceProvider)
public static T GetDbContext<T, Tdb>(MyDatabaseSettings settings, IServiceProvider serviceProvider)
where T : Database, new()
where Tdb : DataContext
{
if (settings.Assemblies == null)
throw new Exception("Please set assemblies.");
@ -25,17 +25,33 @@ public static class DataContextHelper
IsRelational = false
});
}
else if (typeof(T) == typeof(AgentDbContext))
else if (typeof(T) == typeof(BotSharpDbContext))
{
dc.BindDbContext<IAgentTable, DbContext4SqlServer2>(new DatabaseBind
if (typeof(Tdb).Name.StartsWith("DbContext4SqlServer"))
{
ServiceProvider = serviceProvider,
MasterConnection = new SqlConnection(settings.BotSharp.Master),
SlaveConnections = settings.BotSharp.Slavers.Length == 0 ?
new List<DbConnection> { new SqlConnection(settings.BotSharp.Master) } :
settings.BotSharp.Slavers.Select(x => new SqlConnection(x) as DbConnection).ToList(),
CreateDbIfNotExist = true
});
dc.BindDbContext<IBotSharpTable, Tdb>(new DatabaseBind
{
ServiceProvider = serviceProvider,
MasterConnection = new SqlConnection(settings.BotSharp.Master),
SlaveConnections = settings.BotSharp.Slavers.Length == 0 ?
new List<DbConnection> { new SqlConnection(settings.BotSharp.Master) } :
settings.BotSharp.Slavers.Select(x => new SqlConnection(x) as DbConnection)
.ToList(),
CreateDbIfNotExist = true
});
}
else if (typeof(Tdb).Name.StartsWith("DbContext4Aurora") ||
typeof(Tdb).Name.StartsWith("DbContext4MySql"))
{
dc.BindDbContext<IBotSharpTable, Tdb>(new DatabaseBind
{
ServiceProvider = serviceProvider,
MasterConnection = new MySqlConnection(settings.BotSharp.Master),
SlaveConnections = settings.BotSharp.Slavers
.Select(x => new MySqlConnection(x) as DbConnection).ToList(),
CreateDbIfNotExist = true
});
}
}
return dc;
}

View file

@ -5,7 +5,7 @@ using System.ComponentModel.DataAnnotations.Schema;
namespace BotSharp.Core.Repository.DbTables;
[Table("Agent")]
public class AgentRecord : DbRecord, IAgentTable
public class AgentRecord : DbRecord, IBotSharpTable
{
[Required]
[MaxLength(64)]

View file

@ -5,7 +5,7 @@ using System.ComponentModel.DataAnnotations.Schema;
namespace BotSharp.Core.Repository.DbTables;
[Table("Conversation")]
public class ConversationRecord : DbRecord, IAgentTable
public class ConversationRecord : DbRecord, IBotSharpTable
{
[Required]
[MaxLength(36)]

View file

@ -5,7 +5,7 @@ using System.ComponentModel.DataAnnotations.Schema;
namespace BotSharp.Core.Repository.DbTables;
[Table("User")]
public class UserRecord : DbRecord, IAgentTable
public class UserRecord : DbRecord, IBotSharpTable
{
[Required]
[MaxLength(64)]

View file

@ -23,7 +23,7 @@ public class UserService : IUserService
public async Task<User> CreateUser(User user)
{
var db = _services.GetRequiredService<AgentDbContext>();
var db = _services.GetRequiredService<BotSharpDbContext>();
var record = db.User.FirstOrDefault(x => x.Email == user.Email.ToLower());
if (record != null)
{
@ -36,9 +36,9 @@ public class UserService : IUserService
record.Salt = Guid.NewGuid().ToString("N");
record.Password = Utilities.HashText(user.Password, record.Salt);
db.Transaction<IAgentTable>(delegate
db.Transaction<IBotSharpTable>(delegate
{
db.Add<IAgentTable>(record);
db.Add<IBotSharpTable>(record);
});
return record.ToUser();
@ -49,7 +49,7 @@ public class UserService : IUserService
var base64 = Encoding.UTF8.GetString(Convert.FromBase64String(authorization));
var (userEmail, password) = base64.SplitAsTuple(":");
var db = _services.GetRequiredService<AgentDbContext>();
var db = _services.GetRequiredService<BotSharpDbContext>();
var record = db.User.FirstOrDefault(x => x.Email == userEmail);
if (record == null)
{
@ -104,7 +104,7 @@ public class UserService : IUserService
{
var userId = _user.Id;
var db = _services.GetRequiredService<AgentDbContext>();
var db = _services.GetRequiredService<BotSharpDbContext>();
var user = (from u in db.User
where u.Id == userId
select new User