English 中文(简体)
B. 每位用户节省业务规则
原标题:Saving business rules per user
  • 时间:2011-05-04 09:19:48
  •  标签:
  • c#
  • sql
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.

然而,这两个例子似乎并不是挽救这些规则的最佳途径。 是否有更好的办法这样做?

最佳回答

Why not use XML serialisation. The database table would contain the User # and the XML serialisation of the indvidual rule.

问题回答

I believe that you are retrieving from database.
If so, try my suggestion.

Store the retrieved data into datatable, or convert them to datatable before applying the rule.
So the rule can be put as normal sql in datatable s filter property. Instead of rule name and parameters, you can change it to a column containing
column like %foo%

在数据分析中采用这一方法。

PS: I have converted List to Datatable and used such filter before

在我看来,简单地说,你需要把物体绘制到表格中(如果只有一种能够自动做到这一点的技术......)

Based on what you have posted, I envisage at least six SQL tables, more if there are further subclasses of rule e.g.

  • Rules
  • MustContainTextRules (subclass of Rules, 1:0..m)
  • MaxLengthRules (subclass of Rules, 1:0..1)
  • [Possibily further subclasses of Rules e.g. MinLengthRules?]
  • Rulesets ("a set of Rules")
  • Users
  • RulesetOwnership (relationship table between Users and Rulesets)

无论它们是不同的表格,都可能受到规则是否相互关联的影响,例如,如果<条码>明文规则>对<规则>的价值不能超过其<条码>MaxLength 细则的价值,那么在表格不同时,你可能会发现难以写出验证这一条的因素。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签