title: A good design pattern for implementing different behaviors on a subject
body: <p>What is the best design for this scenario?<br>
  I have different Object types: <code>User</code>, <code>Channel</code>, <code>MessageBox</code>, <code>UserGroup</code>, etc.<br>
  <code>User</code> and <code>Channel</code> can have permission on other objects. For example <code>User</code> has the following enum defined as its permissions for <code>MessageBox</code>:</p>
  
  <pre><code>CanRead,
  CanWrite,
  CanDelete,
  ...
  </code></pre>
  
  <p>Other enums are defined for <code>User</code> as the owner of other object types.  </p>
  
  <p>Also, <code>Channel</code> has different enum values on these objects. For example, consider <code>Channel</code> as the owner and <code>MessageBox</code> as the object:  </p>
  
  <pre><code>CanDispath
  CanRetrieve
  ...
  </code></pre>
  
  <p>All the permissions are saved and retrieved from a specific table in database using bitwise comparison:  </p>
  
  <pre><code>OwnerID........OwnerType........ObjectID........ObjectType........AccessLevel  
    1              User              10           MessageBox            38     
    5             Channel            12           MessageBox            18  
  </code></pre>
  
  <p>Now in code behind,  What's the best way to implement permission classes?  </p>
  
  <p>1- Define <code>PermissionManager</code>, <code>UserPermissionManager</code>, <code>ChannelPermissionManager</code> classes separately from each other. Other classes just call <code>PermissionManager</code> like:   </p>
  
  <pre><code>if (new PermissionManager.HasAccess(CurrentUser,  
                                      CurrentMessageBox, 
                                      UserPermissions.CanReadMessages))  
  </code></pre>
  
  <p>Then <code>PermissionManager</code> decides what class this is related to based on the <code>OwnerType</code> (<code>UserPermissionManager</code>  or <code>ChannelPermissionManager</code>) and calls its <code>HasAccess</code> method. This way, <code>PermissionManager.HasAccess</code> is always being called and I think it can make the code more maintainable and extensible. This is my preferred solution but since <code>PermissionManager</code>, <code>UserPermissionManager</code> and <code>ChannelPermissionManager</code> refer to the same context, I think there should be a hierarchy or possibly an interface so these 3 classes become more integrated. But I don't know how to relate them together.</p>
  
  <p>2- Define <code>IPermissionManager</code> interface and implement <code>UserPermissionManager</code> and <code>ChannelPermissionManager</code> from it. Add <code>PermissionManagerTypes</code> enum. Create a factory class and call Managers like:  </p>
  
  <pre><code>IPermissionManager UserManager =   
      PermissionFactory.Create(PermissionsManagerTypes.User);
  if (UserManager.HasAccess(CurrentUser,  
                            CurrentMessageBox, 
                            UserPermissions.CanReadMessages))  
  </code></pre>
  
  <p>This is a kind of failed try to relate classes together. But I thought It'd be good to mention it here to let you know what I'm trying to achieve.</p>
  
  <p>P.S. I cannot define classes as static since they need to have a private variable of type  <code>ObjectContext</code> (Entity Framework).  </p>
  
  <p>Is there a better solution to achieve this?<br>
  Thank you and Apologies for the very lengthy question.</p>