diff --git a/JetKarmaBot/Services/Handling/CommandRouter.cs b/JetKarmaBot/Services/Handling/CommandRouter.cs index b2354c5..c5ef18d 100644 --- a/JetKarmaBot/Services/Handling/CommandRouter.cs +++ b/JetKarmaBot/Services/Handling/CommandRouter.cs @@ -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 { diff --git a/JetKarmaBot/Services/Handling/DatabaseHandler.cs b/JetKarmaBot/Services/Handling/DatabaseHandler.cs index 33e6f5b..73d848d 100644 --- a/JetKarmaBot/Services/Handling/DatabaseHandler.cs +++ b/JetKarmaBot/Services/Handling/DatabaseHandler.cs @@ -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(); } diff --git a/JetKarmaBot/Services/Handling/RequestContext.cs b/JetKarmaBot/Services/Handling/RequestContext.cs index 5481f0c..b59ce84 100644 --- a/JetKarmaBot/Services/Handling/RequestContext.cs +++ b/JetKarmaBot/Services/Handling/RequestContext.cs @@ -14,15 +14,16 @@ namespace JetKarmaBot.Services.Handling public ITelegramBotClient Client { get; } public MessageEventArgs EventArgs { get; } public CommandString Command { get; } - public ICollection Features { get; } = new List(); + public Dictionary Features { get; } = new Dictionary(); 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)Features.First(x => x is T); + public object GetService(Type serviceType) => Features[serviceType]; + public T GetFeature() => (T)Features[typeof(T)]; + public void AddFeature(T feat) => Features[typeof(T)] = feat; //Method to reduce WET in commands public Task SendMessage(string text) => Client.SendTextMessageAsync( diff --git a/JetKarmaBot/Services/Handling/TimeoutManager.cs b/JetKarmaBot/Services/Handling/TimeoutManager.cs index 638168c..7d75c05 100644 --- a/JetKarmaBot/Services/Handling/TimeoutManager.cs +++ b/JetKarmaBot/Services/Handling/TimeoutManager.cs @@ -117,7 +117,7 @@ namespace JetKarmaBot.Services.Handling return; } Feature feature = new Feature(); - ctx.Features.Add(feature); + ctx.AddFeature(feature); await next(ctx);