Exemplo n.º 1
0
        public static HybridDbMessage Execute(Func <string, Type, object> deserializer, DocumentTransaction tx, DequeueCommand command)
        {
            var tablename = tx.Store.Database.FormatTableNameAndEscape(command.Table.Name);

            var msg = (tx.SqlConnection.Query <(string Message, string Discriminator)>(@$ "
                    set nocount on; 
                    delete top(1) from {tablename} with (rowlock, readpast) 
                    output deleted.Message, deleted.Discriminator 
                    where Topic = @Topic;
                    set nocount off;",
                                                                                       new { command.Topic },
                                                                                       tx.SqlTransaction
                                                                                       )).SingleOrDefault();

            if (msg == default)
            {
                return(null);
            }

            var type = tx.Store.Configuration.TypeMapper.ToType(typeof(HybridDbMessage), msg.Discriminator);

            return((HybridDbMessage)deserializer(msg.Message, type));
        }
Exemplo n.º 2
0
        public static void UseMessageQueue(this Configuration config, string tablename = "messages")
        {
            config.GetOrAddTable(new QueueTable(tablename));

            config.Decorate <Func <DocumentTransaction, DmlCommand, Func <object> > >((_, decoratee) => (tx, command) => () =>
                                                                                      Switch <object> .On(command)
                                                                                      .Match <EnqueueCommand>(enqueueCommand => EnqueueCommand.Execute(config.Serializer.Serialize, tx, enqueueCommand))
                                                                                      .Match <DequeueCommand>(dequeueCommand => DequeueCommand.Execute(config.Serializer.Deserialize, tx, dequeueCommand))
                                                                                      .Else(() => decoratee(tx, command)()));
        }