Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I just finished creating something very similar for a system I'm working on. The theory, in general, is to separate the definition (who they are) of an entity from its behaviour (roles; what they do). I also wanted to preserve the ability to query users based on the metadata contained in the roles that they are assigned to.</p> <p>I ended up creating a <code>Person</code> class that looks like this:</p> <pre><code>class Person { Guid Id; ISet&lt;Role&gt; Roles; string Name; // and any other stuff a "person" might have } </code></pre> <p>Next, I have an abstract <code>Role</code> class that looks like this:</p> <pre><code>abstract class Role { Guid Id; abstract string Name { get; } // implemented by concrete definitions of roles Person Person; } </code></pre> <p>From this, I derive individual classes that "define" each role I have in my system. A <code>User</code> role would look something like this:</p> <pre><code>class User : Role { override string Name { get { return "User"; } } string LoginId; string Password; // etc. } </code></pre> <p>Rinse and repeat for any other roles you might have.</p> <p>At the ORM level (where I use NHibernate), I define UserRole as a joined-subclass of the Role entity.</p> <p>The underlying assumptions behind this approach are:</p> <ul> <li>Roles aren't dynamic.</li> </ul> <p>There is no definition of roles at the data layer. A role exists simply because there's a class that derives from it.</p> <ul> <li>You can't assign a role to the same person multiple times with different metadata</li> </ul> <p>Well, in theory, I guess you could but it makes no sense for my particular system for a person to have two <code>User</code> roles assigned to them with different login ids and passwords.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload