public void SaveAndLoadWithEntityRepository() { Converts.Repository .AddJsonKey() .AddJsonEnumSearchHandler() .AddJsonPrimitivesSearchHandler() .AddJsonObjectSearchHandler(); ICompositeTypeProvider compositeTypeProvider = new ReflectionCompositeTypeProvider(new ReflectionCompositeDelegateFactory()); IFactory <ICompositeStorage> storageFactory = new GetterFactory <ICompositeStorage>(() => new JsonCompositeStorage()); EventSourcingContext context = new EventSourcingContext(@"Data Source=.\sqlexpress; Initial Catalog=EventStore;Integrated Security=SSPI"); EntityEventStore eventStore = new EntityEventStore(context); PersistentEventDispatcher eventDispatcher = new PersistentEventDispatcher(eventStore); AggregateRootRepository <Order> repository = new AggregateRootRepository <Order>( eventStore, new CompositeEventFormatter(compositeTypeProvider, storageFactory), new ReflectionAggregateRootFactory <Order>(), eventDispatcher, new NoSnapshotProvider(), new EmptySnapshotStore() ); PersistentCommandDispatcher commandDispatcher = new PersistentCommandDispatcher( new SerialCommandDistributor(), new EntityCommandStore(context), new CompositeCommandFormatter(compositeTypeProvider, storageFactory) ); CreateOrderHandler createHandler = new CreateOrderHandler(repository); AddOrderItemHandler addItemHandler = new AddOrderItemHandler(repository); commandDispatcher.Handlers .Add <CreateOrder>(createHandler) .Add <AddOrderItem>(addItemHandler); CreateOrder create = new CreateOrder(); commandDispatcher.HandleAsync(create); eventDispatcher.Handlers.Await <OrderPlaced>().Wait(); IEnumerable <EventModel> serializedEvents = eventStore.Get(create.OrderKey).ToList(); Assert.AreEqual(1, serializedEvents.Count()); AddOrderItem addItem = new AddOrderItem(create.OrderKey, GuidKey.Create(Guid.NewGuid(), "Product"), 5); commandDispatcher.HandleAsync(Envelope.Create(addItem).AddDelay(TimeSpan.FromMinutes(1))); Task <OrderTotalRecalculated> task = eventDispatcher.Handlers.Await <OrderTotalRecalculated>(); task.Wait(); Console.WriteLine(task.Result); serializedEvents = eventStore.Get(create.OrderKey).ToList(); Assert.AreEqual(4, serializedEvents.Count()); }
private static ValueGetter <VBuffer <TDst> > GetVecGetterAsCore <TSrc, TDst>(VectorType typeSrc, PrimitiveDataViewType typeDst, GetterFactory getterFact) { Contracts.Assert(typeof(TSrc) == typeSrc.ItemType.RawType); Contracts.Assert(typeof(TDst) == typeDst.RawType); Contracts.AssertValue(getterFact); var getter = getterFact.GetGetter <VBuffer <TSrc> >(); bool identity; var conv = Conversions.Instance.GetStandardConversion <TSrc, TDst>(typeSrc.ItemType, typeDst, out identity); if (identity) { Contracts.Assert(typeof(TSrc) == typeof(TDst)); return((ValueGetter <VBuffer <TDst> >)(Delegate) getter); } int size = typeSrc.Size; var src = default(VBuffer <TSrc>); return((ref VBuffer <TDst> dst) => { getter(ref src); if (size > 0) { Contracts.Check(src.Length == size); } var srcValues = src.GetValues(); int count = srcValues.Length; var editor = VBufferEditor.Create(ref dst, src.Length, count); if (count > 0) { // REVIEW: This would be faster if there were loops for each std conversion. // Consider adding those to the Conversions class. for (int i = 0; i < count; i++) { conv(in srcValues[i], ref editor.Values[i]); } if (!src.IsDense) { var srcIndices = src.GetIndices(); srcIndices.CopyTo(editor.Indices); } } dst = editor.Commit(); }); }
internal static ValueGetter <VBuffer <TDst> > GetVecGetterAs <TDst>(PrimitiveDataViewType typeDst, SlotCursor cursor) { Contracts.CheckValue(typeDst, nameof(typeDst)); Contracts.CheckParam(typeDst.RawType == typeof(TDst), nameof(typeDst)); var typeSrc = cursor.GetSlotType(); Func <VectorType, PrimitiveDataViewType, GetterFactory, ValueGetter <VBuffer <TDst> > > del = GetVecGetterAsCore <int, TDst>; var methodInfo = del.GetMethodInfo().GetGenericMethodDefinition().MakeGenericMethod(typeSrc.ItemType.RawType, typeof(TDst)); return((ValueGetter <VBuffer <TDst> >)methodInfo.Invoke(null, new object[] { typeSrc, typeDst, GetterFactory.Create(cursor) })); }
/// <summary> /// Given the item type, typeDst, a row, and column index, return a ValueGetter{VBuffer{TDst}} for the /// vector-valued column with a conversion to a vector of typeDst, if needed. /// </summary> public static ValueGetter <VBuffer <TDst> > GetVecGetterAs <TDst>(PrimitiveDataViewType typeDst, DataViewRow row, int col) { Contracts.CheckValue(typeDst, nameof(typeDst)); Contracts.CheckParam(typeDst.RawType == typeof(TDst), nameof(typeDst)); Contracts.CheckValue(row, nameof(row)); Contracts.CheckParam(0 <= col && col < row.Schema.Count, nameof(col)); Contracts.CheckParam(row.IsColumnActive(col), nameof(col), "column was not active"); var typeSrc = row.Schema[col].Type as VectorType; Contracts.Check(typeSrc != null, "Source column type must be vector"); Func <VectorType, PrimitiveDataViewType, GetterFactory, ValueGetter <VBuffer <TDst> > > del = GetVecGetterAsCore <int, TDst>; var methodInfo = del.GetMethodInfo().GetGenericMethodDefinition().MakeGenericMethod(typeSrc.ItemType.RawType, typeof(TDst)); return((ValueGetter <VBuffer <TDst> >)methodInfo.Invoke(null, new object[] { typeSrc, typeDst, GetterFactory.Create(row, col) })); }
/// <summary> /// Given the item type, typeDst, a row, and column index, return a ValueGetter for the vector-valued /// column with a conversion to a vector of typeDst, if needed. This is the weakly typed version of /// <see cref="GetVecGetterAs{TDst}(PrimitiveType, IRow, int)"/>. /// </summary> public static Delegate GetVecGetterAs(PrimitiveType typeDst, IRow row, int col) { Contracts.CheckValue(typeDst, nameof(typeDst)); Contracts.CheckValue(row, nameof(row)); Contracts.CheckParam(0 <= col && col < row.Schema.ColumnCount, nameof(col)); Contracts.CheckParam(row.IsColumnActive(col), nameof(col), "column was not active"); var typeSrc = row.Schema.GetColumnType(col); Contracts.Check(typeSrc.IsVector, "Source column type must be vector"); Func <VectorType, PrimitiveType, GetterFactory, ValueGetter <VBuffer <int> > > del = GetVecGetterAsCore <int, int>; var methodInfo = del.GetMethodInfo().GetGenericMethodDefinition().MakeGenericMethod(typeSrc.ItemType.RawType, typeDst.RawType); return((Delegate)methodInfo.Invoke(null, new object[] { typeSrc, typeDst, GetterFactory.Create(row, col) })); }
private static ValueGetter <VBuffer <TDst> > GetVecGetterAsCore <TSrc, TDst>(VectorType typeSrc, PrimitiveType typeDst, GetterFactory getterFact) { Contracts.Assert(typeof(TSrc) == typeSrc.ItemType.RawType); Contracts.Assert(typeof(TDst) == typeDst.RawType); Contracts.AssertValue(getterFact); var getter = getterFact.GetGetter <VBuffer <TSrc> >(); bool identity; var conv = Conversions.Instance.GetStandardConversion <TSrc, TDst>(typeSrc.ItemType, typeDst, out identity); if (identity) { Contracts.Assert(typeof(TSrc) == typeof(TDst)); return((ValueGetter <VBuffer <TDst> >)(Delegate) getter); } int size = typeSrc.VectorSize; var src = default(VBuffer <TSrc>); return((ref VBuffer <TDst> dst) => { getter(ref src); if (size > 0) { Contracts.Check(src.Length == size); } var values = dst.Values; var indices = dst.Indices; int count = src.Count; if (count > 0) { if (Utils.Size(values) < count) { values = new TDst[count]; } // REVIEW: This would be faster if there were loops for each std conversion. // Consider adding those to the Conversions class. for (int i = 0; i < count; i++) { conv(ref src.Values[i], ref values[i]); } if (!src.IsDense) { if (Utils.Size(indices) < count) { indices = new int[count]; } Array.Copy(src.Indices, indices, count); } } dst = new VBuffer <TDst>(src.Length, count, values, indices); }); }