Fix DI-navigation support and FallbackStateForm handling for state machines#77
Merged
MajMcCloud merged 13 commits intoMajMcCloud:developmentfrom Nov 16, 2025
Merged
Conversation
Migrating latest developments to the master branch
Small updates and documentation improvements
Extended ISessionSerializationStage interface methods to accept an optional fallbackForm parameter for UseJSON, UseSimpleJSON, and UseXML overloads. This allows specifying a fallback form type for session serialization.
Extended UseJSON, UseSimpleJSON, and UseXML methods to accept an optional fallbackForm Type parameter, allowing specification of a fallback form for state machine initialization. This enhances flexibility in state management by enabling a default form to be set when loading or writing state files.
Renamed IStartFormFactory and related classes to IFormFactory for broader applicability beyond just start forms. Updated all references, method signatures, and class names to reflect this change, improving clarity and consistency in the codebase.
Refactored IFormFactory and its implementations to rename the CreateForm method to CreateStartForm for improved clarity. Updated SessionManager to use the new method name.
Added CreateForm(Type formType) and CreateForm<T>() methods to the IFormFactory interface to support more flexible form instantiation.
Introduced CreateForm(Type) and CreateForm<T>() methods to DefaultFormFactory, LambdaFormFactory, and ServiceProviderFormFactory. This enhances flexibility by allowing creation of forms by type or generic parameter, and unifies the form creation interface across different factory implementations.
Replaces direct constructor invocation with usage of BotBase.FormFactory.CreateForm for form instantiation. Adds additional null checks and ensures fallback form creation is attempted only if necessary. This improves flexibility and maintainability of form creation logic.
The new() constraint was removed from the CreateForm<T> method in IFormFactory and its implementations. This change allows for greater flexibility in form instantiation, enabling support for forms without parameterless constructors. DefaultFormFactory now throws an exception if a parameterless constructor is missing.
Assign the created fallback form to the 'form' variable to ensure it is used when the initial form is null.
Owner
|
Thanks for submitting the commits. I had a look and they all looks legit as expected. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Text by me
Preface
In the Telegram group discussion I raised a question regarding the lack of support for DI-based navigation when using a state machine. A community member replied that he had also encountered this issue long before I did.
The Problems
There are two separate issues:
FallbackStateForm) in standard state machines — theUseJSON(),UseXML(), and similar methods simply don’t have such a parameter and therefore cannot passFallbackStateFormto the state machine.My Solutions
Problem 1
I added an optional parameter
fallbackFormto all serialization setup methods.Problem 2
First, I modified the form factories. Previously, they could only create the start forms. Now, they can accept a desired form type and instantiate it directly.
Next, I changed the behavior of the
SessionManager.LoadSessionStatesmethod so that forms are now created via the factory.This resolves the DI navigation issue, since the factory that integrates with the
ServiceProvidernow assigns it to the form. It also removes the need for parameterless constructors in forms when using DI-based navigation.Text by Copilot
This pull request refactors the form creation infrastructure in the codebase, generalizing the "StartForm" concept into a more flexible "FormFactory" approach. It replaces the previous
IStartFormFactoryand related implementations with a newIFormFactoryinterface and updates all usages throughout the builder and factory classes. Additionally, it enhances session serialization methods to accept a fallback form type.Form Factory Refactoring:
IFormFactoryinterface with methods to create forms by type or generic, and replaced all usages ofIStartFormFactorywithIFormFactoryacross the codebase. (TelegramBotBase/Interfaces/IFormFactory.cs,TelegramBotBase/Builder/BotBaseBuilder.cs,TelegramBotBase/BotBase.cs,TelegramBotBase/Builder/Interfaces/IAPIKeySelectionStage.cs,TelegramBotBase/Builder/Interfaces/IStartFormSelectionStage.cs) [1] [2] [3] [4] [5] [6]DefaultStartFormFactoryandLambdaStartFormFactorywith new implementations:DefaultFormFactoryandLambdaFormFactory, providing more general form creation capabilities. (TelegramBotBase/Factories/DefaultFormFactory.cs,TelegramBotBase/Factories/LambdaFormFactory.cs, removedTelegramBotBase/Factories/DefaultStartFormFactory.cs, removedTelegramBotBase/Factories/LambdaStartFormFactory.cs) [1] [2] [3] [4]ServiceProviderStartFormFactorytoServiceProviderFormFactory, updating it to implementIFormFactoryand support creation of arbitrary form types, not just the start form. (TelegramBotBase/Factories/ServiceProviderFormFactory.cs) [1] [2] [3]Builder and API Changes:
IFormFactoryinstead ofIStartFormFactory, including method renames such asWithStartFormFactory→WithFormFactoryand parameter updates inQuickStartand service provider methods. (TelegramBotBase/Builder/BotBaseBuilder.cs,TelegramBotBase/Builder/Interfaces/IAPIKeySelectionStage.cs,TelegramBotBase/Builder/Interfaces/IStartFormSelectionStage.cs) [1] [2] [3] [4] [5] [6]Session Serialization Enhancements:
UseJSON,UseSimpleJSON,UseXML) to accept an optionalfallbackFormparameter, allowing for more robust deserialization and fallback behavior. Updated both implementation and interface documentation. (TelegramBotBase/Builder/BotBaseBuilder.cs,TelegramBotBase/Builder/Interfaces/ISessionSerializationStage.cs) [1] [2] [3] [4]These changes modernize and unify the form creation pattern, making it easier to extend and maintain, while also improving the flexibility of session state handling.