Skip to content

Conversation

@Severnbli
Copy link

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:

  1. DI navigation when loading state
  2. No way to specify a fallback form type for the IgnoreState case (FallbackStateForm) in standard state machines — the UseJSON(), UseXML(), and similar methods simply don’t have such a parameter and therefore cannot pass FallbackStateForm to the state machine.

My Solutions

Problem 1

I added an optional parameter fallbackForm to 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.LoadSessionStates method so that forms are now created via the factory.

This resolves the DI navigation issue, since the factory that integrates with the ServiceProvider now 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 IStartFormFactory and related implementations with a new IFormFactory interface 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:

  • Introduced a new IFormFactory interface with methods to create forms by type or generic, and replaced all usages of IStartFormFactory with IFormFactory across 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]
  • Replaced DefaultStartFormFactory and LambdaStartFormFactory with new implementations: DefaultFormFactory and LambdaFormFactory, providing more general form creation capabilities. (TelegramBotBase/Factories/DefaultFormFactory.cs, TelegramBotBase/Factories/LambdaFormFactory.cs, removed TelegramBotBase/Factories/DefaultStartFormFactory.cs, removed TelegramBotBase/Factories/LambdaStartFormFactory.cs) [1] [2] [3] [4]
  • Refactored and renamed ServiceProviderStartFormFactory to ServiceProviderFormFactory, updating it to implement IFormFactory and support creation of arbitrary form types, not just the start form. (TelegramBotBase/Factories/ServiceProviderFormFactory.cs) [1] [2] [3]

Builder and API Changes:

  • Updated builder methods and interfaces to use IFormFactory instead of IStartFormFactory, including method renames such as WithStartFormFactoryWithFormFactory and parameter updates in QuickStart and 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:

  • Extended session serialization methods (UseJSON, UseSimpleJSON, UseXML) to accept an optional fallbackForm parameter, 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant