动态业务规则的架构

发布于 2024-08-02 12:12:51 字数 260 浏览 10 评论 0原文

我正在 .NET 中创建一个工资单应用程序。要求之一就是考勤和扣费规则要最大限度地动态、灵活。用户应该能够定义自己的规则,其中每个员工都将受到出勤规则的约束。

一种解决方案是动态编译 C# 代码,其中每个考勤规则的代码都存储在数据库中并在运行时编译,但这不是最优雅的解决方案,因为规则只能由技术人员读取/理解,而且代码的性能和可读性也不会是最好的。

我想知道是否有一个解决方案 \ 架构模式允许我定义和应用考勤规则并根据它们计算扣除额,而无需编写脚本或动态编译 C# 代码。

I am creating a payroll application in .NET. One of the requirements is that the attendance and deduction rules should be dynamic and flexible to the most extent. The user should be able to define his own rules, where every employee will be bound to an attendance rule.

One solution is to compile C# code on the fly where the code to every attendance rule is stored in the database and compiled at run time, but this isn't the most elegant solution since the rules can only be read/understood by a technical person, also the performance and readability of the code will not be the best possible thing.

I am wondering if there is a solution \ architecture pattern that allows me to define and apply attendance rules and calculate the deductions based on them without writing scripts or dynamically compile C# code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

哆啦不做梦 2024-08-09 12:12:51

Boo 是一种成熟的 .NET 语言,具有可扩展的编译器。 Ayende 写了一本好书,名为“在 Boo 中构建领域特定语言”,其中讨论了这些主题扩展编译器以允许执行以下操作:

when User.is_preferred and Order.total_cost > 1000: 
    add_discount_precentage 5 
    apply_free_shipping 
when not User.is_preferred and Order.total_cost > 1000: 
   suggest_upgrade_to_preferred  
    apply_free_shipping 
when User.is_not_preferred and Order.total_cost > 500: 
   apply_free_shipping

当编译为 .NET 程序集时,这些规则将执行得非常快。但是,您确实需要在另一个 AppDomain 中执行生成的代码,以便在规则更改时可以卸载它。

Boo is a full-fledged .NET language with an extensible compiler. Ayende has written a nice book called "Building Domain Specific Languages in Boo", where he discusses the topics of extending a compiler to allow for something like:

when User.is_preferred and Order.total_cost > 1000: 
    add_discount_precentage 5 
    apply_free_shipping 
when not User.is_preferred and Order.total_cost > 1000: 
   suggest_upgrade_to_preferred  
    apply_free_shipping 
when User.is_not_preferred and Order.total_cost > 500: 
   apply_free_shipping

When compiled to .NET assembly these rules will execute blazingly fast. You do need, however, to execute the generated code in another AppDomain so that it can be unloaded in case rules change.

套路撩心 2024-08-09 12:12:51

依靠代码编译来改变你的系统是一个非常糟糕的做法,你不妨说“规则可以随时改变,你需要的只是一个开发人员”。

如果您定义规则,则可以将参数存储在数据库中,并通过在运行时读取它们的代码应用它们。您的代码会更加复杂,但这就是我们为配置付出的代价。

您需要定义规则 - 即正式说明我可以使用哪些参数以及如何应用它们。

例如。如果 90% 的出勤率意味着扣除 5%,那么您将把这 2 个值存储在数据库中。然后,您的代码将获取实际出勤率,找到相应的行并应用扣除。除非您的规则非常复杂以至于无法使用这样的参数进行建模,否则使用配置系统是最好的方法。然后,您可以向用户提供一个简单的 GUI 来调整规则中的值。

Its a really bad practice to rely on code compilation to change your system, you might as well say "rules can be changed any time, all you need is a developer".

If you define your rules, you can store the parameters in the DB, and apply them with code that reads them at runtime. Your code will be more complex, but that's the price we pay for configuration.

You need to define the rules - ie formally say what paramaters my be used, and how they can be applied.

eg. if 90% attendance means a 5% deduction, then you'll store these 2 values in the DB. Your code will then get the actual attendance, find the corresponding row and apply the deduction. Unless your rules are so complex that they cannot be modelled in parameters like this, using a configuration system is the best way to proceed. You can then supply a simple GUI to the users to tweak the values in the rules.

ら栖息 2024-08-09 12:12:51

您可以嵌入解释器并使用一些模板、可视化工具来让用户添加新的代码片段。

You can embed an interpreter and use some templates, visual tools to let your users add new fractions of code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文