public static bool RemoveEntityById(string Id)
        {
            string typeStr = Id.Split('/')[0];
            bool   success = false;

            try
            {
                var entityOnServer = EntityRepository.entityNamesToSets[typeStr].FirstOrDefault(x => x.Id == Id);

                success = Remove(entityOnServer);
                using (var session = Dem2Hub.docDB.OpenSession())
                {
                    session.Advanced.Defer(new DeleteCommandData {
                        Key = entityOnServer.Id
                    });
                    session.SaveChanges();
                }
                //.Advanced.DocumentStore.DatabaseCommands.Delete("posts/1234", null);
            }
            catch (Exception ex)
            {
                if (ExceptionIsCriticalCheck.IsCritical(ex))
                {
                    throw;
                }
                Console.WriteLine("Entity with ID {0} was not found", Id);
                return(false);
            }

            return(success);
        }
        public void respondToReadRequest(IWebSocketConnection socket)       //"r" operation, respond to it can be only "u" (update) or "n" (not found)
        {
            Vote vote = null;
            ServerClientEntity foundEntity = EntityRepository.GetEntityFromSetsByID(entity.Id);

            if (foundEntity != null)
            {
                try
                {
                    if (foundEntity is VotableItem)    //check if the entity we are responding with is a VotableItem or not, props to http://www.hanselman.com/blog/DoesATypeImplementAnInterface.aspx
                    {
                        vote = EntityRepository.allVotes.FirstOrDefault(x => x.subjectId == entity.Id && x.OwnerId == socket.ConnectionInfo.Cookies["user"]);
                    }
                    //var comments = new List<Comment>();

                    //comments = EntityRepository.allComments.Where(x => x.parentId == entity.Id).ToList();
                    //foreach (var comment in comments)
                    //{
                    //    entityOperation createCommentOp = new entityOperation { entity = comment, operation = 'u' };
                    //    createCommentOp.sendTo(socket);
                    //}
                }
                catch (Exception ex)
                {
                    if (ExceptionIsCriticalCheck.IsCritical(ex))
                    {
                        throw;
                    }
                }

                if (entity.version < foundEntity.version)
                {
                    //first possible outcome
                    entity = foundEntity;    //so if the version on client is current by any chance we send back the same as we received
                }
                //second possible outcome
                operation = 'u';
                //client must check whether the update he received has greater version of entity, if not, he knows he has the latest version of entity
                entity = foundEntity;
            }
            else
            {
                //third possible outcome
                operation = 'n';        //not found, nonexistent, in the entity field there is still the one that was in the request, so when it arrives back to the client he will know which request ended up not found
            }
            sendTo(socket);

            if (vote != null)   //votable Item needs to be sent to client before the vote on a votable itself
            {
                entityOperation sendVoteOp = new entityOperation {
                    entity = vote, operation = 'u'
                };
                sendVoteOp.sendTo(socket);
            }
        }
        public static ServerClientEntity GetEntityFromSetsByID(string Id)
        {
            string             type           = Id.Split('/')[0];
            ServerClientEntity entityOnServer = null;

            try
            {
                entityOnServer = EntityRepository.entityNamesToSets[type].FirstOrDefault(x => x.Id == Id);
            }
            catch (Exception ex)
            {
                if (ExceptionIsCriticalCheck.IsCritical(ex))
                {
                    throw;
                }
                Console.WriteLine("Entity with ID {0} was not found", Id);
                return(null);
            }
            return(entityOnServer);
        }