我知道有很多委托/功能示例,但我找不到任何适合我的示例,或者我只是不理解它们。
我正在使用asp.net MVC作为网站,该网站需要一些Web服务调用,以便外部应用程序与我的应用程序进行交互。这些都需要一个函数来执行(转到db和whatnot),并且每次都返回一个类似的数据模型。我想在try / catch中包装每个调用并填充模型。
以下是每次调用时发生的通用代码。
var model = new ResponseDataModel();
try
{
//execute different code here
}
catch (Exception ex)
{
model.Error = true;
model.Message = ex.ToString();
}
return View(model); // will return JSON or XML depending on what the caller specifies
这是我正在使用的控制器方法/功能之一
public ActionResult MillRequestCoil()
{
var model = new ResponseDataModel();
try
{
/* edit */
//specific code
string coilId = "CC12345";
//additional code
model.Data = dataRepository.doSomethingToCoil(coilId);
//replaced code
//model.Data = new { Coil = coilId, M3 = "m3 message", M5 = "m5 message" };
model.Message = string.Format("Coil {0} sent successfully", coilId);
}
catch (Exception ex)
{
model.Error = true;
model.Message = ex.ToString();
}
return View(model);
}
我希望能够以某种方式将特定函数转换为变量以传递给通用代码。我看过代表和匿名函数,但是直到你自己完成它才会让人感到困惑。
答案 0 :(得分:5)
将以下内容放在可访问的位置:
public static ActionResult SafeViewFromModel(
Action<ResponseDataModel> setUpModel)
{
var model = new ResponseDataModel();
try
{
setUpModel(model);
}
catch (Exception ex)
{
model.Error = true;
model.Message = ex.ToString();
}
return View(model);
}
然后将其称为:
public ActionResult MillRequestCoil()
{
return MyHelperClass.SafeViewFromModel(model =>
{
string coilId = "CC12345";
model.Data = new {
Coil = coilId,
M3 = "m3 message",
M5 = "m5 message" };
model.Message = string.Format("Coil {0} sent successfully", coilId);
});
}
答案 1 :(得分:1)
public interface IAction{
public void doAction(ResponseDataModel model);
}
public class Action1 : IAction
{
public void doAction(ResponseDataModel model)
{
string coilId = "CC12345";
model.Data = new { Coil = coilId, M3 = "m3 message", M5 = "m5 message" };
model.Message = string.Format("Coil {0} sent successfully", coilId);
}
}
class Class1
{
public ActionResult MillRequestCoil(IAction action)
{
var model = new ResponseDataModel();
try
{
//specific code
action.doAction(model);
}
catch (Exception ex)
{
model.Error = true;
model.Message = ex.ToString();
}
return View(model);
}
}
使用:
var result = MillRequestCoil(new Action1());
或执行其他代码
var result = MillRequestCoil(new ActionXY());
答案 2 :(得分:0)
这是Rawling答案的变体,我相信它具有更好的可读性:
public ActionResult MillRequestCoil()
{
var model = CreateResponseDataModel(RunSpecificCode);
return View(model);
}
public ActionResult MillDoSomethingElse ()
{
var model = CreateResponseDataModel(RunOtherCode);
return View(model);
}
private ResponseDataModel CreateResponseDataModel(Action<ResponseDataModel> action)
{
var model = new ResponseDataModel();
try
{
action(model);
}
catch (Exception ex)
{
model.Error = true;
model.Message = ex.ToString();
}
return model;
}
private void RunSpecificCode(ResponseDataModel model)
{
/* edit */
//specific code
const string coilId = "CC12345";
//additional code
model.Data = _dataRepository.DoSomethingToCoil(coilId);
//replaced code
//model.Data = new { Coil = coilId, M3 = "m3 message", M5 = "m5 message" };
model.Message = string.Format("Coil {0} sent successfully", coilId);
}
private void RunOtherCode(ResponseDataModel obj)
{
// some other piece of code
}
没有lambdas,并且您的特定代码与try / catch和模型构建的模板代码之间的关注点很好。