Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It seems likely that your code will eventually compile a list of UI elements to hide, or perform a given action to, and then perform those actions based on the current role. Something like</p> <pre><code>Dictionary&lt;Control, Action&lt;Control, string&gt;&gt; actions = new Dictionary&lt;Control, Action&lt;Control, string&gt;&gt; { { button, (c, r) =&gt; c.Enabled = (r == "administrator") }, // etc. }; </code></pre> <p>How you compile that list is primarily what your question is concerned with. AOP frameworks definitely help with the separation of concerns, but a homebrew solution wouldn't be impossible. I'm thinking something like:</p> <ul> <li>Create an <code>EnableForRoleAttribute</code>, with parameter <code>role</code>.</li> <li>Reflect on your forms (possibly using reflection to find the forms, or possibly providing them to your code directly, or even finding forms decorated with a <code>RoleVaryingAttribute</code> of your creation).</li> <li>Reflect on the fields of your forms, filtering for <code>Control</code> instances, then for <code>Control</code> instances with the <code>EnableForRoleAttribute</code>.</li> <li>Now you have your list! Set <code>Enabled</code> based on the role.</li> </ul> <p>The above bulleted list is specific to your <code>Enabled</code> example, mainly because attributes can't take lambdas as parameters :(. You could be more flexible with a <code>SetPropertyIfInRoleAttribute</code> with parameters <code>role</code>, <code>propertyName</code>, and <code>propertyValue</code>. Or any such construction.</p> <p>Basically, the AOP frameworks make this work easier for you, and some like PostSharp make it happen at compilation time instead of runtime. But the homebrew solution is pretty valid too.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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