You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何批量配置类型注册以减少依赖注入重复代码?

Batch Register Validator Types in Autofac

Absolutely! You can ditch that repetitive boilerplate registration code in a snap by leveraging Autofac's assembly scanning capabilities—this is exactly the kind of scenario it’s designed to solve. Instead of manually registering each IValidator<T> implementation one by one, you can set up a single batch registration that automatically picks up all your validators and applies your shared configuration.

Step-by-Step Solution

Here’s how to implement it:

  1. Target the right assembly: First, reference the assembly where your validator classes live. You can use any validator type to grab its parent assembly:

    // Use one of your existing validator types to locate the assembly
    var validatorAssembly = typeof(UserViewModelValidation).Assembly;
    
  2. Set up batch registration: Use RegisterAssemblyTypes to scan the assembly, filter for validators, and apply your universal settings:

    builder.RegisterAssemblyTypes(validatorAssembly)
           // Filter to only include classes that implement IValidator<T> (closed generic types)
           .Where(t => t.IsClosedTypeOf(typeof(IValidator<>)))
           // Register each validator as all of its implemented interfaces (matches IValidator<T>)
           .AsImplementedInterfaces()
           // Apply your shared configuration
           .PropertiesAutowired()
           .InstancePerLifetimeScope();
    

How This Works

Let’s break down the key parts:

  • RegisterAssemblyTypes(validatorAssembly): Tells Autofac to scan all types in the specified assembly.
  • Where(t => t.IsClosedTypeOf(typeof(IValidator<>))): Filters the types to only include classes that are concrete implementations of the IValidator<T> generic interface (e.g., UserViewModelValidation for IValidator<UserViewModel>).
  • AsImplementedInterfaces(): Automatically registers each validator class against all the interfaces it implements—this perfectly replaces your manual .As<IValidator<...>>() calls for each type.
  • The final .PropertiesAutowired() and .InstancePerLifetimeScope() apply your required shared configuration to every registered validator, just like your original code.

Additional Customizations

If you need to adjust the setup for edge cases:

Multiple Assemblies

If your validators are spread across multiple assemblies, pass an array of assemblies to RegisterAssemblyTypes:

var assemblies = new[] 
{ 
    typeof(UserViewModelValidation).Assembly, 
    typeof(OrderValidation).Assembly 
};

builder.RegisterAssemblyTypes(assemblies)
       .Where(t => t.IsClosedTypeOf(typeof(IValidator<>)))
       .AsImplementedInterfaces()
       .PropertiesAutowired()
       .InstancePerLifetimeScope();

Exclude Specific Types

To skip abstract base classes or other non-concrete validators, add an extra check in the Where clause:

.Where(t => t.IsClosedTypeOf(typeof(IValidator<>)) && !t.IsAbstract)

Wrap-Up

This approach eliminates the need for manual registration of every new validator you add. From now on, any class that implements IValidator<T> in your target assembly will be automatically registered with your desired configuration—no more copy-pasting those repetitive lines!

内容的提问来源于stack exchange,提问作者Ciaran Gallagher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:20:34