Q:

SignalR Generic Hub

I am working on ASP.net Web Api , SignalR , want to send the message to all users  , Lock other users while updating when entity updated but want to avoid duplicating code in HUB and Web API Controller.
I want the generic code represent the below behaviour for all entities not 1 per entity.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using SignalRGridDemo.Models;
using System.Data;
using System.Collections.Concurrent;
using System.Threading.Tasks;

namespace SignalRGridDemo.Hubs {
  [HubName("employee")]
  public class EmployeeHub : Hub {
    private static ConcurrentDictionary<string, List<int>> _mapping = new ConcurrentDictionary<string, List<int>>();
    private SignalRGridDemoContext db = new SignalRGridDemoContext();

    public override Task OnConnected() {
      _mapping.TryAdd(Context.ConnectionId, new List<int>());
      Clients.All.newConnection(Context.ConnectionId);
      return base.OnConnected();
    }

    public void Lock(int id) {
      var employeeToPatch = db.Employees.Find(id);
      employeeToPatch.Locked = true;
      db.Entry(employeeToPatch).State = EntityState.Modified;
      db.SaveChanges();
      Clients.Others.lockEmployee(id);
      _mapping[Context.ConnectionId].Add(id);
    }

    public void Unlock(int id) {
      UnlockHelper(id);
      _mapping[Context.ConnectionId].Remove(id);
    }

    private void UnlockHelper(int id) {
      var employeeToPatch = db.Employees.Find(id);
      employeeToPatch.Locked = false;
      db.Entry(employeeToPatch).State = EntityState.Modified;
      db.SaveChanges();
      Clients.Others.unlockEmployee(id);
    }

    public override Task OnDisconnected() {
      foreach (var id in _mapping[Context.ConnectionId]) {
        UnlockHelper(id);
      }
      var list = new List<int>();
      _mapping.TryRemove(Context.ConnectionId, out list);
      return base.OnDisconnected();
    }
  }
}

A:

I don't think that you need a generic Hub, what you need is a generic parameter, or to be more specific a parameter that inherits the Lock functionality
so what you need is:

Make your entities inherit an ILockable interface, with lock\unlock methods
Make a version of the Find\Save methods that return an ILockable object
Do the locking functionality on the returned object then save it 

