using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FactoryPattern.Factories; //builder.Services.AddTransient(); //builder.Services.AddSingleton>(x => () => x.GetService()); public static class AbstractFactoryExtension { public static void AddAbstractFactory(this IServiceCollection services) where TInterface : class where TImplementation : class, TInterface { services.AddTransient(); services.AddSingleton>(x => () => x.GetService()!); services.AddSingleton, AbstractFactory>(); } } public class AbstractFactory : IAbstractFactory { private readonly Func _factory; public AbstractFactory(Func factory) { _factory = factory; } public T Create() { return _factory(); } } public interface IAbstractFactory { T Create(); }