-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 に答える