Add project files.
This commit is contained in:
18
WinGreedWPF/StartupHelpers/AbstractFactory.cs
Normal file
18
WinGreedWPF/StartupHelpers/AbstractFactory.cs
Normal file
@ -0,0 +1,18 @@
|
||||
using System;
|
||||
|
||||
namespace WinGreedWPF.StartupHelpers;
|
||||
|
||||
public class AbstractFactory<T> : IAbstractFactory<T>
|
||||
{
|
||||
private readonly Func<T> _factory;
|
||||
|
||||
public AbstractFactory(Func<T> factory)
|
||||
{
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
public T Create()
|
||||
{
|
||||
return _factory();
|
||||
}
|
||||
}
|
||||
7
WinGreedWPF/StartupHelpers/IAbstractFactory.cs
Normal file
7
WinGreedWPF/StartupHelpers/IAbstractFactory.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace WinGreedWPF.StartupHelpers
|
||||
{
|
||||
public interface IAbstractFactory<T>
|
||||
{
|
||||
T Create();
|
||||
}
|
||||
}
|
||||
15
WinGreedWPF/StartupHelpers/ServiceExtensions.cs
Normal file
15
WinGreedWPF/StartupHelpers/ServiceExtensions.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
|
||||
namespace WinGreedWPF.StartupHelpers;
|
||||
|
||||
public static class ServiceExtensions
|
||||
{
|
||||
public static void AddFormFactory<TForm>(this IServiceCollection services)
|
||||
where TForm : class
|
||||
{
|
||||
services.AddTransient<TForm>();
|
||||
services.AddSingleton<Func<TForm>>(x => () => x.GetService<TForm>()!);
|
||||
services.AddSingleton<IAbstractFactory<TForm>, AbstractFactory<TForm>>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user