Make RequestContext.Features a dictionary

This commit is contained in:
Basique Evangelist 2020-05-16 23:31:49 +03:00
parent e106681c11
commit 09c07e6296
Signed by untrusted user: BasiqueEvangelist
GPG Key ID: B370219149301706
4 changed files with 8 additions and 7 deletions

View File

@ -31,7 +31,7 @@ namespace JetKarmaBot.Services.Handling
log.Debug("Message received");
CommandString cmd = ctx.Command;
Feature feature = new Feature() { Router = this };
ctx.Features.Add(feature);
ctx.AddFeature(feature);
try
{

View File

@ -13,8 +13,8 @@ namespace JetKarmaBot.Services.Handling
{
using (var db = Db.GetContext())
{
ctx.Features.Add(db); // KarmaContext
ctx.Features.Add(Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id))?.Locale ?? "ru-ru"]); // Locale
ctx.AddFeature(db); // KarmaContext
ctx.AddFeature(Locale[(await db.Chats.FindAsync(ctx.EventArgs.Message.Chat.Id))?.Locale ?? "ru-ru"]); // Locale
await next(ctx);
await db.SaveChangesAsync();
}

View File

@ -14,15 +14,16 @@ namespace JetKarmaBot.Services.Handling
public ITelegramBotClient Client { get; }
public MessageEventArgs EventArgs { get; }
public CommandString Command { get; }
public ICollection<object> Features { get; } = new List<object>();
public Dictionary<Type, object> Features { get; } = new Dictionary<Type, object>();
public RequestContext(ITelegramBotClient client, MessageEventArgs args, CommandString cmd)
{
Client = client;
EventArgs = args;
Command = cmd;
}
public object GetService(Type serviceType) => Features.First(x => x.GetType() == serviceType);
public T GetFeature<T>() => (T)Features.First(x => x is T);
public object GetService(Type serviceType) => Features[serviceType];
public T GetFeature<T>() => (T)Features[typeof(T)];
public void AddFeature<T>(T feat) => Features[typeof(T)] = feat;
//Method to reduce WET in commands
public Task SendMessage(string text) => Client.SendTextMessageAsync(

View File

@ -117,7 +117,7 @@ namespace JetKarmaBot.Services.Handling
return;
}
Feature feature = new Feature();
ctx.Features.Add(feature);
ctx.AddFeature(feature);
await next(ctx);