gpt4 book ai didi

asp.net-core - 如何在类似 StructureMap 的 Scrutor 中注册程序集上的所有接口(interface)

转载 作者:行者123 更新时间:2023-12-04 14:22:39 25 4
gpt4 key购买 nike

如何使用 scan 在程序集中注册所有接口(interface)在 ASP.NET Core 2 中没有写的扩展全部分开?

在结构图中:

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

});

在 Scrutor 中:

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());

最佳答案

这将注册所有实现一些接口(interface)的类,默认情况下,StructureMap 会这样做:

services.Scan(scan => scan
.FromAssemblyOf<IService>()
.AddClasses()
.AsImplementedInterfaces()
.WithTransientLifetime());

关于asp.net-core - 如何在类似 StructureMap 的 Scrutor 中注册程序集上的所有接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52320413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com