简体   繁体   English

NVelocity(或Velocity)作为独立的公式评估器

[英]NVelocity (or Velocity) as a stand-alone formula evaluator

I am using NVelocity in my application to generate html emails. 我在应用程序中使用NVelocity生成HTML电子邮件。 My application has an event-driven model, where saving and/or updating of objects causes these emails to be sent out. 我的应用程序具有事件驱动的模型,其中对象的保存和/或更新导致这些电子邮件被发送出去。 Each event can trigger zero, one or multiple multiple emails. 每个事件可以触发零个,一个或多个多封电子邮件。

I want to be able to configure which emails get sent out at run-time without having to modify code. 我希望能够配置在运行时发送哪些电子邮件,而无需修改代码。 I was thinking I could leverage the NVelocity #if() directive to do this. 我当时想我可以利用NVelocity #if()指令执行此操作。 Here is my idea... 这是我的主意...

Step 1) Prior to email sending, the administrator must configure a formula for NVelocity to evaluate. 步骤1)在发送电子邮件之前,管理员必须配置NVelocity的公式进行评估。 For example: 例如:

$User.FirstName == "Jack"

Step 2) When an object is saved or created, build an NVelocity template in memory based on the input formula. 步骤2)保存或创建对象后,请根据输入公式在内存中构建NVelocity模板。 For example: 例如:

String formula = GetFormulaFromDB(); // $User.FirstName == "Jack"
String templ = "#if( " + formula + ") 1 #else 0 #end";

Step 3) Execute the NVelocity engine in memory against the template. 步骤3)对模板执行内存中的NVelocity引擎。 Check the results to see if we have to send the email: 检查结果以查看是否必须发送电子邮件:

String result = VelocityMerge(templ); // utility function 
if( result.Trim() == "1" )
{
    SendEmail();
}

I know this is not exactly what NVelocity was intended to do, but I think it just might work :) One of the benefits of doing things this way is that the same syntax can be used for the formula as is used inside the template. 我知道这并不是NVelocity的原意,但我认为它可能会起作用:)用这种方式做事的好处之一是,公式中可以使用与模板内相同的语法。

Does anybody have any words of caution or suggestions? 有人有任何警告或建议的话吗? Is there a way to execute the #if() directive without jumping through hoops like I have above? 有没有一种方法可以执行#if()指令而不会像我上面那样跳过篮球? Is there a recommended way to validate the formula syntax ahead of time? 是否有建议的方法可以提前验证公式语法?

Thanks. 谢谢。

If a non-technical end-user is the one that changes the criteria, I'd be very careful to validate his input. 如果非技术的最终用户是更改标准的用户,那么我将非常小心地验证他的输入。 You could easily validate it by running the template (ie your VelocityMerge() method) against the input, if NVelocity complains you reject the input, otherwise save it to the database. 如果NVelocity抱怨您拒绝输入,则可以通过对输入运行模板(即VelocityMerge()方法)来轻松验证它,否则将其保存到数据库中。

Another limitation is that you'd have to know upfront all the variables that can be present on the criteria, as serg555 commented. 另一个限制是,您必须预先知道标准中可能存在的所有变量,如serg555所评论。 Some mail template might need a variable that no other mail needs, but still you'd have to make it available to all templates. 某些邮件模板可能需要一个其他邮件不需要的变量,但仍然需要使它可用于所有模板。 Whether this is a real limitation or not depends on the homogeneity (?) of your mail templates. 这是否是真正的限制取决于邮件模板的同质性(?)。

Also the end-user that defines the criteria would have to know all available variables (eg $User ) and the properties of each variable (eg FirstName, LastName, etc). 定义标准的最终用户还必须知道所有可用变量(例如$User )和每个变量的属性(例如FirstName,LastName等)。 Some help screen that lists them, perhaps. 可能会列出一些帮助屏幕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM