jeudi 28 mai 2020

SimpleInjector Lazy in a Reflection

We are using SimpleInjector as a Dependency Injector, and we are registering all interface types using assembly iteration.

public static void RegisterInterfaceTypes(this Container container, Assembly assembly)
{
    assembly.GetExportedTypes()
        .Select(t => new {
            Type = t,
            Interface = t.GetInterfaces().FirstOrDefault()
        })
        .ToList()
        .ForEach(t =>
        {
            container.Register(t.Interface, t.Type, Lifestyle.Transient);
        });
}

We also have lazy classes to register. We can register these classes like below one by one. But we want to register all lazy types with similar iteration using reflection.

container.Register(() => new Lazy<ICommonBusiness>(container.GetInstance<CommonBusiness>));




Aucun commentaire:

Enregistrer un commentaire