public interface IRule
{
bool Check(string input);
}
我有一个接口来界定一项规则。 规则只是用户可以产生的一般业务规则或限制。 因此,我有两个样本规则:
public class ContainsRule : IRule
{
public string MustContain { get; set; }
public bool Check(string input)
{
return input.Contains(this.MustContain);
}
}
public class LengthRule : IRule
{
public int MaxLength { get; set; }
public bool Check(string input)
{
return input.Length <= this.MaxLength;
}
}
规则可能不止一个可以确定的财产,但举例来说,每项规则只拥有一个财产。
用户可以制定自己的一套规则,应予挽救。 例如,用户有这三项规则:
IRule[] rules = new IRule[]
{
new ContainsRule { MustContain = "foo" },
new ContainsRule { MustContain = "bar" },
new LengthRule { MaxLength = 5}
};
我需要把这一信息坚持到数据库或每个用户的一些数据库中。 由于每个用户都有自己的一套规则,我无法确定数据库表应当看什么。
User | ClassName | Parameters
-----------------------------------------------
1 | Namespace.ContainsRule | MustContain:foo
1 | Namespace.ContainsRule | MustContain:bar
1 | Namespace.LengthRule | MaxLength:5
My initial guess would be to create a table that looks something like the above, where parameters should a string. This means I would need to parse out the information and use reflection or something to set the properties. I would need to use the activator to create the class using the ClassName column. Another suggestion was instead of creating a delimited string for all the properties, there would be another table. Each of the properties would be its own row that has a foreign key relationship back to a row in the table above.
然而,这两个例子似乎并不是挽救这些规则的最佳途径。 是否有更好的办法这样做?