تزریق وابستگیها به صورت پویا در فروشگاهساز Nop Commerce
نویسنده: ارشاد رئوفی
تاریخ: ۱۴۰۱/۱۰/۲۰ ۲۰:۲۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
namespace Nop.Services
{
public interface ICustomService
{
protected InjectType Inject { get; }
protected int Order { get; }
protected ImplementationType implementationType { get; }
}
public enum ImplementationType
{
WithInterface = 0,
WithoutInterface = 1
}
public enum InjectType
{
Scopped=0,
Transit=1,
SingleTon=2
}
} namespace Nop.Services
{
public interface IMyCustomService: ICustomService
{
int ok();
}
} namespace Nop.Services
{
public class MyCustomService : IMyCustomService
{
public InjectType Inject { get; }
public int Order { get; }
public ImplementationType implementationType { get; }
public MyCustomService()
{
implementationType = ImplementationType.WithInterface;
Inject = InjectType.Scopped;
Order = 1;
}
public int ok()
{
return 10;
}
}
} namespace Nop.Web.Framework.Infrastructure
{
public class CustomDependencyInjection : NopStartup
{
private static bool IsSubInterface(Type t1, Type t2)
{
if (!t2.IsAssignableFrom(t1))
return false;
if (t1.BaseType == null)
return true;
return !t2.IsAssignableFrom(t1.BaseType);
}
public override void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
//-------------Get All Services-------------
var asm = AppDomain.CurrentDomain
.GetAssemblies()
.Single(x => x.FullName.Contains("Nop.Services"));
//-------------find Services that inheriance of ICustomService-------------
var types = asm.DefinedTypes.Where(x => IsSubInterface(x, typeof(ICustomService)));
//-----------Get All Custom Service Classess-------
var allRelatedClassServices = types
.Where(x => x.IsClass)
.OrderBy(x=>(Int32)x.GetProperty("Order")
.GetValue(Activator.CreateInstance(x), null));
//-----------Get All Custom Service Interfaces-------
var allRelatedInterfaceServices = types.Where(x => x.IsInterface);
//-----------Matche Class Services To Related Interface Services-------
TypeInfo interfaceService=null;
foreach (var classService in allRelatedClassServices)
{
//-----------detect Implementation Type for service-----------
var implementationValue = (ImplementationType)classService.GetProperty("implementationType")
.GetValue(Activator.CreateInstance(classService), null);
//-----------detect inject type for service-----------
var InjectValue = (InjectType)classService.GetProperty("Inject")
.GetValue(Activator.CreateInstance(classService), null);
//-----------get related interface for service class-----------
if (implementationValue == ImplementationType.WithInterface)
interfaceService = allRelatedInterfaceServices.Single(x => x.Name == $"I{classService.Name}");
//----------finally Add Custom Service To Service Collection-----------
switch (InjectValue)
{
case InjectType.Scopped:
if(interfaceService!=null)
services.AddScoped(interfaceService, classService);
else
services.AddScoped(classService);
break;
case InjectType.Transit:
if (interfaceService != null)
services.AddTransient(interfaceService, classService);
else
services.AddTransient(classService);
break;
case InjectType.SingleTon:
if (interfaceService != null)
services.AddSingleton(interfaceService, classService);
else
services.AddSingleton(classService);
break;
default:
break;
}
interfaceService = null;
}
}
}
}