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"); log.Debug("Message received");
CommandString cmd = ctx.Command; CommandString cmd = ctx.Command;
Feature feature = new Feature() { Router = this }; Feature feature = new Feature() { Router = this };
ctx.Features.Add(feature); ctx.AddFeature(feature);
try try
{ {

View File

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

View File

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

View File

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