Esempio n. 1
0
        public void ConvertToBytes(object id)
        {
            if (id is string strId && string.IsNullOrEmpty(strId))
            {
                id = Guid.NewGuid();
            }

            var eventName  = "TestName";
            var baseBytes  = Encoding.UTF8.GetBytes(eventName);
            var eventBytes = Encoding.UTF8.GetBytes(eventName);
            var eventUnit  = new EventTransUnit(eventName, id, baseBytes, eventBytes);

            using var array = eventUnit.ConvertToBytes();
            var bytes     = array.ToArray();
            var tryResult = EventConverter.TryParse(bytes, out var bitUnit);

            Assert.True(tryResult);
            Assert.Equal(id, bitUnit.ActorId);
            Assert.Equal(eventName, bitUnit.EventName);
            Assert.Equal(eventName, Encoding.UTF8.GetString(bitUnit.MetaBytes));
            Assert.Equal(eventName, Encoding.UTF8.GetString(bitUnit.EventBytes));
            tryResult = EventConverter.TryParseWithNoId(bytes, out bitUnit);
            Assert.True(tryResult);
            Assert.Null(bitUnit.ActorId);
            Assert.Equal(eventName, bitUnit.EventName);
            Assert.Equal(eventName, Encoding.UTF8.GetString(bitUnit.MetaBytes));
            Assert.Equal(eventName, Encoding.UTF8.GetString(bitUnit.EventBytes));
            tryResult = EventConverter.TryParseActorId(bytes, out var actorId);
            Assert.True(tryResult);
            Assert.Equal(id, actorId);
        }
Esempio n. 2
0
        public Task EventHandler(Type observerType, List <BytesBox> list)
        {
            var groups = list.Select(bytes =>
            {
                var success = EventConverter.TryParseActorId(bytes.Value, out var actorId);
                if (!success)
                {
                    if (this.Logger.IsEnabled(LogLevel.Error))
                    {
                        this.Logger.LogError($"{nameof(EventConverter.TryParseActorId)} failed");
                    }
                }

                return(success, actorId, bytes);
            }).Where(o => o.success).GroupBy(o => o.actorId);

            return(Task.WhenAll(groups.Select(async kv =>
            {
                var items = kv.Select(item => item.bytes.Value).ToList();
                switch (kv.Key)
                {
                case long id: await this.GetObserver(observerType, id).OnNext(new Immutable <List <byte[]> >(items)); break;

                case string id: await this.GetObserver(observerType, id).OnNext(new Immutable <List <byte[]> >(items)); break;

                case Guid id: await this.GetObserver(observerType, id).OnNext(new Immutable <List <byte[]> >(items)); break;

                default: break;
                }
                foreach (var(_, _, bytes) in kv)
                {
                    bytes.Success = true;
                }
            })));
        }
Esempio n. 3
0
        public void Upgrade_theEvent_null()
        {
            var converter = new EventConverter(_typeResolver);
            var ex        = Assert.Throws <ArgumentNullException>(() => converter.Upgrade(null));

            ex.ParamName.Should().Be("theEvent");
        }
Esempio n. 4
0
        public void AddConverter_ByName_converter_null()
        {
            var converter = new EventConverter(_typeResolver);
            var ex        = Assert.Throws <ArgumentNullException>(() => converter.AddConverter(EventName, null));

            ex.ParamName.Should().Be("converter");
        }
Esempio n. 5
0
        public void AddConverter_ByType_eventType_null()
        {
            var converter = new EventConverter(_typeResolver);
            var ex        = Assert.Throws <ArgumentNullException>(() => converter.AddConverter((Type)null, _childConverter));

            ex.ParamName.Should().Be("eventType");
        }
Esempio n. 6
0
        public void AddConverter_duplicate_name()
        {
            var converter = new EventConverter(_typeResolver);

            converter.AddConverter(EventName, _childConverter);

            var ex = Assert.Throws <ArgumentException>(() => converter.AddConverter(EventName, _childConverter));

            ex.ParamName.Should().Be("eventName");
            ex.Message.Should().StartWith("There is already a converter for event 'bob'.");
        }
