如何在 Scrutor 中注册组件上的所有接口(类似 StructureMap)

2024-03-17

如何在程序集中注册所有接口scan扩展名没有在 ASP.NET Core 2 中全部分开写入?

在结构图中:

Scan(_ =>
{
    // Declare which assemblies to scan
    _.Assembly("StructureMap.Testing"); 

});

在巡查员中:

collection.Scan(scan => scan
     // We start out with all types in the assembly of ITransientService
    .FromAssemblyOf<ITransientService>()
        // AddClasses starts out with all public, non-abstract types in this
        // assembly. These types are then filtered by the delegate passed to the
        // method. In this case, we filter out only the classes that are assignable
        // to ITransientService.
        .AddClasses(classes => classes.AssignableTo<ITransientService>())
            // We then specify what type we want to register these classes as.
            // In this case, we want to register the types as all of its implemented
            // interfaces. So if a type implements 3 interfaces; A, B, C, we'd end
            // up with three separate registrations.
            .AsImplementedInterfaces()
            // And lastly, we specify the lifetime of these registrations.
            .WithTransientLifetime()
        // Here we start again, with a new full set of classes from the assembly
        // above. This time, filtering out only the classes assignable to
        // IScopedService.
        .AddClasses(classes => classes.AssignableTo<IScopedService>())
            // Now, we just want to register these types as a single interface,
            // IScopedService.
            .As<IScopedService>()
            // And again, just specify the lifetime.
            .WithScopedLifetime());

这将注册所有实现某些接口的类,就像 StructureMap 默认情况下所做的那样:

services.Scan(scan => scan
    .FromAssemblyOf<IService>()
    .AddClasses()
    .AsImplementedInterfaces()
    .WithTransientLifetime());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Scrutor 中注册组件上的所有接口(类似 StructureMap) 的相关文章

随机推荐