public override Flow <EventStreamElement <TE>, Done, NotUsed> Handle()
        {
            return(Flow.FromFunction(
                       new Func <EventStreamElement <TE>, Done>(
                           element =>
            {
                if (EventHandlers.TryGetValue(element.Event.GetType(), out var dbAction))
                {
                    DbActionExecutor(element, dbAction);
                }
                else
                {
                    // TODO: log the unhandled event
                }

                using (var con = ReadSideConnectionFactory.Invoke())
                {
                    con.Execute(@"
                                update read_side_offsets
                                set sequence_offset = @offset
                                where read_side_id = @readSideId
                                and tag = @tag",
                                new
                    {
                        offset = (element.Offset as Sequence).Value,
                        readSideId = ReadSideId,
                        tag = ((element.Event as AggregateEvent <TE>).AggregateTag as AggregateEventTag).Tag
                    });
                }

                return Done.Instance;
            }
                           )
                       ));
        }
 public override Task <Done> GlobalPrepare()
 {
     using (var con = ReadSideConnectionFactory.Invoke())
     {
         con.Execute(@"
     if not exists (select * from sysobjects where name='read_side_offsets' and xtype='U')
         CREATE TABLE read_side_offsets (read_side_id VARCHAR(255), tag VARCHAR(255),sequence_offset bigint, time_uuid_offset char(36),PRIMARY KEY (read_side_id, tag))
     ");
         GlobalPrepareCallback(con);
     }
     return(base.GlobalPrepare());
 }
 public virtual void DbActionExecutor(EventStreamElement <TE> element, Action <SqlConnection, EventStreamElement <TE> > action)
 {
     try
     {
         using (var con = ReadSideConnectionFactory.Invoke())
             action(con, element);
     }
     catch (Exception ex)
     {
         throw;
     }
 }
 public override Task <Offset> Prepare(AggregateEventTag tag)
 {
     using (var con = ReadSideConnectionFactory.Invoke())
     {
         var offset = con.QueryFirstOrDefault <long>(@"
         select sequence_offset
         from read_side_offsets
         where read_side_id = @readSideId
         and tag = @tag
     ", new
         {
             readSideId = ReadSideId,
             tag        = tag.Tag
         });
         return(Task.FromResult(
                    Offset.Sequence(
                        offset
                        )
                    ));
     }
 }