void ServerClientEntity_OnChange(ServerClientEntity o, EventArgs e)
 {
     foreach (var subscriber in subscribedUsers)
     {
         var op = new entityOperation()
         {
             operation = 'u', entity = this
         };
         Dem2Hub.sendItTo(op, subscriber.Value.connection);
     }
 }
 public void sendTo(IWebSocketConnection socket)
 {
     Dem2Hub.sendItTo(this, socket);
     if (operation == 'u' || operation == 'c')
     {
         var subs = new Subscription()
         {
             onEntityId = entity.Id
         };                                                         //we presume, that the entity will be displayed at the client, so we subscribe him
         subs.subscribe(socket);
     }
 }
        internal void resolveEntityRequest(IWebSocketConnection socket, JObject receivedObj)
        {
            switch (operation)
            {
            case 'c':       //create new user generated entity
                /*
                 * //Example shows json which creates new Vote for the present user
                 * {
                 *   "msgType": "entity",
                 *   "operation": "c",
                 *   "className": "Vote",
                 *   "entity": {"subjectID": "voting/215", "Agrees": true}
                 * }
                 *
                 * // TODO implement create spam check here
                 */
            {
                try
                {
                    var  className  = (string)receivedObj["className"];
                    Type type       = Type.GetType("Dem2UserCreated." + className);
                    var  sourceJSON = receivedObj["entity"].ToString();

                    var instance = JsonConvert.DeserializeObject(sourceJSON, type, new IsoDateTimeConverter());
                    entity         = (ServerClientEntity)instance;
                    entity.OwnerId = fromUser.Id;
                    entity.Subscribe(fromUser);
                    switch (className)
                    {
                    case "Vote":
                        var newVote = (Vote)instance;              //the serialized entity must be initialized
                        newVote.InitVote(fromUser);                //the after initialization, we return the entity back
                        Console.WriteLine("Created new vote with Id {0}", newVote.Id);
                        break;

                    case "Comment":
                        var newComment = (Comment)instance;
                        EntityRepository.Add(newComment);
                        Console.WriteLine("Created new comment with Id {0}", newComment.Id);
                        break;

                    case "Listing":
                        var newListing = (Listing)instance;
                        newListing.JSONQuery.sourceJSON = receivedObj["entity"]["JSONQuery"].ToString();;
                        var added = EntityRepository.Add(newListing);
                        if (added == false)
                        {
                            entity = EntityRepository.allListings.Single(x => x.JSONQuery.sourceJSON == sourceJSON);
                            entity.Subscribe(fromUser);
                            sendTo(socket);
                        }

                        Console.WriteLine("Created new listing with Id {0}", newListing.Id);
                        break;

                    default:
                        break;
                    }

                    //var op = new entityOperation() { operation = 'c', entity = instance as ServerClientEntity };
                    //
                    Console.WriteLine("Object {0} created", instance.ToString());
                }
                catch (Exception)
                {
                    throw;
                }
            }
            break;

            case 'r':       //read an entity
                /*
                 * Example shows json for this branch
                 * {
                 * "msgType": "entity",
                 * "operation": "r",
                 * "entity":{
                 *    "Id": "user/132"
                 * }
                 * }*/
                respondToReadRequest(socket);
                break;

            case 'u':       //read an entity
                /*
                 * Example shows json for this branch
                 * {
                 * "msgType": "entity",
                 * "operation": "r",
                 * "entity":{
                 *    "Id": "user/132"
                 * }
                 * }*/
                respondToUpdateRequest(receivedObj);
                break;

            case 'd':     //delete an entity
                /*
                 * {
                 *  "msgType": "entity",
                 *  "operation": "d",
                 *  "entity": {"Id": "voting/215"}
                 * }
                 *
                 */
            {
                string IdPrefixStr = entity.Id.Split('/')[0];
                Type   type        = EntityRepository.GetRepoTypeById(IdPrefixStr);

                var instance = JsonConvert.DeserializeObject(receivedObj["entity"].ToString(), type, new IsoDateTimeConverter());

                {
                    var Id = (string)receivedObj["entity"]["Id"];
                    ServerClientEntity toDelete = EntityRepository.GetEntityFromSetsByID(Id);
                    var deletor = socket.ConnectionInfo.Cookies["user"];
                    if (deletor == toDelete.OwnerId)
                    {
                        var success = toDelete.Delete();
                        if (success)
                        {
                            if (toDelete is Vote)
                            {           // update the subject
                                Vote aVote   = (Vote)toDelete;
                                var  subject = EntityRepository.GetEntityFromSetsByID(aVote.subjectId);
                                subject.IncrementVersion();
                            }
                            var op = new entityOperation()
                            {
                                operation = 'd', entity = new ServerClientEntity(toDelete.Id, toDelete.version)
                            };
                            Dem2Hub.sendItTo(op, socket);
                        }
                    }
                    else
                    {
                        var err = new ServerError()
                        {
                            message = "sorry, you can't delete the entity id " + Id
                        };
                        Dem2Hub.sendItTo(err, socket);
                    }
                }
            }
            break;

            default:
                Console.WriteLine("Unknown type of request: {0}", operation);
                break;
            }
        }