Esempio n. 7
0
        public void AddConverter_ByType()
        {
            var anEvent   = CreateEvent();
            var converter = new EventConverter(_typeResolver);

            converter.AddConverter(typeof(AnEvent), _childConverter);

            converter.Upgrade(anEvent);

            _childConverter.AssertWasCalled(x => x.Upgrade(anEvent));
        }
Esempio n. 8
0
        public List <EventModel> getEventsList()
        {
            List <tbl_event>  eventList = new PostsProvider().getEventList();
            List <EventModel> modelList = new List <EventModel>();

            foreach (var item in eventList)
            {
                modelList.Add(EventConverter.entityToModel(item));
            }
            return(modelList);
        }
Esempio n. 9
0
 public EventApi(
     string host,
     IHttpClientWrapper httpClientWrapper,
     EventConverter eventConverter,
     int batchSize)
 {
     _host = host;
     _httpClientWrapper = httpClientWrapper;
     _eventConverter    = eventConverter;
     _batchSize         = batchSize;
 }
Esempio n. 10
0
        private byte[] ConvertToBytes(EventDocumentDto document, long id)
        {
            var meta = new EventMeta {
                Version = document.Version, Timestamp = document.Timestamp, FlowId = document.FlowId
            };

            using var baseBytes = EventExtensions.ConvertToBytes(meta);
            var transUnit = new EventTransUnit(document.Name, id, baseBytes.AsSpan(), Encoding.UTF8.GetBytes(document.Data));

            using var buffer = EventConverter.ConvertToBytes(transUnit);
            return(buffer.ToArray());
        }
        public Event[] GetTopLevelEvents()
        {
            ListTopLevelEventsResponse response = _proxy.ListTopLevelEvents(new ListTopLevelEventsRequest());

            if (response.ReturnStatus.Code != 0)
            {
                throw new Exception(response.ReturnStatus.Description);
            }


            Event[] topLvlEvents = EventConverter.ConvertEventClassifierTypeCollection(response.EventClassifiers);
            return(topLvlEvents);
        }
Esempio n. 12
0
        private static IEventStore InitializeEventStore()
        {
            var typeResolver = new AttributeEventTypeResolver();

            typeResolver.AddAllEventsInAssembly(typeof(Program).Assembly);

            var converter = new EventConverter(typeResolver);

            converter.AddConverter(typeof(NameChangedEvent), new NameChangedEventPostConverter());
            converter.AddConverter(typeof(PersonCreatedEvent), new PersonCreatedEventPostConverter());

            var eventStore = new MsSqlServerEventStore(ConfigurationManager.ConnectionStrings["EventStore"].ConnectionString, typeResolver, converter);

            return(eventStore);
        }
 public override object CreateFromValue(ServiceProviderContext serviceContext, XamlValueConverter <TypeConverter> ts, object value, XamlMember property)
 {
     if (ts == BuiltInValueConverter.Event)
     {
         string methodName = value as string;
         if (methodName != null)
         {
             object obj2;
             Type   type;
             EventConverter.GetRootObjectAndDelegateType(serviceContext, out obj2, out type);
             return(this.CreateDelegate(type, obj2, methodName));
         }
     }
     return(base.CreateFromValue(serviceContext, ts, value, property));
 }
Esempio n. 14
0
        public void Upgrade()
        {
            var event1       = CreateEvent("foo");
            var event2       = CreateEvent("bar");
            var fooConverter = MockRepository.GenerateStub <IEventConverter>();
            var barConverter = MockRepository.GenerateStub <IEventConverter>();

            var converter = new EventConverter(_typeResolver);

            converter.AddConverter("foo", fooConverter);
            converter.AddConverter("bar", barConverter);

            converter.Upgrade(event1);
            converter.Upgrade(event2);

            fooConverter.AssertWasCalled(x => x.Upgrade(event1));
            barConverter.AssertWasCalled(x => x.Upgrade(event2));
        }
