public void NoDispatcher() { DomainEvent.Assign(null); var domainEvent = Substitute.For <IDomainEvent>(); Assert.Throws <InvalidOperationException>(() => DomainEvent.Publish(domainEvent)); }
/// <summary> /// Dispatch commands using our favorite container /// </summary> /// <param name="container">Container instance.</param> public static void DispatchEvents(this IParentContainer container) { if (container == null) { throw new ArgumentNullException("container"); } DomainEvent.Assign(new DomainEvents.IocDispatcher(new ContainerAdapter(container))); }
public static void UseMe() { if (!IsAssigned) { DomainEvent.Assign(new TestDomainEvent()); } IsAssigned = true; Events.Clear(); }
public void RegularDispatch() { var innerDispatcher = Substitute.For <IDomainEventDispatcher>(); DomainEvent.Assign(innerDispatcher); var domainEvent = Substitute.For <IDomainEvent>(); DomainEvent.Publish(domainEvent); innerDispatcher.Received().Dispatch(domainEvent); }
/* * 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(); }