private void SetupEventPipeline(EventPipelineBuilder builder) { builder .AddMiddleware(new Events.PipelineMiddlewares.EventHandlerLocatorMiddleware( typeof(EventsTests).GetTypeInfo().Assembly)) .AddMiddleware(new Events.PipelineMiddlewares.EventHandlerResolverMiddleware()) .AddMiddleware(new Events.PipelineMiddlewares.EventHandlerExecutorMiddleware { UseParametersResolve = true }); }
/* * This example will hold all domain events until your transaction have been completed. * That's great since failing transactions won't result in events for stuff that hasn't been persisted. * * To keep the example simple we'll use a fake transaction. * */ static void Main(string[] args) { // configure Griffin.Container var container = ConfigureGriffinContainer(); var dbContext = new FakeDbContext(); // Use a synchronous IoC dispatcher var builder = new EventPipelineBuilder(new ErrorHandler()); var dispatcher = builder .WaitOnTransactions(dbContext) //do not release events if a transaction is active .UseGriffinContainer(container) //use Griffin.Container .Build(); // Build the pipeline dispatcher Console.WriteLine("Step 1. A simple publising"); Console.WriteLine("============================="); DomainEvent.Assign(dispatcher); // Will be published directly DomainEvent.Publish(new UserRegistered("Arne")); Console.WriteLine(); Console.WriteLine("Step 1. Disposed Uow = no events"); Console.WriteLine("============================="); using (var uow = dbContext.CreateUnitOfWork()) { Console.WriteLine("** Nothing should come between this line"); DomainEvent.Publish(new UserRegistered("Arne")); Console.WriteLine("** ...and this line..."); //no save = rollback } Console.WriteLine("* this should come directly after 'And this line...'"); Console.WriteLine(); Console.WriteLine("Step 3. Uow = dispatch after"); Console.WriteLine("============================="); using (var uow = dbContext.CreateUnitOfWork()) { Console.WriteLine("** Nothing should come between this line"); DomainEvent.Publish(new UserRegistered("Arne")); Console.WriteLine("** ...and this line..."); uow.SaveChanges(); } Console.WriteLine("** Two domain messages should have come."); // and wait Console.ReadLine(); }
/// <summary> /// Use Griffin.Container to dispatch events /// </summary> /// <param name="builder">this</param> /// <param name="griffinContainer">Your created container, look at the Griffin.Container HP for instructions on how to build it.</param> /// <returns>this</returns> public static EventPipelineBuilder UseGriffinContainer(this EventPipelineBuilder builder, IParentContainer griffinContainer) { if (builder == null) { throw new ArgumentNullException("builder"); } if (griffinContainer == null) { throw new ArgumentNullException("griffinContainer"); } builder.UseContainer(new ContainerAdapter(griffinContainer)); return(builder); }