BotSharp/BotSharp.Core/AgentStorage/AgentStorageServiceRegister.cs
2019-03-16 19:55:55 -05:00

49 lines
1.7 KiB
C#

using BotSharp.Platform.Abstractions;
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<AgentStorageInFile<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 if (key.Equals("AgentStorageInFile"))
{
return factory.GetService<AgentStorageInFile<TAgent>>();
}
else
{
throw new ArgumentException($"Not Support key : {key}");
}
};
return accesor;
});
}
}
}