有没有更好的方法来编写业务逻辑来验证互斥代码?

时间:2015-07-09 13:32:41

标签: c# business-logic

我有两个属性。我需要针对显示兼容值的相当大的矩阵验证这些属性。

示例属性:

public string firstValue { get; set; }
public string lastValue { get; set; }

样本矩阵:

    A   B   C
A   X   O   O
B   O   X   X
C   X   O   X

让x轴代表" firstValue" y轴代表" lastValue"。该程序必须验证由" X"表示的无效组合。向用户抛出错误。

样本验证:

if (firstValue == "A")
{
    if (lastValue == "A" || lastValue =="C)
        Console.WriteLine("Invalid Combination");
}

显然可以使用if \ else和switch逻辑以多种方式重构,但是当你向矩阵添加数百个值时,它开始变得可怕。我决定在矩阵中创建一个每行包含一个函数的新类。

public class MutuallyExclusiveValidation
{
    public void CheckA(string lastValue)
    {
        if (lastValue == "A" || lastValue == "C")
        {
            Console.WriteLine("Invalid Combination");
        }
    }

    public void CheckB(string lastValue)
    {
        if (lastValue == "B")
        {
            Console.WriteLine("Invalid Combination");
        }
    }

    public void CheckC(string lastValue)
    {
        if (lastValue == "B" || lastValue == "C")
        {
            Console.WriteLine("Invalid Combination");
        }
    }
}

然后我设计了这样的东西,以确保我没有达到比我需要更多的代码。谓词将返回布尔选择并调用需要运行的验证方法。 (只是一个样本,而不是生产代码)

    public static void CheckForMutuallyExclusiveValues(string firstValue, string lastvalue)
    {
        var allValidators = GetMutuallyExclusiveValidators(firstValue, lastvalue);
        var requiredValidators = allValidators.First(x => x.Key(firstValue));
        var validationClassInstance = new MutuallyExclusiveValidation();
        requiredValidators.Value(validationClassInstance);
    }

    public static Dictionary<Predicate<string>, Action<MutuallyExclusiveValidation>> GetMutuallyExclusiveValidators(string firstValue, string lastvalue)
    {
        var returnDictionary = new Dictionary<Predicate<string>, Action<MutuallyExclusiveValidation>>();
        Action<MutuallyExclusiveValidation> validation;
        Predicate<string> condition;

        validation = x => x.CheckA(lastvalue);
        condition = x => x == "A";
        returnDictionary.Add(condition, validation);

        validation = x => x.CheckB(lastvalue);
        condition = (x => x == "B");
        returnDictionary.Add(condition, validation);

        validation = x => x.CheckC(lastvalue);
        condition = (x => x == "C");
        returnDictionary.Add(condition, validation);

        return returnDictionary;
    }

这似乎很有效,但我无法帮助您思考更好的方法。有什么想法吗?

编辑:Fabjan的回答是我为这个特殊问题而回答的问题。还要感谢Tim Schmelter。

2 个答案:

答案 0 :(得分:3)

使用某种方法创建一个二维数组并使用它来验证记录,如:

class MyClass
{
    bool[,] matrix = new bool[,]
    {            
        {false, true, false},  // x1 y123
        {true, false, true},   // x2 y123
        {true, false, false},  // x3 y123
    };

    string[] xValues = { "A", "B", "C" };
    string[] yValues = { "A", "B", "C" };

    public bool IsValid(string value1, string value2)
    {
        return matrix[Array.IndexOf(xValues, value1), Array.IndexOf(yValues, value2)];
    }
}


class Program
{
    static void Main()
    {
        MyClass c = new MyClass();

        Console.WriteLine(c.IsValid("A", "A"));

        Console.WriteLine(c.IsValid("B", "C"));

        Console.WriteLine(c.IsValid("A", "C"));

        Console.ReadKey();
    }
}

输出:

Output

答案 1 :(得分:3)

您可以使用Dictionary<string, HashSet<string>>将X值存储为关键字,将所有无效的Y值存储为值。

Dictionary<string, HashSet<string>> InvalidValues = new Dictionary<string, HashSet<string>> { 
    {"A", new HashSet<string>{"A", "C"}},
    {"B", new HashSet<string>{"B"}},
    {"C", new HashSet<string>{"B", "C"}}
};

现在,如果给定的x值和y值无效,您可以有效地检查:

var emptySet = new HashSet<string>();
var invalidValues = allValues
    .Where(x => InvalidValues.TryGet(x.FirstValue, emptySet).Contains(x.LastValue));
foreach (Value val in invalidValues)
    Console.WriteLine("Invalid Combination: {0}|{1}", val.FirstValue, val.LastValue);

使用此扩展来linqify Dictionary.TryGetValue

public static TValue TryGet<TKey, TValue>(this Dictionary<TKey, TValue> dict, TKey key, TValue defaultValue = default(TValue))
{
    TValue value;
    bool success = dict.TryGetValue(key, out value);
    if (success)
        return value;
    else
        return defaultValue;
}

这堂课:

public class Value
{
    public string FirstValue { get; set; }
    public string LastValue { get; set; }
}

和一些样本值:

var allValues = new List<Value> { 
    new Value { FirstValue = "A", LastValue = "C" }, 
    new Value { FirstValue = "A", LastValue = "B" }, 
    new Value { FirstValue = "B", LastValue = "C" }, 
    new Value { FirstValue = "A", LastValue = "A" } 
};