feat: add DI form factory
This commit is contained in:
parent
df7880fcf3
commit
b2d4c4c3f8
12
Examples/EFCoreBot/Database/BotDbContext.cs
Normal file
12
Examples/EFCoreBot/Database/BotDbContext.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace EFCoreBot.Database;
|
||||
|
||||
public class BotDbContext : DbContext
|
||||
{
|
||||
public BotDbContext(DbContextOptions options) : base(options)
|
||||
{
|
||||
}
|
||||
|
||||
public DbSet<User> Users { get; set; }
|
||||
}
|
||||
7
Examples/EFCoreBot/Database/User.cs
Normal file
7
Examples/EFCoreBot/Database/User.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace EFCoreBot.Database;
|
||||
|
||||
public class User
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public string LastMessage { get; set; }
|
||||
}
|
||||
19
Examples/EFCoreBot/EFCoreBot.csproj
Normal file
19
Examples/EFCoreBot/EFCoreBot.csproj
Normal file
@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>disable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.8" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="6.0.8" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
23
Examples/EFCoreBot/Program.cs
Normal file
23
Examples/EFCoreBot/Program.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using EFCoreBot;
|
||||
using EFCoreBot.Database;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using TelegramBotBase.Builder;
|
||||
|
||||
var serviceCollection = new ServiceCollection()
|
||||
.AddDbContext<BotDbContext>(x => x.UseInMemoryDatabase("TelegramBotBase"));
|
||||
|
||||
var serviceProvider = serviceCollection.BuildServiceProvider();
|
||||
|
||||
var bot = BotBaseBuilder.Create()
|
||||
.WithAPIKey(Environment.GetEnvironmentVariable("API_KEY") ?? throw new Exception("API_KEY is not set"))
|
||||
.DefaultMessageLoop()
|
||||
.WithServiceProvider<StartForm>(serviceProvider)
|
||||
.NoProxy()
|
||||
.NoCommands()
|
||||
.NoSerialization()
|
||||
.DefaultLanguage()
|
||||
.Build();
|
||||
|
||||
bot.Start();
|
||||
await Task.Delay(-1);
|
||||
36
Examples/EFCoreBot/StartForm.cs
Normal file
36
Examples/EFCoreBot/StartForm.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using EFCoreBot.Database;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace EFCoreBot;
|
||||
|
||||
public class StartForm : FormBase
|
||||
{
|
||||
private readonly BotDbContext _dbContext;
|
||||
|
||||
public StartForm(BotDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public override async Task Load(MessageResult message)
|
||||
{
|
||||
var user = await _dbContext.Users.FindAsync(Device.DeviceId);
|
||||
if (user is null)
|
||||
{
|
||||
user = new User
|
||||
{
|
||||
Id = Device.DeviceId,
|
||||
LastMessage = "<unknown>"
|
||||
};
|
||||
|
||||
_dbContext.Users.Add(user);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
await Device.Send($"Your last message's text was: `{user.LastMessage}`");
|
||||
|
||||
user.LastMessage = string.IsNullOrWhiteSpace(message.MessageText) ? "<unknown>" : message.MessageText;
|
||||
await _dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,7 @@ using Telegram.Bot.Types;
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Builder.Interfaces;
|
||||
using TelegramBotBase.Commands;
|
||||
using TelegramBotBase.Factories;
|
||||
using TelegramBotBase.Form;
|
||||
using TelegramBotBase.Interfaces;
|
||||
using TelegramBotBase.Localizations;
|
||||
@ -148,7 +149,7 @@ namespace TelegramBotBase.Builder
|
||||
#endregion
|
||||
|
||||
|
||||
#region "Step 3 (Start Form/Factory)"
|
||||
#region "Step 3 (Start Form/Factory)"
|
||||
|
||||
public INetworkingSelectionStage WithStartForm(Type startFormClass)
|
||||
{
|
||||
@ -163,6 +164,19 @@ namespace TelegramBotBase.Builder
|
||||
return this;
|
||||
}
|
||||
|
||||
public INetworkingSelectionStage WithServiceProvider(Type startFormClass, IServiceProvider serviceProvider)
|
||||
{
|
||||
this._factory = new ServiceProviderStartFormFactory(startFormClass, serviceProvider);
|
||||
return this;
|
||||
}
|
||||
|
||||
public INetworkingSelectionStage WithServiceProvider<T>(IServiceProvider serviceProvider)
|
||||
where T : FormBase
|
||||
{
|
||||
this._factory = new ServiceProviderStartFormFactory<T>(serviceProvider);
|
||||
return this;
|
||||
}
|
||||
|
||||
public INetworkingSelectionStage WithStartFormFactory(IStartFormFactory factory)
|
||||
{
|
||||
this._factory = factory;
|
||||
|
||||
@ -23,6 +23,22 @@ namespace TelegramBotBase.Builder.Interfaces
|
||||
/// <returns></returns>
|
||||
INetworkingSelectionStage WithStartForm<T>() where T : FormBase, new();
|
||||
|
||||
/// <summary>
|
||||
/// Chooses a StartFormFactory 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.
|
||||
/// </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.
|
||||
/// </summary>
|
||||
|
||||
35
TelegramBotBase/Factories/ServiceProviderStartFormFactory.cs
Normal file
35
TelegramBotBase/Factories/ServiceProviderStartFormFactory.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using TelegramBotBase.Form;
|
||||
using TelegramBotBase.Interfaces;
|
||||
|
||||
namespace TelegramBotBase.Factories
|
||||
{
|
||||
public class ServiceProviderStartFormFactory : IStartFormFactory
|
||||
{
|
||||
private readonly Type _startFormClass;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public ServiceProviderStartFormFactory(Type startFormClass, IServiceProvider serviceProvider)
|
||||
{
|
||||
if (!typeof(FormBase).IsAssignableFrom(startFormClass))
|
||||
throw new ArgumentException("startFormClass argument must be a FormBase type");
|
||||
|
||||
_startFormClass = startFormClass;
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
public FormBase CreateForm()
|
||||
{
|
||||
return (FormBase)ActivatorUtilities.CreateInstance(_serviceProvider, _startFormClass);
|
||||
}
|
||||
}
|
||||
|
||||
public class ServiceProviderStartFormFactory<T> : ServiceProviderStartFormFactory
|
||||
where T : FormBase
|
||||
{
|
||||
public ServiceProviderStartFormFactory(IServiceProvider serviceProvider) : base(typeof(T), serviceProvider)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -23,6 +23,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
|
||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
@ -26,6 +26,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Extensions", "Extensions",
|
||||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TelegramBotBase.Extensions.Serializer.Database.MSSQL", "TelegramBotBase.Extensions.Serializer.Database.MSSQL\TelegramBotBase.Extensions.Serializer.Database.MSSQL.csproj", "{889B170E-32E9-4F26-BB04-8D06EA367857}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFCoreBot", "Examples\EFCoreBot\EFCoreBot.csproj", "{261BED47-0404-4A9A-86FC-047DE42A7D25}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@ -60,6 +62,10 @@ Global
|
||||
{889B170E-32E9-4F26-BB04-8D06EA367857}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{889B170E-32E9-4F26-BB04-8D06EA367857}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{889B170E-32E9-4F26-BB04-8D06EA367857}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{261BED47-0404-4A9A-86FC-047DE42A7D25}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{261BED47-0404-4A9A-86FC-047DE42A7D25}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{261BED47-0404-4A9A-86FC-047DE42A7D25}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{261BED47-0404-4A9A-86FC-047DE42A7D25}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@ -70,6 +76,7 @@ Global
|
||||
{B5DDFA45-0E01-46A5-B67D-541300CDD606} = {E3193182-6FDA-4FA3-AD26-A487291E7681}
|
||||
{673A56F5-6110-4AED-A68D-562FD6ED3EA6} = {BFA71E3F-31C0-4FC1-A320-4DCF704768C5}
|
||||
{889B170E-32E9-4F26-BB04-8D06EA367857} = {E3193182-6FDA-4FA3-AD26-A487291E7681}
|
||||
{261BED47-0404-4A9A-86FC-047DE42A7D25} = {BFA71E3F-31C0-4FC1-A320-4DCF704768C5}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {59CB40E1-9FA7-4867-A56F-4F418286F057}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user