考虑两个观点。哪个是首选,哪种情况下你会使用样式1而不是样式2,反之亦然?
using System.Web.Mvc;
using Ninject;
namespace Views.Models.ViewClasses {
public abstract class CalculatorView : WebViewPage {
[Inject]
public ICalculator Calulator { get; set; }
}
}
观点:
@inherits Views.Models.ViewClasses.CalculatorView
@{
ViewBag.Title = "Calculate";
}
<h4>Calculate</h4>
The calculation result for @ViewBag.X and @ViewBag.Y is @Calulator.Sum(ViewBag.X, ViewBag.Y)
public class CalculatorModel {
// a constructor in here somewhere
public int X { get; set; }
public int Y { get; set; }
public int SumXY { get; set; }
}
控制器:
public class CalculatorController : Controller {
private readonly ICalculator _calculator;
[Inject]
public CalculatorController (ICalculator calculator) {
_calculator = calculator;
}
public ActionResult Calculate(int x, int y)
{
return View(new CalculatorModel(x, y, _calculator.Sum(x, y))
}
...
观点:
@model CalculatorModel
@{
ViewBag.Title = "Calculate";
}
<h4>Calculate</h4>
The calculation result for @Model.X and @Model.Y is Model.SumXY
我真正能想到的是: