我正在使用NRules来定义规则,并试图在NRules基类中使用接口,但是发生了一些错误,我得到了“没有为这个对象定义无参数构造函数”的错误。下面是我的接口定义
{
public interface ICalcDiscount
{
public void ApplyPoint(int point);
}
public class CalcDiscount:ICalcDiscount
{
private readonly UniContext _context;
public CalcPoint(UniContext context)
{
_context = context;
}
public void ApplyDiscount(int d)
{
_context.Discount.Add(new Discount{ CustomerId = 1, d= d});
_context.SaveChanges();
}
}
}NRule类
public class PreferredCustomerDiscountRule : Rule
{
private readonly ICalcDiscount _d;
public PreferredCustomerDiscountRule(ICalcDiscount d)
{
_d = d;
}
public override void Define()
{
Book book = null;
When()
.Match(() => book);
Then()
.Do(ctx => _c.ApplyDiscount(10));
}
}当NRules开始加载程序集MissingMethodException时,我收到一个错误:没有为此对象定义无参数构造函数。
//Load rules
var repository = new RuleRepository();
repository.Load(x => x.From(typeof(PreferredCustomerDiscountRule).Assembly));//problem is here!
//Compile rules
var factory = repository.Compile();发布于 2019-08-30 17:27:07
正如错误所说,您不能有一个包含参数的构造函数的规则。
您还应该解析规则定义中的依赖项,如NRules维基上的Rule Dependency所示。
因此,您的类应该类似于以下内容:
public class PreferredCustomerDiscountRule : Rule
{
public override void Define()
{
Book book = null;
ICalcDiscount discountService = null;
Dependency()
.Resolve(() => discountService);
When()
.Match(() => book);
Then()
.Do(_ => discountService.ApplyDiscount(10));
}
}https://stackoverflow.com/questions/56535788
复制相似问题