Rav*_*Ram 0 c# refactoring database-connection
我试图避免两次调用数据库.我需要检查记录是否存在,如果是,那么用数据填充我的视图.我有以下代码:
if (Presenters.PayeePresenter.GetByID(id) != null)
{
view = BLL.Presenters.PayeePresenter.GetByID(id);
msg.Success = true;
msg.Text = "Record Found";
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能对数据库进行最少量的调用?
将结果存储在变量中,并在分配属性之前检查其是否为null.
var obj = Presenters.PayeePresenter.GetByID(id); //Assuming this is database method call
if (obj!= null)
{
//use obj.Properties to fill custom object or any additional logic
msg.Success = true;
msg.Text = "Record Found";
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
67 次 |
最近记录: |