Using Autofac with Domain Events

后端 未结 2 1493
粉色の甜心
粉色の甜心 2021-01-31 00:17

I\'m trying to introduce domain events into a project. The concept is described in Udi Dahan\'s post - http://www.udidahan.com/2009/06/14/domain-events-salvation/

Here\'

相关标签:
2条回答
  • 2021-01-31 00:42

    As Peter pointed out, the registration problem was with your Where() clause.

    When scanning assemblies Autofac filters components automatically based on the services you specify, so it would be equally correct to just use:

    builder.RegisterAssemblyTypes(asm)
        .AsClosedTypesOf(handlerType)
        .InstancePerLifetimeScope();
    
    0 讨论(0)
  • 2021-01-31 01:06

    The problem in your assembly scanning code is when you use IsAssignableFrom. The filter will ask: "could an instance of SendWebQuestionToCSO be assigned to a variable of IHandleDomainEvents<>?" The answer is obviously "no" since you can never have a variable of an open generic type.

    The trick would be to inspect the interfaces implemented by each type and check whether any of them are closing the open generic interface type. Here's a revised scanner:

        var asm = Assembly.GetExecutingAssembly();
        var handlerType = typeof(IHandleDomainEvents<>);
    
        builder.RegisterAssemblyTypes(asm)
            .Where(t => t.GetInterfaces().Any(t => t.IsClosedTypeOf(handlerType)))
            .AsImplementedInterfaces()
            .InstancePerLifetimeScope(); 
    
    0 讨论(0)
提交回复
热议问题