我正在查看Project Silk项目的源代码,其中有一个我以前从未见过的"handler“模式。首先,这个2009年的link暗指了它,但让我不知所措
该示例显示的是一个方法类,其中每个类代表相关存储库类中每个方法的一个方法。这些类的命名方式类似于方法名。
public class GetFillupsForVehicle
{
private readonly IFillupRepository _fillupRepository;
public GetFillupsForVehicle(IFillupRepository fillupRepository)
{
_fillupRepository = fillupRepository;
}
public virtual IEnumerable<FillupEntry> Execute(int vehicleId)
{
try
{
var fillups = _fillupRepository
.GetFillups(vehicleId)
.OrderBy(f => f.Date)
.ToList();
return new ReadOnlyCollection<FillupEntry>(fillups);
}
catch (InvalidOperationException ex)
{
throw new BusinessServicesException(Resources.UnableToRetireveFillupsExceptionMessage, ex);
}
}
}有没有人能给我解释一下这个模式,或者告诉我一些我可以阅读以了解更多信息的东西?
谢谢,保罗
发布于 2012-07-20 17:18:03
请参考此关于Project Silk的信息,该信息将与现在采用的更相关。
在我从Microsoft提供的Project Silk提供的PDF文档中重发的代码片段中,Silk将帮助您了解它是如何被消费的。根据我的说法,它更多地被认为是在业务领域级别触发事件的脚手架。
也可以参考这个specific post,它可能会在他们要去的地方投射光。
public ActionResult Add(int vehicleId)
{
var vehicles = Using<GetVehicleListForUser>()
.Execute(CurrentUserId);
var vehicle = vehicles.First(v => v.VehicleId == vehicleId);
var newFillupEntry = new FillupEntryFormModel
{
Odometer = (vehicle.Odometer.HasValue)
? vehicle.Odometer.Value : 0
};
var fillups = Using<GetFillupsForVehicle>()
.Execute(vehicleId)
.OrderByDescending(f => f.Date);
var viewModel = new FillupAddViewModel
{
VehicleList = new VehicleListViewModel(vehicles, vehicleId)
{IsCollapsed = true},
FillupEntry = newFillupEntry,
Fillups = new SelectedItemList<Model.FillupEntry>(fillups),
};
ViewBag.IsFirstFillup = (!fillups.Any());
return View(viewModel);
}
https://stackoverflow.com/questions/7570492
复制相似问题