From 904939af719e61c0c08b9f0e1e130c68859b3e71 Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Mon, 14 Aug 2023 13:16:35 -0500 Subject: [PATCH] Remove database dependency. #100 --- .../Services/AgentService.CreateAgent.cs | 1 + .../BotSharpServiceCollectionExtensions.cs | 32 +++++++----- .../Repository/FileRepository.cs | 50 +++++++++++++++++-- src/WebStarter/Program.cs | 8 ++- src/WebStarter/appsettings.json | 1 + 5 files changed, 73 insertions(+), 19 deletions(-) diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs index 105470fba..cb2dd8382 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.CreateAgent.cs @@ -26,6 +26,7 @@ record = AgentRecord.FromAgent(agent); var userAgentRecord = new UserAgentRecord { + Id = Guid.NewGuid().ToString(), UserId = _user.Id, AgentId = record.Id, CreatedTime = DateTime.UtcNow, diff --git a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs index 37db512ea..66d8d4a3e 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpServiceCollectionExtensions.cs @@ -23,14 +23,6 @@ public static IServiceCollection AddBotSharp(this IServiceCollection services, I services.AddScoped(); services.AddScoped(); - RegisterPlugins(services, config); - - return services; - } - - public static IServiceCollection ConfigureBotSharpRepository(this IServiceCollection services, IConfiguration config) - where Tdb : DataContext - { var databaseSettings = new DatabaseSettings(); config.Bind("Database", databaseSettings); services.AddSingleton((IServiceProvider x) => databaseSettings); @@ -39,14 +31,28 @@ public static IServiceCollection ConfigureBotSharpRepository(this IServiceC config.Bind("Database", myDatabaseSettings); services.AddSingleton((IServiceProvider x) => myDatabaseSettings); - services.AddScoped((IServiceProvider x) - => DataContextHelper.GetDbContext(myDatabaseSettings, x)); + RegisterPlugins(services, config); + + return services; + } + public static IServiceCollection UsingSqlServer(this IServiceCollection services, IConfiguration config) + { + services.AddScoped(sp => + { + var myDatabaseSettings = sp.GetRequiredService(); + return DataContextHelper.GetDbContext(myDatabaseSettings, sp); + }); + + return services; + } + + public static IServiceCollection UsingFileRepository(this IServiceCollection services, IConfiguration config) + { services.AddScoped(sp => { - return myDatabaseSettings.Default == "FileRepository" ? - new FileRepository(myDatabaseSettings, sp) : - DataContextHelper.GetDbContext(myDatabaseSettings, sp); + var myDatabaseSettings = sp.GetRequiredService(); + return new FileRepository(myDatabaseSettings, sp); }); return services; diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index 3287343cd..c1688a4c2 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -70,7 +70,7 @@ public IQueryable UserAgent { get { - if (_userAgents != null) + if (_userAgents != null && _userAgents.Count > 0) { return _userAgents.AsQueryable(); } @@ -79,8 +79,12 @@ public IQueryable UserAgent _userAgents = new List(); foreach (var d in Directory.GetDirectories(dir)) { - var json = File.ReadAllText(Path.Combine(d, "agents.json")); - _userAgents.AddRange(JsonSerializer.Deserialize>(json, _options)); + var file = Path.Combine(d, "agents.json"); + if (Directory.Exists(d) && File.Exists(file)) + { + var json = File.ReadAllText(Path.Combine(d, "agents.json")); + _userAgents.AddRange(JsonSerializer.Deserialize>(json, _options)); + } } return _userAgents.AsQueryable(); } @@ -125,6 +129,16 @@ public void Add(object entity) _agents.Add(agent); _changedTableNames.Add(nameof(AgentRecord)); } + else if (entity is UserRecord user) + { + _users.Add(user); + _changedTableNames.Add(nameof(UserRecord)); + } + else if (entity is UserAgentRecord userAgent) + { + _userAgents.Add(userAgent); + _changedTableNames.Add(nameof(UserAgentRecord)); + } } List _changedTableNames = new List(); @@ -139,7 +153,7 @@ public int Transaction(Action action) if (table == nameof(ConversationRecord)) { var convSettings = _services.GetService(); - + foreach (var conversation in _conversations) { var dir = Path.Combine(_dbSettings.FileRepository, @@ -170,6 +184,34 @@ public int Transaction(Action action) File.WriteAllText(path, JsonSerializer.Serialize(agent, _options)); } } + else if (table == nameof(UserRecord)) + { + foreach (var user in _users) + { + var dir = Path.Combine(_dbSettings.FileRepository, + "users", + user.Id); + if (!Directory.Exists(dir)) + { + Directory.CreateDirectory(dir); + } + var path = Path.Combine(dir, "user.json"); + File.WriteAllText(path, JsonSerializer.Serialize(user, _options)); + } + } + else if (table == nameof(UserAgentRecord)) + { + _userAgents.GroupBy(x => x.UserId) + .Select(x => x.Key).ToList() + .ForEach(uid => + { + var dir = Path.Combine(_dbSettings.FileRepository, + "users", + uid); + var path = Path.Combine(dir, "agents.json"); + File.WriteAllText(path, JsonSerializer.Serialize(_userAgents.Where(x => x.UserId == uid), _options)); + }); + } } return _changedTableNames.Count; diff --git a/src/WebStarter/Program.cs b/src/WebStarter/Program.cs index 5d9db0032..2dc9675e3 100644 --- a/src/WebStarter/Program.cs +++ b/src/WebStarter/Program.cs @@ -40,8 +40,12 @@ builder.Services.AddScoped(); // Add BotSharp -builder.Services.AddBotSharp(builder.Configuration) - .ConfigureBotSharpRepository(builder.Configuration); +builder.Services.AddBotSharp(builder.Configuration); + +// Change below if you want to use other data storage. +// builder.Services.UsingSqlServer(builder.Configuration); +// Default is using File Storage +builder.Services.UsingFileRepository(builder.Configuration); builder.Services.AddCors(options => { diff --git a/src/WebStarter/appsettings.json b/src/WebStarter/appsettings.json index dc4880f51..61b34f0b5 100644 --- a/src/WebStarter/appsettings.json +++ b/src/WebStarter/appsettings.json @@ -61,6 +61,7 @@ "Master": "Data Source=(localdb)\\ProjectModels;Initial Catalog=BotSharp;Integrated Security=True;Connect Timeout=30;Encrypt=False;Trust Server Certificate=False;Application Intent=ReadWrite;Multi Subnet Failover=False", "Slavers": [] }, + "FileRepository": "data", "UseCamelCase": true, "Assemblies": [ "BotSharp.Core" ] },