private static void AppendToStream(UserCredentials credentials, IEventStoreConnection connection, UnitOfWork unitOfWork) { var affected = unitOfWork.GetChanges().Single(); connection.AppendToStream( affected.Identifier, affected.ExpectedVersion, affected.Root.GetChanges(). Select(_ => new EventData( Guid.NewGuid(), _.GetType().Name, true, ToJsonByteArray(_), new byte[0])), credentials); }
public void Show() { //Somewhere in an application service wrapper var unitOfWork = new UnitOfWork(); //Dependency of a domain service or application service var dogRepository = new Repository<Dog>(Dog.Factory, unitOfWork, new EmptyEventStreamReader()); //Application service handler code var dog = new Dog(Guid.NewGuid(), "Sparky", DateTime.Today.AddYears(-1)); dog.AdministerShotOf("Anti Diarrhea Medicine", DateTime.Today); dogRepository.Add(dog.DogId.ToString(), dog); //Back in the application service wrapper Console.WriteLine("We observed that:"); foreach (var change in unitOfWork.GetChanges().SelectMany(aggregate => aggregate.Root.GetChanges())) { Console.WriteLine(change); } }
static void Main() { //Make sure you start an instance of EventStore before running this!! var credentials = new UserCredentials("admin", "changeit"); var connection = EventStoreConnection.Create( ConnectionSettings.Create(). UseConsoleLogger(). SetDefaultUserCredentials( credentials), new IPEndPoint(IPAddress.Loopback, 1113), "EventStoreShopping"); connection.Connect(); var unitOfWork = new UnitOfWork(); var repository = new Repository<ShoppingCart>( ShoppingCart.Factory, unitOfWork, connection, new EventReaderConfiguration( new SliceSize(512), new JsonDeserializer(), new PassThroughStreamNameResolver(), new FixedStreamUserCredentialsResolver(credentials))); //Handle Start Shopping command var id = new ShoppingCartId("shopping_at_boutique_" + DateTime.UtcNow.Ticks); repository.Add(id, new ShoppingCart(id)); //Handle Add Item command var cart1 = repository.Get(id); cart1.AddItem(new ItemId("pair_of_socks"), 2); //Handle Add Item command var cart2 = repository.Get(id); cart2.AddItem(new ItemId("pair_of_shirts"), 4); //Handle Remove Item command var cart3 = repository.Get(id); cart3.RemoveItem(new ItemId("pair_of_socks")); //Handle Increment Item Count command var cart4 = repository.Get(id); cart4.IncrementItemCount(new ItemId("pair_of_shirts")); //Handle Checkout command var cart5 = repository.Get(id); cart5.Checkout(); //Append to stream var affected = unitOfWork.GetChanges().Single(); connection.AppendToStream( affected.Identifier, affected.ExpectedVersion, affected.Root.GetChanges(). Select(_ => new EventData( Guid.NewGuid(), _.GetType().Name, true, ToJsonByteArray(_), new byte[0])), credentials); }
public void Handle(UnitOfWork unitOfWork) { var affected = unitOfWork.GetChanges().Single(); Write(new[] {affected}); }
static void Main(string[] args) { string usersFilePath = null; var options = new OptionSet { {"i|input:", "Path to file that contains names and SIDs of users to be imported.", value => usersFilePath = value} }; try { options.Parse(args); if (usersFilePath != null) { var watch = Stopwatch.StartNew(); var identity = WindowsIdentity.GetCurrent(); Log.Info("Importing {0} at {1} as {2}.", usersFilePath, XmlConvert.ToString(DateTime.Now, XmlDateTimeSerializationMode.Local), identity != null ? identity.Name : "<unknown>"); using (var connection = EventStoreConnection.Create()) { connection.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1113)); using (var file = File.OpenText(usersFilePath)) { string row; while ((row = file.ReadLine()) != null) { Record record; if (Record.TryParse(row, out record)) { var unitOfWork = new UnitOfWork(); IRepository<User> userRepository = new Repository<User>(User.Factory, unitOfWork, connection); IRepository<Role> roleRepository = new Repository<Role>(Role.Factory, unitOfWork, connection); var service = new UserApplicationService(userRepository, roleRepository); var command = record.ToCommand(); Console.WriteLine("Start handling command: {0} - {1}", command.GetType().Name, command.Identifier); service.Handle(command); if (unitOfWork.HasChanges()) { const int sliceEventCount = 500; var aggregate = unitOfWork.GetChanges().Single(); var eventIndex = 0; var slices = from eventData in aggregate.Root. GetChanges(). Select(change => new EventData( Guid.NewGuid(), change.GetType().AssemblyQualifiedName, false, SerializeEvent(change), new byte[0])) group eventData by eventIndex++%sliceEventCount into slice select slice.AsEnumerable(); using (var transaction = connection.StartTransaction( aggregate.Identifier, aggregate.ExpectedVersion)) { foreach (var slice in slices) { transaction.Write(slice); } transaction.Commit(); Console.WriteLine("Committed events for command"); } } } else { Log.Error("Could not import the following row. Please make sure it's well formed: {0}", row); } } } } Log.Info("Completed import at {0} (took {1}ms).", XmlConvert.ToString(DateTime.Now, XmlDateTimeSerializationMode.Local), watch.ElapsedMilliseconds); } else { options.WriteOptionDescriptions(Console.Out); } } catch (OptionException exception) { Console.WriteLine(exception.Message); } }