Esempio n. 15
0
        //CreateFromValue is expected to convert the provided value via any applicable converter (on property or type) or provide the original value if there is no converter
        public override object CreateFromValue(
            ServiceProviderContext serviceContext,
            XamlValueConverter <TypeConverter> ts, object value,
            XamlMember property)
        {
            if (ts == BuiltInValueConverter.Event)
            {
                string valueString = value as string;
                if (valueString != null)
                {
                    object rootObject;
                    Type   delegateType;
                    EventConverter.GetRootObjectAndDelegateType(serviceContext, out rootObject, out delegateType);

                    return(CreateDelegate(delegateType, rootObject, valueString));
                }
            }
            return(base.CreateFromValue(serviceContext, ts, value, property));
        }
        public Event GetEventSubTreeNoSelections(long eventId)
        {
            GetEventSubTreeNoSelectionsRequest request = new GetEventSubTreeNoSelectionsRequest();

            request.EventClassifierIds = new long[] { eventId };


            GetEventSubTreeNoSelectionsResponse response = _proxy.GetEventSubTreeNoSelections(request);

            if (response.ReturnStatus.Code != 0)
            {
                throw new Exception(response.ReturnStatus.Description);
            }


            // Since only receive one eventId as param then can assume will always be just one event returned.
            Event[] eventsTreeToReturn = EventConverter.ConvertEventClassifierTypeCollection(response.EventClassifiers);
            return(eventsTreeToReturn[0]);
        }
Esempio n. 17
0
        public EventBufferUnit(EventUnit <TPrimaryKey> eventUnit, string flowId, IEventTypeContainer eventTypeContainer, ISerializer serializer)
        {
            var evtType = eventUnit.Event.GetType();

            if (!eventTypeContainer.TryGet(evtType, out this.eventName))
            {
                throw new NoNullAllowedException($"event name of {evtType.FullName}");
            }

            this.eventBaseArray  = eventUnit.Meta.ConvertToBytes();
            this.EventBytes      = serializer.SerializeToUtf8Bytes(eventUnit.Event, evtType);
            this.eventTransArray = EventConverter.ConvertToBytes(new EventTransUnit(this.eventName, eventUnit.ActorId, this.eventBaseArray.AsSpan(), this.EventBytes));
            this.EventUnit       = eventUnit;
            this.Document        = new EventDocument <TPrimaryKey>
            {
                ActorId   = eventUnit.ActorId,
                Data      = Encoding.UTF8.GetString(this.EventBytes),
                FlowId    = flowId,
                Name      = this.eventName,
                Version   = eventUnit.Meta.Version,
                Timestamp = eventUnit.Meta.Timestamp
            };
        }
Esempio n. 18
0
        public async Task EventHandler(Type observerType, BytesBox bytes)
        {
            if (EventConverter.TryParseActorId(bytes.Value, out var actorId))
            {
                switch (actorId)
                {
                case long id: await this.GetObserver(observerType, id).OnNext(new Immutable <byte[]>(bytes.Value)); break;

                case string id: await this.GetObserver(observerType, id).OnNext(new Immutable <byte[]>(bytes.Value)); break;

                case Guid id: await this.GetObserver(observerType, id).OnNext(new Immutable <byte[]>(bytes.Value)); break;

                default: break;
                }
                bytes.Success = true;
            }
            else
            {
                if (this.Logger.IsEnabled(LogLevel.Error))
                {
                    this.Logger.LogError($"{nameof(EventConverter.TryParseActorId)} failed");
                }
            }
        }
Esempio n. 19
0
 public EventBus(IEventStoreConnection connection, EventConverter converter)
 {
     _connection = connection;
     _converter  = converter;
 }
Esempio n. 20
0
        public void Upgrade_unknown_event()
        {
            var converter = new EventConverter(_typeResolver);

            Assert.DoesNotThrow(() => converter.Upgrade(CreateEvent()));
        }
Esempio n. 21
0
 public EventStore(IEventStoreConnection connection, EventConverter converter, IEventBus eventBus)
 {
     _connection = connection;
     _converter  = converter;
     _eventBus   = eventBus;
 }
Esempio n. 22
0
        public EventModel GetEvent(int id)
        {
            Event eventDB = _iEventDao.GetEvent(id);

            return(EventConverter.ConvertToModel(eventDB));
        }
Esempio n. 23
0
        public void Upgrade_unknown_event()
        {
            var converter = new EventConverter(_typeResolver);

            converter.Upgrade(CreateEvent());
        }
Esempio n. 24
0
        public List <EventModel> GetAllEvents()
        {
            List <Event> listEvents = _iEventDao.GetAllEvents();

            return(new List <EventModel>(listEvents.Select(x => EventConverter.ConvertToModel(x))));
        }
Esempio n. 25
0
 public void Ctor()
 {
     var converter = new EventConverter(_typeResolver);
 }
