optimize agent storage DI.

This commit is contained in:
botsharp2018 2018-10-11 21:42:56 -05:00
parent 3bfe8d26e1
commit bc6d087914
12 changed files with 94 additions and 38 deletions

View file

@ -6,7 +6,7 @@ using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Core
namespace BotSharp.Core.AgentStorage
{
public class AgentStorageFactory<TAgent> : IAgentStorageFactory<TAgent> where TAgent : AgentBase
{

View file

@ -6,7 +6,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Core
namespace BotSharp.Core.AgentStorage
{
/// <summary>
/// Save agent instance into memory.

View file

@ -9,7 +9,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BotSharp.Core
namespace BotSharp.Core.AgentStorage
{
public class AgentStorageInRedis<TAgent> : IAgentStorage<TAgent>
where TAgent : AgentBase

View file

@ -0,0 +1,43 @@
using BotSharp.Platform.Abstraction;
using BotSharp.Platform.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Core.AgentStorage
{
public class AgentStorageServiceRegister
{
public static void Register<TAgent>(IServiceCollection services)
where TAgent : AgentBase
{
services.AddSingleton<IAgentStorageFactory<TAgent>, AgentStorageFactory<TAgent>>();
services.AddSingleton<AgentStorageInMemory<TAgent>>();
services.AddSingleton<AgentStorageInRedis<TAgent>>();
services.AddSingleton(factory =>
{
Func<string, IAgentStorage<TAgent>> accesor = key =>
{
if (key.Equals("AgentStorageInRedis"))
{
return factory.GetService<AgentStorageInRedis<TAgent>>();
}
else if (key.Equals("AgentStorageInMemory"))
{
return factory.GetService<AgentStorageInMemory<TAgent>>();
}
else
{
throw new ArgumentException($"Not Support key : {key}");
}
};
return accesor;
});
}
}
}

View file

@ -20,14 +20,6 @@ namespace Microsoft.Extensions.DependencyInjection
var platform = configuration.GetValue<string>("platformModuleName");
var module = options.Modules.Find(x => x.Name == platform);
var engine = configuration.GetValue<string>($"{platform}:BotEngine");
Formatter[] settings = new Formatter[]
{
new Formatter(platform, Color.Yellow),
new Formatter(module.Name, Color.Yellow),
new Formatter(engine, Color.Yellow),
};
// load platform emulator dynamically
Console.WriteLine();
@ -37,12 +29,19 @@ namespace Microsoft.Extensions.DependencyInjection
{
Assembly library = AssemblyLoadContext.Default.LoadFromAssemblyPath(platformDllPath);
action(library);
Console.WriteLineFormatted("Loaded {0} platform emulator from {1} assembly which is using {2} engine.", Color.White, settings);
Formatter[] settings = new Formatter[]
{
new Formatter(platform, Color.Yellow),
new Formatter(platformDllPath, Color.Yellow)
};
Console.WriteLineFormatted("Loaded {0} platform emulator from {1} assembly.", Color.White, settings);
}
else
{
Console.WriteLine($"Can't load {module.Type} assembly.");
Console.WriteLine($"Can't load {module.Type} assembly from {platformDllPath}.");
}
Console.WriteLine();
}
}

View file

@ -0,0 +1,32 @@
using BotSharp.Platform.Abstraction;
using Colorful;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using Console = Colorful.Console;
namespace BotSharp.Core
{
public class PlatformConfigServiceRegister
{
public static void Register<ISettings>(string section, IServiceCollection services, IConfiguration config)
where ISettings : IPlatformSettings, new()
{
var setting = new ISettings();
config.GetSection(section).Bind(setting);
services.AddSingleton<IPlatformSettings>(setting);
Formatter[] settings = new Formatter[]
{
new Formatter(setting.BotEngine, Color.Yellow),
new Formatter(setting.AgentStorage, Color.Yellow)
};
Console.WriteLineFormatted("NLU engine: {0}, Agent Storage: {1}.", Color.White, settings);
Console.WriteLine();
}
}
}

View file

@ -79,6 +79,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\botsharp-dialogflow\BotSharp.Platform.Dialogflow\BotSharp.Platform.Dialogflow.csproj" />
<ProjectReference Include="..\BotSharp.Core\BotSharp.Core.csproj" />
</ItemGroup>
@ -86,15 +87,9 @@
<Content Update="Settings\app.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Update="Settings\RasaAi.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Update="Settings\DialogflowAi.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Update="Settings\ArticulateAi.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Update="Settings\auth.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

View file

@ -1,7 +0,0 @@
{
"articulateAi": {
"botEngine": "BotSharpNLU",
"agentStorage": "AgentStorageInRedis"
}
}

View file

@ -1,7 +1,6 @@
{
// if you want to override platform setting, please set corresponding value, otherwise you don't need this section.
"dialogflowAi": {
"botEngine": "BotSharpNLU",
"agentStorage": "AgentStorageInMemory"
"botEngine": "BotSharpNLU"
}
}

View file

@ -1,7 +0,0 @@
{
"rasaAi": {
"botEngine": "BotSharpNLU",
"agentStorage": "AgentStorageInMemory"
}
}

View file

@ -8,7 +8,7 @@
"dataDir": "D:\\Projects\\BotSharp\\Data"
},
"moduleBasePath": "D:\\Projects",
"moduleBasePath": "C:\\Users\\haipi\\Documents\\Projects",
"modules": [
{
"Name": "DialogflowAi",

View file

@ -98,9 +98,11 @@ namespace BotSharp.WebHost
c.DocumentTitle = info.Title;
c.InjectStylesheet(Configuration.GetValue<String>("Swagger:Stylesheet"));
Console.WriteLine();
Console.WriteLine($"{info.Title} [{info.Version}] {info.License.Name}");
Console.WriteLine($"{info.Description}");
Console.WriteLine($"{info.Contact.Name}");
Console.WriteLine($"{info.Contact.Name}, {DateTime.UtcNow.ToString()}");
Console.WriteLine();
});
app.Use(async (context, next) =>