在评估规则时,我陷入了一个场景,它初始化类B并调用默认构造函数,但我是通过参数构造函数注入Db连接的。请参阅下面的代码和建议。
public class A
{
[ExternalMethod(typeof(B), "IsSpecialWord")]
[Field(DisplayName = "English Word", Description = "English Word", Max = 200)]
public string EngWord { get; set; }
}
public class B
{
public IDbCoonection dbCon { get; set; }
public B(IDbConnection db)
{
dbCon = db;
}
[Method("Is Special Word", "Indicates to test abbreviated word")]
public IsSpecialWord(sting word)
{
db.CheckSpecialWord(word);
}
public insert SpecialWord(T t)
{
db.Insert();
}
public update SpecialWord(T t)
{
db.Update();
}
}
public class TestController
{
A aObj = new A();
aObj.EngWord ="test";
IDbConnection db = null;
if(1)
db = new SQLDBConnection(connString);
else
db = new OracleDBConnection(connectionString);
B b = new B(db);
Evaluator<A> evaluator = new Evaluator<A>(editor.Rule.GetRuleXml());
bool success = evaluator.Evaluate(aObj);
}
发布于 2020-08-11 18:22:42
如果使用实例外部方法,则声明类必须具有空构造函数,以便引擎能够实例化该类并成功调用该方法。
在本例中,您可以在源中声明一个Connection
属性,在计算开始之前设置它的值,然后将该源作为参数传递,以便在外部方法中使用该连接。源类型的参数对规则作者是不可见的,它们在计算期间由引擎自动传递给方法。在Rule XML中,它们被声明为<self/>
参数节点。
下面是它可能的样子:
public class A
{
public IDbCoonection Connection { get; set; }
// The rest of the source type goes here...
}
public class B
{
public B( ) { } // Empty constructor
[Method("Is Special Word", "Indicates to test abbreviated word")]
public bool IsSpecialWord( A source, string word )
{
source.Connection.CheckSpecialWord(word);
}
}
规则将如下所示:
If Is Special Word ( test ) then Do Something
请注意,规则创建者不必将源代码作为param传递给方法,它会自动发生。
https://stackoverflow.com/questions/63353855
复制