Skip to content
2 changes: 1 addition & 1 deletion TelegramBotBase/BotBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ internal BotBase(string apiKey, MessageClient client)
/// <summary>
/// Offers functionality to manage the creation process of the start form.
/// </summary>
public IStartFormFactory StartFormFactory { get; internal set; }
public IFormFactory FormFactory { get; internal set; }

/// <summary>
/// Contains the message loop factory, which cares about "message-management."
Expand Down
46 changes: 23 additions & 23 deletions TelegramBotBase/Builder/BotBaseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class BotBaseBuilder : IAPIKeySelectionStage, IMessageLoopSelectionStage,

private MessageClient _client;

private IStartFormFactory _factory;
private IFormFactory _factory;

private IMessageLoopFactory _messageLoopFactory;

Expand All @@ -49,7 +49,7 @@ public BotBase Build()
{
var bot = new BotBase(_apiKey, _client)
{
StartFormFactory = _factory,
FormFactory = _factory,
BotCommandScopes = BotCommandScopes,
StateMachine = _stateMachine,
MessageLoopFactory = _messageLoopFactory
Expand All @@ -75,7 +75,7 @@ public IMessageLoopSelectionStage WithAPIKey(string apiKey)
public IBuildingStage QuickStart(string apiKey, Type startForm, bool throwPendingUpdates = false)
{
_apiKey = apiKey;
_factory = new DefaultStartFormFactory(startForm);
_factory = new DefaultFormFactory(startForm);

DefaultMessageLoop();

Expand All @@ -97,7 +97,7 @@ public IBuildingStage QuickStart<T>(string apiKey, bool throwPendingUpdates = fa
where T : FormBase
{
_apiKey = apiKey;
_factory = new DefaultStartFormFactory(typeof(T));
_factory = new DefaultFormFactory(typeof(T));

DefaultMessageLoop();

Expand All @@ -114,10 +114,10 @@ public IBuildingStage QuickStart<T>(string apiKey, bool throwPendingUpdates = fa
return this;
}

public IBuildingStage QuickStart(string apiKey, IStartFormFactory startFormFactory, bool throwPendingUpdates = false)
public IBuildingStage QuickStart(string apiKey, IFormFactory formFactory, bool throwPendingUpdates = false)
{
_apiKey = apiKey;
_factory = startFormFactory;
_factory = formFactory;

DefaultMessageLoop();

Expand Down Expand Up @@ -200,31 +200,31 @@ public IStartFormSelectionStage CustomMessageLoop<T>()

public INetworkingSelectionStage WithStartForm(Type startFormClass)
{
_factory = new DefaultStartFormFactory(startFormClass);
_factory = new DefaultFormFactory(startFormClass);
return this;
}

public INetworkingSelectionStage WithStartForm<T>()
where T : FormBase, new()
{
_factory = new DefaultStartFormFactory(typeof(T));
_factory = new DefaultFormFactory(typeof(T));
return this;
}

public INetworkingSelectionStage WithServiceProvider(Type startFormClass, IServiceProvider serviceProvider)
{
_factory = new ServiceProviderStartFormFactory(startFormClass, serviceProvider);
_factory = new ServiceProviderFormFactory(startFormClass, serviceProvider);
return this;
}

public INetworkingSelectionStage WithServiceProvider<T>(IServiceProvider serviceProvider)
where T : FormBase
{
_factory = new ServiceProviderStartFormFactory<T>(serviceProvider);
_factory = new ServiceProviderFormFactory<T>(serviceProvider);
return this;
}

public INetworkingSelectionStage WithStartFormFactory(IStartFormFactory factory)
public INetworkingSelectionStage WithFormFactory(IFormFactory factory)
{
_factory = factory;
return this;
Expand Down Expand Up @@ -356,10 +356,10 @@ public ILanguageSelectionStage UseSerialization(IStateMachine machine)
/// </summary>
/// <seealso href="https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/">For the legacy version use the UseNewtonsoftJson method of TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson</seealso>
/// <returns></returns>
public ILanguageSelectionStage UseJSON()
public ILanguageSelectionStage UseJSON(Type fallbackForm = null)
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "states.json");
_stateMachine = new JsonStateMachine(path);
_stateMachine = new JsonStateMachine(path, fallbackForm);
return this;
}

Expand All @@ -368,51 +368,51 @@ public ILanguageSelectionStage UseJSON()
/// </summary>
/// <seealso href="https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/">For the legacy version use the UseNewtonsoftJson method of TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson</seealso>
/// <returns></returns>
public ILanguageSelectionStage UseJSON(string path)
public ILanguageSelectionStage UseJSON(string path, Type fallbackForm = null)
{
_stateMachine = new JsonStateMachine(path);
_stateMachine = new JsonStateMachine(path, fallbackForm);
return this;
}

/// <summary>
/// Uses the application runtime path to load and write a states.json file.
/// </summary>
/// <returns></returns>
public ILanguageSelectionStage UseSimpleJSON()
public ILanguageSelectionStage UseSimpleJSON(Type fallbackForm = null)
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "states.json");
_stateMachine = new SimpleJsonStateMachine(path);
_stateMachine = new SimpleJsonStateMachine(path, fallbackForm);
return this;
}

/// <summary>
/// Uses the given path to load and write a states.json file.
/// </summary>
/// <returns></returns>
public ILanguageSelectionStage UseSimpleJSON(string path)
public ILanguageSelectionStage UseSimpleJSON(string path, Type fallbackForm = null)
{
_stateMachine = new SimpleJsonStateMachine(path);
_stateMachine = new SimpleJsonStateMachine(path, fallbackForm);
return this;
}

/// <summary>
/// Uses the application runtime path to load and write a states.xml file.
/// </summary>
/// <returns></returns>
public ILanguageSelectionStage UseXML()
public ILanguageSelectionStage UseXML(Type fallbackForm = null)
{
var path = Path.Combine(Directory.GetCurrentDirectory(), "states.xml");
_stateMachine = new XmlStateMachine(path);
_stateMachine = new XmlStateMachine(path, fallbackForm);
return this;
}

/// <summary>
/// Uses the given path to load and write a states.xml file.
/// </summary>
/// <returns></returns>
public ILanguageSelectionStage UseXML(string path)
public ILanguageSelectionStage UseXML(string path, Type fallbackForm = null)
{
_stateMachine = new XmlStateMachine(path);
_stateMachine = new XmlStateMachine(path, fallbackForm);
return this;
}

Expand Down
4 changes: 2 additions & 2 deletions TelegramBotBase/Builder/Interfaces/IAPIKeySelectionStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public interface IAPIKeySelectionStage
/// Uses: DefaultMessageLoop, NoProxy, OnlyStart, NoSerialization, DefaultLanguage
/// </summary>
/// <param name="apiKey"></param>
/// <param name="StartFormFactory"></param>
/// <param name="formFactory"></param>
/// <param name="throwPendingUpdates">Indicates if all pending Telegram.Bot.Types.Updates should be thrown out before start polling.</param>
/// <returns></returns>
IBuildingStage QuickStart(string apiKey, IStartFormFactory StartFormFactory, bool throwPendingUpdates = false);
IBuildingStage QuickStart(string apiKey, IFormFactory formFactory, bool throwPendingUpdates = false);
}
21 changes: 12 additions & 9 deletions TelegramBotBase/Builder/Interfaces/ISessionSerializationStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public interface ISessionSerializationStage
/// </para>
/// <seealso href="https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/">For the legacy version use the UseNewtonsoftJson method of TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson</seealso>
/// </remarks>
/// <param name="path"></param>
/// <param name="fallbackForm"></param>
/// <returns></returns>
/// <seealso cref="UseNewtonsoftJson"/>
ILanguageSelectionStage UseJSON();
ILanguageSelectionStage UseJSON(Type fallbackForm = null);

/// <summary>
/// Using the complex version of .Net JSON, which can serialize all objects.
Expand All @@ -47,39 +47,42 @@ public interface ISessionSerializationStage
/// <seealso href="https://www.nuget.org/packages/TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson/">For the legacy version use the UseNewtonsoftJson method of TelegramBotBase.Extensions.Serializer.Legacy.NewtonsoftJson</seealso>
/// </remarks>
/// <param name="path"></param>
/// <param name="fallbackForm"></param>
/// <returns></returns>
/// <seealso cref="UseNewtonsoftJson"/>
ILanguageSelectionStage UseJSON(string path);
ILanguageSelectionStage UseJSON(string path, Type fallbackForm = null);

/// <summary>
/// Use the easy version of .Net JSON, which can serialize basic types, but not generics and others.
/// Saves in application directory.
/// </summary>
/// <param name="path"></param>
/// <param name="fallbackForm"></param>
/// <returns></returns>
[Obsolete("Use UseJSON instead.")]
ILanguageSelectionStage UseSimpleJSON();
ILanguageSelectionStage UseSimpleJSON(Type fallbackForm = null);

/// <summary>
/// Use the easy version of .Net JSON, which can serialize basic types, but not generics and others.
/// </summary>
/// <param name="path"></param>
/// <param name="fallbackForm"></param>
/// <returns></returns>
[Obsolete("Use UseJSON instead.")]
ILanguageSelectionStage UseSimpleJSON(string path);
ILanguageSelectionStage UseSimpleJSON(string path, Type fallbackForm = null);

/// <summary>
/// Uses the XML serializer for session serialization.
/// Saves in application directory.
/// </summary>
/// <param name="path"></param>
/// <param name="fallbackForm"></param>
/// <returns></returns>
ILanguageSelectionStage UseXML();
ILanguageSelectionStage UseXML(Type fallbackForm = null);

/// <summary>
/// Uses the XML serializer for session serialization.
/// </summary>
/// <param name="path"></param>
/// <param name="fallbackForm"></param>
/// <returns></returns>
ILanguageSelectionStage UseXML(string path);
ILanguageSelectionStage UseXML(string path, Type fallbackForm = null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@ public interface IStartFormSelectionStage
INetworkingSelectionStage WithStartForm<T>() where T : FormBase, new();

/// <summary>
/// Chooses a StartFormFactory which will be use for new sessions.
/// Chooses a FormFactory which will be use for new sessions.
/// </summary>
/// <param name="startFormClass"></param>
/// <param name="serviceProvider"></param>
/// <returns></returns>
INetworkingSelectionStage WithServiceProvider(Type startFormClass, IServiceProvider serviceProvider);

/// <summary>
/// Chooses a StartFormFactory which will be use for new sessions.
/// Chooses a FormFactory which will be use for new sessions.
/// </summary>
/// <param name="serviceProvider"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
INetworkingSelectionStage WithServiceProvider<T>(IServiceProvider serviceProvider) where T : FormBase;

/// <summary>
/// Chooses a StartFormFactory which will be use for new sessions.
/// Chooses a FormFactory which will be use for new sessions.
/// </summary>
/// <param name="factory"></param>
/// <returns></returns>
INetworkingSelectionStage WithStartFormFactory(IStartFormFactory factory);
INetworkingSelectionStage WithFormFactory(IFormFactory factory);
}
47 changes: 47 additions & 0 deletions TelegramBotBase/Factories/DefaultFormFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;

namespace TelegramBotBase.Factories;

public class DefaultFormFactory : IFormFactory
{
private readonly Type _startFormClass;

public DefaultFormFactory(Type startFormClass)
{
if (!typeof(FormBase).IsAssignableFrom(startFormClass))
{
throw new ArgumentException($"{nameof(startFormClass)} argument must be a {nameof(FormBase)} type");
}

_startFormClass = startFormClass;
}


public FormBase CreateStartForm()
{
return CreateForm(_startFormClass);
}

public FormBase CreateForm(Type formType)
{
if (!typeof(FormBase).IsAssignableFrom(formType))
{
throw new ArgumentException($"{nameof(formType)} argument must be a {nameof(FormBase)} type");
}

// No parameterless constructor
if (!(formType.GetConstructor(new Type[] { })?.Invoke(new object[] { }) is FormBase form))
{
throw new Exception($"{formType} must have a parameterless constructor.");
}

return form;
}

public FormBase CreateForm<T>() where T : FormBase
{
return CreateForm(typeof(T));
}
}
26 changes: 0 additions & 26 deletions TelegramBotBase/Factories/DefaultStartFormFactory.cs

This file was deleted.

35 changes: 35 additions & 0 deletions TelegramBotBase/Factories/LambdaFormFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;

namespace TelegramBotBase.Factories;

public class LambdaFormFactory : IFormFactory
{
public delegate FormBase CreateFormDelegate();

private readonly CreateFormDelegate _lambda;

private readonly DefaultFormFactory _defaultFormFactory;

public LambdaFormFactory(CreateFormDelegate lambda)
{
_lambda = lambda;
_defaultFormFactory = new DefaultFormFactory(lambda.GetType());
}

public FormBase CreateStartForm()
{
return _lambda();
}

public FormBase CreateForm(Type formType)
{
return _defaultFormFactory.CreateForm(formType);
}

public FormBase CreateForm<T>() where T : FormBase
{
return CreateForm(typeof(T));
}
}
Loading