Esempio n. 26
0
        public List <EventModel> GetListEventsByCategoryIdAndByCountry(int categoryId, int countryId)
        {
            List <Event> listEventDB = _iEventDao.GetListEventsByCategoryIdAndByCountry(categoryId, countryId);

            return(listEventDB.Select(x => EventConverter.ConvertToModel(x)).ToList());
        }
Esempio n. 27
0
        public List <RevenueModel> GetAllRevenues()
        {
            List <UserEventHistory> listEventDB = _iUserEventHistoryDao.GetAllRevenues();

            return(listEventDB.Select(x => EventConverter.ConvertToRevenueModel(x)).ToList());
        }
Esempio n. 28
0
        protected virtual async Task <bool> RaiseEvent(IEvent @event, string flowId = null)
        {
            if (string.IsNullOrEmpty(flowId))
            {
                flowId = RequestContext.Get(RuntimeConsts.EventFlowIdKey) as string;
                if (string.IsNullOrEmpty(flowId))
                {
                    throw new ArgumentNullException(nameof(flowId));
                }
            }
            try
            {
                var eventBox = new EventUnit <TPrimaryKey>
                {
                    Event = @event,
                    Meta  = new EventMeta
                    {
                        FlowId    = flowId,
                        Version   = this.Snapshot.Meta.Version + 1,
                        Timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds()
                    },
                    ActorId = this.Snapshot.Meta.ActorId
                };

                await this.OnRaiseStart(eventBox);

                this.Snapshot.Meta.IncrementDoingVersion(this.ActorType); // Mark the Version to be processed
                var evtType = @event.GetType();

                if (!this.EventTypeContainer.TryGet(evtType, out var eventName))
                {
                    throw new NoNullAllowedException($"event name of {evtType.FullName}");
                }

                var evtBytes     = this.Serializer.SerializeToUtf8Bytes(@event, evtType);
                var appendResult = await this.EventStorage.Append(new EventDocument <TPrimaryKey>
                {
                    FlowId    = eventBox.Meta.FlowId,
                    ActorId   = this.ActorId,
                    Data      = Encoding.UTF8.GetString(evtBytes),
                    Name      = eventName,
                    Timestamp = eventBox.Meta.Timestamp,
                    Version   = eventBox.Meta.Version
                });

                if (appendResult)
                {
                    this.SnapshotHandler.Apply(this.Snapshot, eventBox);
                    this.Snapshot.Meta.UpdateVersion(eventBox.Meta, this.ActorType); // Version of the update process
                    await this.OnRaiseSuccess(eventBox, evtBytes);

                    await this.SaveSnapshotAsync();

                    using var baseBytes = eventBox.Meta.ConvertToBytes();
                    using var buffer    = EventConverter.ConvertToBytes(new EventTransUnit(eventName, this.Snapshot.Meta.ActorId, baseBytes.AsSpan(), evtBytes));
                    if (this.EventStream != default)
                    {
                        await this.EventStream.Next(buffer.ToArray());
                    }

                    if (this.Logger.IsEnabled(LogLevel.Trace))
                    {
                        this.Logger.LogTrace("RaiseEvent completed: {0}->{1}->{2}", this.ActorType.FullName, this.Serializer.Serialize(eventBox), this.Serializer.Serialize(this.Snapshot));
                    }

                    return(true);
                }
                else
                {
                    if (this.Logger.IsEnabled(LogLevel.Trace))
                    {
                        this.Logger.LogTrace("RaiseEvent failed: {0}->{1}->{2}", this.ActorType.FullName, this.Serializer.Serialize(eventBox), this.Serializer.Serialize(this.Snapshot));
                    }

                    this.Snapshot.Meta.DecrementDoingVersion(); // Restore the doing version
                    await this.OnRaiseFailed(eventBox);

                    return(false);
                }
            }
            catch (Exception ex)
            {
                this.Logger.LogCritical(ex, "RaiseEvent failed: {0}->{1}", this.ActorType.FullName, this.Serializer.Serialize(this.Snapshot));
                await this.RecoverySnapshot(); // Restore state

                // Errors may appear repeatedly, so update the previous snapshot to improve the restoration speed
                await this.SaveSnapshotAsync(true);

                throw;
            }
        }