diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..9d5e6ee --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "perfusion"] + path = perfusion + url = http://192.168.1.65/git/perfusion.git diff --git a/JetKarmaBot.sln b/JetKarmaBot.sln index 9a514a0..9dd60ad 100644 --- a/JetKarmaBot.sln +++ b/JetKarmaBot.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 VisualStudioVersion = 15.0.28010.2036 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JetKarmaBot", "JetKarmaBot\JetKarmaBot.csproj", "{729E88EE-BE5E-4D12-B83F-EDC5FC5E2D07}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JetKarmaBot", "JetKarmaBot\JetKarmaBot.csproj", "{729E88EE-BE5E-4D12-B83F-EDC5FC5E2D07}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/JetKarmaBot/Commands/AwardCommand.cs b/JetKarmaBot/Commands/AwardCommand.cs index 51a406e..4b3d507 100644 --- a/JetKarmaBot/Commands/AwardCommand.cs +++ b/JetKarmaBot/Commands/AwardCommand.cs @@ -6,6 +6,7 @@ using Telegram.Bot; using Telegram.Bot.Args; using Telegram.Bot.Types; using Telegram.Bot.Types.Enums; +using Perfusion; namespace JetKarmaBot.Commands { @@ -59,14 +60,12 @@ namespace JetKarmaBot.Commands } - Db Db { get; } - TelegramBotClient Client { get; } + [Inject(true)]Db Db { get; set; } + [Inject(true)] TelegramBotClient Client { get; set; } User Me { get; } - public AwardCommand(Db db, TelegramBotClient client, User me) + public AwardCommand(User me) { - Db = db; - Client = client; Me = me; } } diff --git a/JetKarmaBot/Commands/StartCommand.cs b/JetKarmaBot/Commands/StartCommand.cs index 81de7ed..0aa3516 100644 --- a/JetKarmaBot/Commands/StartCommand.cs +++ b/JetKarmaBot/Commands/StartCommand.cs @@ -1,16 +1,12 @@ using System.Collections.Generic; using Telegram.Bot.Args; +using Perfusion; namespace JetKarmaBot.Commands { public class StartCommand : IChatCommand { - Db Db; - - public StartCommand(Db db) - { - Db = db; - } + [Inject(true)]Db Db; public IReadOnlyCollection Names => new[] { "start" }; diff --git a/JetKarmaBot/JetKarmaBot.cs b/JetKarmaBot/JetKarmaBot.cs index ee41dde..d9c8c05 100644 --- a/JetKarmaBot/JetKarmaBot.cs +++ b/JetKarmaBot/JetKarmaBot.cs @@ -1,4 +1,5 @@ using JetKarmaBot.Commands; +using Perfusion; using System; using System.Collections.Generic; using System.Linq; @@ -21,19 +22,19 @@ namespace JetKarmaBot foreach (var u in db.Chats) client.SendTextMessageAsync(u.Value.ChatId, message); } - - public JetKarmaBot(Config cfg, Db db) + + public JetKarmaBot([Inject(true)]Config cfg, [Inject(true)] Container container) { - this.db = db; var httpProxy = new WebProxy($"{cfg.ProxyUrl}:{cfg.ProxyPort}") { Credentials = new NetworkCredential(cfg.ProxyLogin, cfg.ProxyPassword) }; var botClient = new TelegramBotClient(cfg.ApiKey, httpProxy); + container.AddInstance(botClient); var cred = new NetworkCredential(cfg.ProxyLogin, cfg.ProxyPassword); client = new TelegramBotClient(cfg.ApiKey, httpProxy); me = client.GetMeAsync().Result; - InitCommands(); + InitCommands(container); client.OnMessage += BotOnMessageReceived; client.StartReceiving(); } @@ -46,7 +47,7 @@ namespace JetKarmaBot #endregion #region service - Db db { get; } + [Inject(true)] Db db { get; set; } TelegramBotClient client { get; } User me { get; } @@ -62,11 +63,11 @@ namespace JetKarmaBot long from = message.From.Id; Task.Run(() => commands.Execute(sender, messageEventArgs)); } - void InitCommands() + void InitCommands(Container c) { commands = new ChatCommandRouter(); - commands.Add(new StartCommand(db)); - commands.Add(new AwardCommand(db, client, me)); + commands.Add(c.ResolveObject(new StartCommand())); + commands.Add(c.ResolveObject(new AwardCommand(me))); } #endregion diff --git a/JetKarmaBot/JetKarmaBot.csproj b/JetKarmaBot/JetKarmaBot.csproj index 067512e..eeaee13 100644 --- a/JetKarmaBot/JetKarmaBot.csproj +++ b/JetKarmaBot/JetKarmaBot.csproj @@ -10,6 +10,7 @@ + diff --git a/JetKarmaBot/Program.cs b/JetKarmaBot/Program.cs index 4e4e88a..6fe46bc 100644 --- a/JetKarmaBot/Program.cs +++ b/JetKarmaBot/Program.cs @@ -1,4 +1,5 @@ using System; +using Perfusion; namespace JetKarmaBot { @@ -6,23 +7,20 @@ namespace JetKarmaBot { public static void Main(string[] args) { - Current = new App(new Config("karma.cfg.json")); - + Container c = new Container(); + c.AddInstance(new Config("karma.cfg.json")); + Current = c.GetInstance(typeof(App)) as App; Console.ReadKey(); } public static App Current { get; private set; } - - public App(Config cfg) + [Inject(true)] + public void Run(Container c) { - Config = cfg; - Db = new Db(Config); - Watcher = new JetKarmaBot(Config, Db); + Watcher = c.GetInstance(); Console.WriteLine("JetKarmaBot started!"); } - Config Config { get; } - Db Db { get; } - JetKarmaBot Watcher { get; } + JetKarmaBot Watcher { get; set; } } } diff --git a/JetKarmaBot/Services/Db.cs b/JetKarmaBot/Services/Db.cs index 0868bd3..41d16b5 100644 --- a/JetKarmaBot/Services/Db.cs +++ b/JetKarmaBot/Services/Db.cs @@ -1,9 +1,10 @@ -using System; +using System; using System.Collections.Generic; using System.Data; using System.Linq; using Dapper; using MySql.Data.MySqlClient; +using Perfusion; namespace JetKarmaBot { @@ -105,7 +106,7 @@ namespace JetKarmaBot #endregion #region service - public Db(Config cfg) + public Db([Inject(true)]Config cfg) { Log("Initializing..."); Conn = new MySqlConnection(cfg.ConnectionString); diff --git a/perfusion b/perfusion new file mode 160000 index 0000000..470af23 --- /dev/null +++ b/perfusion @@ -0,0 +1 @@ +Subproject commit 470af231e3f7cf6942907d3b50473fa04dc04caf