【发布时间】:2016-04-22 07:23:51
【问题描述】:
我有一个内部 CRM 系统,可以让客户查看他们的发票。
函数中的第 5 行检查发票是否属于已登录的客户(if (invoice.CustomerId != loggerInCustomerId))。
我不确定是否应该在此处进行检查。
public ActionResult ViewInvoice(Guid invnum)
{
int loggerInCustomerId = GetTheLoggedInCustomerId();
Invoice invoice = _invoiceLogic.GetInvoice(invnum);
if (invoice.CustomerId != loggerInCustomerId)
{
//Invalid Action
return RedirectToAction("Index", "MyInvoices");
}
//do other stuff as normal
}
是否应该将此检查移至业务逻辑中? GetInvoice 将接受发票编号参数和登录用户的参数。 GetInvoice 然后会做这个检查并抛出一个异常,我会在我的操作方法中有一个 Try Catch。
或者有更好的方法吗?
【问题讨论】:
-
是的,最好将其移动到业务逻辑中,这样代码重用能力也将在那里。
-
这个问题没有一个好的答案,因此应该关闭。
标签: c# .net asp.net-mvc business-logic