From 6a80ec66ad3302b3cf1946b570a7c3dd814ec197 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=A7=D0=BE=D0=B2?= =?UTF-8?q?=D0=BD=D1=8E=D0=BA?= Date: Fri, 6 Dec 2024 16:13:46 +0500 Subject: [PATCH] di and serialization support --- .gitea/workflows/TelegramBotFramework.nuget.yaml | 2 +- .../Factories/ServiceProviderStartFormFactory.cs | 6 ++++-- TelegramBotBase/SessionManager.cs | 13 +++++++++++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/.gitea/workflows/TelegramBotFramework.nuget.yaml b/.gitea/workflows/TelegramBotFramework.nuget.yaml index 1c0f420..1a3c622 100644 --- a/.gitea/workflows/TelegramBotFramework.nuget.yaml +++ b/.gitea/workflows/TelegramBotFramework.nuget.yaml @@ -8,7 +8,7 @@ jobs: Build-TelegramBotBase: env: APP_PROJECT_NAME: TelegramBotBase - PACKAGE_VERSION: "123.0.0" + PACKAGE_VERSION: "123.0.1" strategy: matrix: os: diff --git a/TelegramBotBase/Factories/ServiceProviderStartFormFactory.cs b/TelegramBotBase/Factories/ServiceProviderStartFormFactory.cs index f143604..8d825e2 100644 --- a/TelegramBotBase/Factories/ServiceProviderStartFormFactory.cs +++ b/TelegramBotBase/Factories/ServiceProviderStartFormFactory.cs @@ -23,13 +23,15 @@ public class ServiceProviderStartFormFactory : IStartFormFactory _serviceProvider = serviceProvider; } - public FormBase CreateForm() + public FormBase CreateForm() => CreateForm(null); + + public FormBase CreateForm(Type? specifiedStartFrom = null) { FormBase fb = null; try { - fb = (FormBase)ActivatorUtilities.CreateInstance(_serviceProvider, _startFormClass); + fb = (FormBase)ActivatorUtilities.CreateInstance(_serviceProvider, specifiedStartFrom ?? _startFormClass); } catch(InvalidOperationException ex) { diff --git a/TelegramBotBase/SessionManager.cs b/TelegramBotBase/SessionManager.cs index 7e9d257..f5e04d1 100644 --- a/TelegramBotBase/SessionManager.cs +++ b/TelegramBotBase/SessionManager.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using TelegramBotBase.Args; using TelegramBotBase.Attributes; using TelegramBotBase.Base; +using TelegramBotBase.Factories; using TelegramBotBase.Form; using TelegramBotBase.Interfaces; using TelegramBotBase.Sessions; @@ -145,9 +146,17 @@ public class SessionManager { continue; } - + FormBase form; + if (BotBase.StartFormFactory is ServiceProviderStartFormFactory diFactory) + { + form = diFactory.CreateForm(t); + } //No default constructor, fallback - if (!(t.GetConstructor(new Type[] { })?.Invoke(new object[] { }) is FormBase form)) + else if (t.GetConstructor(new Type[] { })?.Invoke(new object[] { }) is FormBase f) + { + form = f; + } + else { if (!statemachine.FallbackStateForm.IsSubclassOf(typeof(FormBase))) {