dimanche 20 août 2017

Using Simple Injector to register lazy parameters by reflection

I register all implementations of IService interface in Simple Injector by Reflection:

var registrations = assembly.GetExportedTypes()
   .Where(type => type.IsClass && baseType.IsAssignableFrom(IService))
   .Select(type => new { Interface = type.GetInterfaces().First(), Implementation = type });

foreach (var reg in registrations)
{
    container.Register(reg.Interface, reg.Implementation, Lifestyle.Transient);
}

I found Lazy Register solution to registering one instance of an interface. But I want register all implementations in lazy mode by Reflection. my desired solution is:

foreach (var reg in registrations)
{
    container.Register(() => new Lazy<reg.Interface>(container.GetInstance<typeof(reg.Implementation) >), Lifestyle.Transient);
}

Is there any solution to the problem described?





Aucun commentaire:

Enregistrer un commentaire