Attach middleware functionality onto an object