Lua:我可以使用"require"吗?作为依赖注入的一种形式? [英] Lua: can I use "require" as a form of dependency injection?

查看:113
本文介绍了Lua:我可以使用"require"吗?作为依赖注入的一种形式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个医院集成系统,该系统将使用来自各家医院(我们的客户)的数据,然后根据从数据库中读取的配置将特定的业务规则应用于所述数据.如果我使用Java,我的第一个直觉将是构建一系列代表各种业务规则的接口,然后注入具体实例(使用Spring/guice)以构成一个完全配置的对象.这将使我能够实现配置逻辑(应将哪些biz规则应用于Hospital Foo?)与实际业务规则之间的明确区分.

I'm designing a hospital integration system which consumes data from various hospitals (our customers), and then applies specific business rules to said data based on reading configurations from a database. If I were using Java, my first instinct would be to build a series of interfaces representing the various business rules, and then inject concrete instances (using Spring/guice) to compose a fully configured object. This would allow me to achieve a clean separation between configuration logic (what biz rules should apply to Hospital Foo?) and the actual business rules themselves.

不幸的是,我没有使用Java,而是使用Lua.在过去的几天里,我一直沉浸在Lua文献中,而与DI最相似的地方似乎是模块的使用.此外,看来控制运行时lua模块如何解决的规则完全基于询问本地文件系统.

Unfortunately, I'm not using Java, I'm using Lua. I've immersed myself in Lua literature over the last few days, and the closest analogue to DI appears to be the use of modules. Furthermore, it appears that the rules that govern how lua modules are resolved at run-time are based entirely on interrogating the local file system.

模块模式"是实现我所追求的最好/唯一方法(将配置逻辑与biz逻辑分离)吗?如果是这样,如何利用Lua的模块加载规则来更改运行时加载的实际模块?

Is the "module pattern" the best/only way to achieve what am I after (separation of configuration logic from biz logic)? If so, how can one leverage Lua's module loading rules to vary the actual module(s) loaded at run-time?

推荐答案

步骤1:不再像Java程序员那样思考.

Step 1: Stop thinking like a Java programmer.

您现在在 Lua 中.没有带有显式和编译时固定原型的类.函数是一流的对象;它们是.因此,以这种方式解决问题.

You're in Lua now. There are no classes with explicit and compile-time fixed prototypes. Functions are first-class objects; they are values. So break the problem down in that way.

您有一堆规则"(即:函数).您想将其中一些规则应用于某些数据.您有一个配置系统,上面写着:将规则应用于来自位置X的数据时,请使用此规则集."所以...那样做.

You have a bunch of "rules" (ie: functions). You want to apply some of those rules to certain data. You have a configuration system that says, "When applying rules to data from location X, use this set of rules." So... do that.

您检测到数据来自位置X.因此,您调用配置逻辑以为位置X构建一个Lua表,该表包含要应用于该数据的规则(即函数).位置X的配置逻辑从存储规则的任何地方加载规则,并将其返回.如果配置在数据库或其他内容中,则位置X的数据库条目可能会按名称引用规则.

You detect that data comes from location X. So you call your configuration logic to build a Lua table for location X that contains your rules (ie: functions) to apply to that data. The configuration logic for location X loads the rules from wherever they are stored, and returns them. If the configuration is in a database or something, then the database entry for location X would probably refer to rules by name.

如何将规则名称转换为实际的Lua函数取决于您,但是有很多方法.您可能有一个注册表,该注册表将目录中的所有Lua文件预加载到表中,然后根据该表中的名称选择规则.或者,也许您有一个名为Lua的文件/脚本数据库,每个文件/脚本都是一个单独的规则.有很多方法可以实现这一点.

How you convert that rule name into an actual Lua function is up to you, but there are many ways. You could have a registry that preloads all of the Lua files from a directory into a table, and then pick the rule based on names in that table. Or perhaps you have a database of named Lua files/scripts, each of which is a separate rule. There are any number of ways to implement this.

在Lua中,依赖注入"仅是决定如何构建功能集合".这不是什么特别的事情,因为Lua比Java是一种自由格式的语言.这只是函数的来源,如何将它们放到一个表中,然后如何将该表应用于某些数据的问题.所有这些都取决于您.

In Lua, "dependency injection" is just "deciding how to build collections of functions." It's not a special thing because Lua is a much more free-form language than Java. It's just a matter of where the functions come from, how to put them together in a table, and then how to apply that table to some data. And all of those are up to you.

这篇关于Lua:我可以使用"require"吗?作为依赖注入的一种形式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