【发布时间】:2019-04-28 20:48:25
【问题描述】:
我有两条业务规则,我尝试申请:
- 用户只有在所有发票都关闭时才能插入新发票。
- 发票关闭后,用户无法编辑她。
发票有两种状态:当前、已关闭、已付款和已取消
为此,我在业务层的创建方法中实现了我的业务逻辑
当用户尝试点击提交按钮时,httppost 操作方法调用业务代码。
但我想在 httpget 创建/编辑操作方法中应用这些规则,这样,当用户尝试单击添加按钮以显示创建/编辑视图时,他会分别获得创建和编辑的异常消息
这是我的代码
//business logic
public AddInvoice(Invoice invoice)
{
var invoicesCount = Context.Invoices.Count(x=>x.State !=
InvoiceState.Closed);//InvoiceState is enum
if (invoicesCount > 0)
throw new BusinessReulesException("you should close all your
invoices before insert");
Context.Invoices.Add(invoice);
Context.SaveChanges();
}
public UpdateInvoice(Invoice invoice)
{
if (Context.Entry(invoice).State == EntityState.Detached)
Context.Invoices.Attach(invoice);
if (invoices.State == InvoiceState.Closed)
throw new BusinessReulesException("you can't update an closed invoices );
Context.Entry(invoice).State =EntityState.Modified;
Context.SaveChanges();
}
//controller code
[httpGet]
public ActionResult Create()
{
//how to apply business logic for catch Exception here
}
[httpPost]
public ActionResult Create(Invoice invoice)
{
if(ModelState.isValide){
try{
invoiceBll.AddInvoice(invoice);
return RedirectToAction("Index");
}
catch(BusinessRulesException ex){
ViewBag.Message = ex.Message;
}
}
}
//the same thing for update
//the rest of code
有什么想法吗?
【问题讨论】:
标签: c# asp.net-mvc entity-framework