Skip to content

Commit

Permalink
Remove database dependency. #100
Browse files Browse the repository at this point in the history
  • Loading branch information
Oceania2018 committed Aug 14, 2023
1 parent 62f8800 commit 904939a
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,6 @@ public static IServiceCollection AddBotSharp(this IServiceCollection services, I
services.AddScoped<IConversationService, ConversationService>();
services.AddScoped<IConversationStateService, ConversationStateService>();

RegisterPlugins(services, config);

return 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);
Expand All @@ -39,14 +31,28 @@ public static IServiceCollection ConfigureBotSharpRepository<Tdb>(this IServiceC
config.Bind("Database", myDatabaseSettings);
services.AddSingleton((IServiceProvider x) => myDatabaseSettings);

services.AddScoped((IServiceProvider x)
=> DataContextHelper.GetDbContext<MongoDbContext, Tdb>(myDatabaseSettings, x));
RegisterPlugins(services, config);

return services;
}

public static IServiceCollection UsingSqlServer(this IServiceCollection services, IConfiguration config)
{
services.AddScoped<IBotSharpRepository>(sp =>
{
var myDatabaseSettings = sp.GetRequiredService<MyDatabaseSettings>();
return DataContextHelper.GetDbContext<BotSharpDbContext, DbContext4SqlServer>(myDatabaseSettings, sp);
});

return services;
}

public static IServiceCollection UsingFileRepository(this IServiceCollection services, IConfiguration config)
{
services.AddScoped<IBotSharpRepository>(sp =>
{
return myDatabaseSettings.Default == "FileRepository" ?
new FileRepository(myDatabaseSettings, sp) :
DataContextHelper.GetDbContext<BotSharpDbContext, Tdb>(myDatabaseSettings, sp);
var myDatabaseSettings = sp.GetRequiredService<MyDatabaseSettings>();
return new FileRepository(myDatabaseSettings, sp);
});

return services;
Expand Down
50 changes: 46 additions & 4 deletions src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public IQueryable<UserAgentRecord> UserAgent
{
get
{
if (_userAgents != null)
if (_userAgents != null && _userAgents.Count > 0)
{
return _userAgents.AsQueryable();
}
Expand All @@ -79,8 +79,12 @@ public IQueryable<UserAgentRecord> UserAgent
_userAgents = new List<UserAgentRecord>();
foreach (var d in Directory.GetDirectories(dir))
{
var json = File.ReadAllText(Path.Combine(d, "agents.json"));
_userAgents.AddRange(JsonSerializer.Deserialize<List<UserAgentRecord>>(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<List<UserAgentRecord>>(json, _options));
}
}
return _userAgents.AsQueryable();
}
Expand Down Expand Up @@ -125,6 +129,16 @@ public void Add<TTableInterface>(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<string> _changedTableNames = new List<string>();
Expand All @@ -139,7 +153,7 @@ public int Transaction<TTableInterface>(Action action)
if (table == nameof(ConversationRecord))
{
var convSettings = _services.GetService<ConversationSetting>();

foreach (var conversation in _conversations)
{
var dir = Path.Combine(_dbSettings.FileRepository,
Expand Down Expand Up @@ -170,6 +184,34 @@ public int Transaction<TTableInterface>(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;
Expand Down
8 changes: 6 additions & 2 deletions src/WebStarter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@
builder.Services.AddScoped<IUserIdentity, UserIdentity>();

// Add BotSharp
builder.Services.AddBotSharp(builder.Configuration)
.ConfigureBotSharpRepository<DbContext4SqlServer>(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 =>
{
Expand Down
1 change: 1 addition & 0 deletions src/WebStarter/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]
},
Expand Down

0 comments on commit 904939a

Please sign in to comment.