-3

I have a situation where it is possible for a user to develop complex requirements using a UI. Specifically it deals with authorising users to perform certain work based on their qualifications.

For example the user must have;

  1. All of These: Qual1, Qual2, Qual3
    • OR (One of These: (Qual4, Qual5) AND (All of These: Qual11, Qual12, Qual13))
  2. AND
    • One or More of These: Qual6, Qual7, Qual8
    • AND One of These: Qual9, Qual10

I've had a look at the Specification Pattern but I'm not sure if this is the best solution for the problem.

The requirements for each role are stored in the database using an Authorisation table linked to a Qualifications table and the user's training via a Training table linked to the Qualifications table.

4

1 回答 1

1

在代码中表示这些规则似乎很简单。首先,你把它弄得太复杂了。“And”和“all of”都只是“all”,“one or more”和“or”都只是“any”。所以你只需要几个谓词:

abstract class Requirement 
{
   abstract public bool Satisfied(User user);
}
sealed class Qual1 : Requirement { ... }
sealed class Qual2 : Requirement { ... }
...
sealed class All : Requirement 
{
   private IEnumerable<Requirement> r;
   public All(params Requirement[] r) { this.r = r; }
   public All(IEnumerable<Requirement> r) { this.r = r; }
   public override bool Satisfied(User user) {
     return r.All(x => x.Satisfied(user));
   }
}
sealed class Any : Requirement 
{
   ....

所以现在你只需要说:

var q1 = new Qual1();
... etc ...
var rule =  All(
              Any(
                 All(q1, q2, q3), 
                 All(
                     Any(q4, q5), 
                     All(q11, q12, q13))), 
              All(
                Any(q6, q7, q8), 
                Any(q9, q10)));

现在你可以说:

if (rule(user)) ...

十分简单。

于 2016-06-21T04:16:27.623 回答