Usando RegisterInitializer para conectar manipuladores de eventos
Eu tenho um serviço WCF que usa o Simple Injector para injeção de dependência. Quero conectar alguns manipuladores de eventos no bootstrapper de contêiner. Eu criei uma interfaceIStatusChangeNotification
:
public interface IStatusChangeNotification
{
event EventHandler<int> JobStatusChange;
}
MinhasCommandHandler
implementaIStatusChangeNotification
e existem duas classes de manipulador de eventosEmailNotification
eMmrNotification
, cada um definindo umNotify()
método. Então, no meu código de inicialização, tenho o seguinte:
container.Register<EmailNotification>();
container.Register<MmrNotification>();
container.RegisterManyForOpenGeneric(typeof(ICommandHandler<>),
Assembly.GetExecutingAssembly());
container.RegisterInitializer<IStatusChangeNotification>(scn =>
{
scn.JobStatusChange += container.GetInstance<EmailNotification>().Notify;
scn.JobStatusChange += container.GetInstance<MmrNotification>().Notify;
});
Isso funciona e as notificações são recebidas. Minha pergunta é se esta é a abordagem correta / melhor para conectar manipuladores de eventos? Como removo os manipuladores no final da solicitação e a falha em removê-los resulta em um vazamento de memória?