BotSharp/BotSharp.Core/PlatformBuilderBase.cs

71 lines
1.7 KiB
C#
Raw Normal View History

using BotSharp.Platform.Abstraction;
using BotSharp.Platform.Models;
2018-10-01 17:15:17 +00:00
using DotNetToolkit;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
2018-10-01 17:15:17 +00:00
using System.Linq;
using System.Text;
namespace BotSharp.Core
{
2018-10-01 17:15:17 +00:00
public abstract class PlatformBuilderBase<TAgent> where TAgent : AgentBase
{
2018-10-01 17:15:17 +00:00
public IAgentStorage<TAgent> Storage { get; set; }
public IConfiguration PlatformConfig { get; set; }
2018-09-29 18:18:34 +00:00
public List<TAgent> GetAllAgents()
{
2018-10-01 17:15:17 +00:00
GetStorage();
return Storage.Query();
}
2018-09-29 18:18:34 +00:00
public TAgent GetAgentById(string agentId)
{
2018-10-01 17:15:17 +00:00
GetStorage();
return Storage.FetchById(agentId);
}
2018-09-29 18:18:34 +00:00
public TAgent GetAgentByName(string agentName)
{
2018-10-01 17:15:17 +00:00
GetStorage();
return Storage.FetchByName(agentName);
}
2018-09-29 18:18:34 +00:00
public virtual bool SaveAgent(TAgent agent)
{
2018-10-01 17:15:17 +00:00
GetStorage();
// default save agent in FileStorage
2018-10-01 17:15:17 +00:00
Storage.Persist(agent);
return true;
}
2018-10-01 17:15:17 +00:00
private IAgentStorage<TAgent> GetStorage()
{
if (Storage == null)
{
string storageName = PlatformConfig.GetValue<String>("AgentStorage");
switch (storageName)
{
case "AgentStorageInRedis":
Storage = Activator.CreateInstance<AgentStorageInRedis<TAgent>>();
break;
case "AgentStorageInMemory":
Storage = Activator.CreateInstance<AgentStorageInMemory<TAgent>>();
break;
}
}
return Storage;
}
}
}