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

如何在Simple Injector中注入IEnumerable<Func<T>>以获取特定事件处理器?

Hey there! Let's work through how to inject IEnumerable<Func<T>> for your event handlers using Simple Injector, based on your existing setup.

First, let's recap your current setup

You've already registered all your domain event handlers with:

container.RegisterCollection(typeof(IHandleDomainEvent<>), AppDomain.CurrentDomain.GetAssemblies());

And you have multiple implementations (like Reservation) targeting the same event type (OrderConfirmed).

The Good News: Simple Injector Handles This Natively

Simple Injector has built-in support for resolving Func<T> delegates and collections of them. You don't need any fancy extra configuration for most cases. Here's how to make it work:

1. Inject the factory collection directly into your consumer

Just declare IEnumerable<Func<IHandleDomainEvent<OrderConfirmed>>> as a constructor dependency in the class that needs to trigger the event handlers. For example:

public class OrderProcessingService
{
    private readonly IEnumerable<Func<IHandleDomainEvent<OrderConfirmed>>> _orderConfirmedHandlerFactories;

    public OrderProcessingService(IEnumerable<Func<IHandleDomainEvent<OrderConfirmed>>> orderConfirmedHandlerFactories)
    {
        _orderConfirmedHandlerFactories = orderConfirmedHandlerFactories;
    }

    public void ConfirmOrder(OrderConfirmed eventData)
    {
        // Resolve and execute each handler
        foreach (var handlerFactory in _orderConfirmedHandlerFactories)
        {
            var handler = handlerFactory();
            handler.Handle(eventData); // Assuming your IHandleDomainEvent<T> has a Handle method
        }
    }
}

Simple Injector will automatically map each registered IHandleDomainEvent<OrderConfirmed> implementation to a Func<IHandleDomainEvent<OrderConfirmed>> factory, so this injection will just work.

2. Generic Support for Any Event Type

If you want a reusable event dispatcher that works with any domain event type, create a generic class that accepts the factory collection:

public class GenericEventDispatcher<TEvent> where TEvent : IDomainEvent
{
    private readonly IEnumerable<Func<IHandleDomainEvent<TEvent>>> _handlerFactories;

    public GenericEventDispatcher(IEnumerable<Func<IHandleDomainEvent<TEvent>>> handlerFactories)
    {
        _handlerFactories = handlerFactories;
    }

    public void Dispatch(TEvent eventData)
    {
        foreach (var factory in _handlerFactories)
        {
            factory().Handle(eventData);
        }
    }
}

Simple Injector will resolve this generic type automatically for any event type you need, as long as you've registered the corresponding handlers via RegisterCollection.

Tweaking Lifecycle Behavior

By default, the Func<T> delegates will respect the lifecycle you set for your handlers:

  • If your handlers are registered as Transient (the default for RegisterCollection), each call to handlerFactory() will create a new instance.
  • If you registered handlers as Scoped or Singleton, the factory will return the same instance for the scope (or lifetime) respectively.

To explicitly set the lifecycle for all handlers during registration:

container.RegisterCollection(
    typeof(IHandleDomainEvent<>),
    assemblies: AppDomain.CurrentDomain.GetAssemblies(),
    lifestyle: Lifestyle.Transient); // Or Lifestyle.Scoped/Singleton

Edge Case: Explicit Factory Registration (Rarely Needed)

If for some reason you need to manually register the factory collection (e.g., custom filtering), you can do this:

var handlerTypes = container.GetTypesToRegister(
    typeof(IHandleDomainEvent<>), 
    AppDomain.CurrentDomain.GetAssemblies());

container.RegisterCollection(
    typeof(Func<IHandleDomainEvent<>>),
    handlerTypes.Select(t => typeof(Func<>).MakeGenericType(t)));

But this is almost never required—Simple Injector's native support should cover most scenarios.

内容的提问来源于stack exchange,提问作者Rodrigo Riskalla Leal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 02:22:40