首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Scrutor相似的StructureMap中注册组件上的所有接口

如何在Scrutor相似的StructureMap中注册组件上的所有接口
EN

Stack Overflow用户
提问于 2018-09-13 19:17:53
回答 2查看 3.7K关注 0票数 3

如何使用scan扩展注册程序集中的所有接口,而不必在ASP.NET核心2中完全分离?

在StructureMap中:

代码语言:javascript
复制
Scan(_ =>
{
    // Declare which assemblies to scan
    _.Assembly("StructureMap.Testing"); 

});

在巡洋舰:

代码语言:javascript
复制
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());
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-17 12:42:24

这将注册与StructureMap一样实现某些接口的所有类,默认情况下:

代码语言:javascript
复制
services.Scan(scan => scan
    .FromAssemblyOf<IService>()
    .AddClasses()
    .AsImplementedInterfaces()
    .WithTransientLifetime());
票数 4
EN

Stack Overflow用户

发布于 2019-12-14 20:35:12

对于所有类型的使用寿命,您可以定义

代码语言:javascript
复制
services.Scan(scan => scan
      .FromAssemblyOf<IApplicationService>()

      .AddClasses(classes => classes.AssignableTo<IScopedDependency>())
      .AsMatchingInterface()
      .WithScopedLifetime()

      .AddClasses(classes => classes.AssignableTo<ISingletonDependency>())
      .AsMatchingInterface()
      .WithSingletonLifetime()

      .AddClasses(classes => classes.AssignableTo<ITransientDependency>())
      .AsMatchingInterface()
      .WithTransientLifetime()
);

接口在哪里

代码语言:javascript
复制
public interface IApplicationService { }
public interface IScopedDependency { }
public interface ITransientDependency { }
public interface ISingletonDependency { }

以及接口中的示例

代码语言:javascript
复制
public interface IUserService : IScopedDependency { }
public class UserService: IUserService { }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52320413

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档