Add project files.
This commit is contained in:
41
FactoryPattern/Factories/AbstractFactory.cs
Normal file
41
FactoryPattern/Factories/AbstractFactory.cs
Normal file
@ -0,0 +1,41 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FactoryPattern.Factories;
|
||||
//builder.Services.AddTransient<ISample1, Sample1>();
|
||||
//builder.Services.AddSingleton<Func<ISample1>>(x => () => x.GetService<ISample1>());
|
||||
|
||||
public static class AbstractFactoryExtension
|
||||
{
|
||||
public static void AddAbstractFactory<TInterface, TImplementation>(this IServiceCollection services)
|
||||
where TInterface : class
|
||||
where TImplementation : class, TInterface
|
||||
{
|
||||
services.AddTransient<TInterface, TImplementation>();
|
||||
services.AddSingleton<Func<TInterface>>(x => () => x.GetService<TInterface>()!);
|
||||
services.AddSingleton<IAbstractFactory<TInterface>, AbstractFactory<TInterface>>();
|
||||
}
|
||||
}
|
||||
|
||||
public class AbstractFactory<T> : IAbstractFactory<T>
|
||||
{
|
||||
private readonly Func<T> _factory;
|
||||
|
||||
public AbstractFactory(Func<T> factory)
|
||||
{
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
public T Create()
|
||||
{
|
||||
return _factory();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IAbstractFactory<T>
|
||||
{
|
||||
T Create();
|
||||
}
|
||||
45
FactoryPattern/Factories/DifferentImplementationsFactory.cs
Normal file
45
FactoryPattern/Factories/DifferentImplementationsFactory.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using FactoryPattern.Samples;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FactoryPattern.Factories;
|
||||
|
||||
|
||||
public static class DifferentImplementationsFactoryExtension
|
||||
{
|
||||
public static void AddVehicleFactory(this IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<IVehicle, Car>();
|
||||
services.AddTransient<IVehicle, Truck>();
|
||||
services.AddTransient<IVehicle, Van>();
|
||||
services.AddSingleton<Func<IEnumerable<IVehicle>>>
|
||||
(x => () => x.GetService<IEnumerable<IVehicle>>()!);
|
||||
services.AddSingleton<IVehicleFactory,VehicleFactory >();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public interface IVehicleFactory
|
||||
{
|
||||
IVehicle Create(string name);
|
||||
}
|
||||
|
||||
public class VehicleFactory : IVehicleFactory
|
||||
{
|
||||
private readonly Func<IEnumerable<IVehicle>> _factory;
|
||||
|
||||
public VehicleFactory(Func<IEnumerable<IVehicle>> factory)
|
||||
{
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
public IVehicle Create(string name)
|
||||
{
|
||||
var set = _factory();
|
||||
IVehicle output = set.Where(x => x.VehicleType == name).First();
|
||||
return output;
|
||||
}
|
||||
}
|
||||
40
FactoryPattern/Factories/GenerateClassWithDataFactory.cs
Normal file
40
FactoryPattern/Factories/GenerateClassWithDataFactory.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using FactoryPattern.Samples;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace FactoryPattern.Factories;
|
||||
|
||||
public static class GenerateClassWithDataFactoryExtension
|
||||
{
|
||||
public static void AddGenericClassWithDataFactory(this IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<IUserData, UserData>();
|
||||
services.AddSingleton<Func<IUserData>>(x => () => x.GetService<IUserData>()!);
|
||||
services.AddSingleton<IUserDataFactory, UserDataFactory>();
|
||||
}
|
||||
}
|
||||
|
||||
public interface IUserDataFactory
|
||||
{
|
||||
IUserData Create(string name);
|
||||
}
|
||||
|
||||
public class UserDataFactory : IUserDataFactory
|
||||
{
|
||||
private readonly Func<IUserData> _factory;
|
||||
|
||||
public UserDataFactory(Func<IUserData> factory)
|
||||
{
|
||||
_factory = factory;
|
||||
}
|
||||
|
||||
public IUserData Create(string name)
|
||||
{
|
||||
var output = _factory();
|
||||
output.Name = name;
|
||||
return output;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user