diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..9b1dc1a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "perfusion"] + path = perfusion + url = http://lserver1/git/perfusion.git diff --git a/JetKarmaBot/Db.cs b/JetKarmaBot/Db.cs index a32d44a..fc0b1c6 100644 --- a/JetKarmaBot/Db.cs +++ b/JetKarmaBot/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/JetKarmaBot/JetKarmaBot.cs b/JetKarmaBot/JetKarmaBot.cs index 9f72e27..815c6ca 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,10 +22,9 @@ 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) { - this.db = db; var httpProxy = new WebProxy($"{cfg.ProxyUrl}:{cfg.ProxyPort}") { Credentials = new NetworkCredential(cfg.ProxyLogin, cfg.ProxyPassword) @@ -46,8 +46,9 @@ namespace JetKarmaBot #endregion #region service - Db db { get; } + [Inject(true)] Db db { get; set; } TelegramBotClient client { get; } + [Inject(true)] Container container { get; set; } User me { get; } ChatCommandRouter commands; @@ -65,10 +66,10 @@ namespace JetKarmaBot void InitCommands() { commands = new ChatCommandRouter(); - commands.Add(new StartCommand(db)); - commands.Add(new EchoCommand(client)); - commands.Add(new DefineCommand(client)); - commands.Add(new AwardCommand(db, client, me)); + commands.Add(container.ResolveObject(new StartCommand(db))); + commands.Add(container.ResolveObject(new EchoCommand(client))); + commands.Add(container.ResolveObject(new DefineCommand(client))); + commands.Add(container.ResolveObject(new AwardCommand(db, client, 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 ba7aac3..fb3ebd3 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; + Current.Run(); Console.ReadKey(); } public static App Current { get; private set; } - public App(Config cfg) + public void Run() { - Config = cfg; - Db = new Db(Config); - Watcher = new JetKarmaBot(Config, Db); Console.WriteLine("JatKarmaBot started!"); } - Config Config { get; } - Db Db { get; } - JetKarmaBot Watcher { get; } + [Inject(true)] JetKarmaBot Watcher { get; } } } diff --git a/perfusion b/perfusion new file mode 160000 index 0000000..a9bd490 --- /dev/null +++ b/perfusion @@ -0,0 +1 @@ +Subproject commit a9bd490c796b243245573ca4c9739a29364d2dde