Fix DI-navigation support and FallbackStateForm handling for state machines #77
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.