Thursday, June 7, 2018

MediatR and StructureMap–Using Generic NotificationHandlers.

One of our application is using a CQRS approach in combination with MediatR and StructureMap. Part of the application is rather straightforward and to avoid a lot of boilerplate code we would like to use a NotificationHandler that could handle generic events. Here is the code we had written:

However when we tried to register this using StructureMap nothing happened resulting in no event that was picked up by the NotificationHandler. Here is the registration code we had:

The problem is that it is one level of generic too deep to handle by StructureMap, so StructureMap has no clue on how to close this open generic. To solve this problem we can create our own registry convention:

As a last step we have to apply this convention when configuring StructureMap:

No comments: