我在v1,v2和(很快)v3中有許多MVC.NET應用程序。MVC.NET + Ninject DI到控制器中
許多應用程序使用存儲庫模式(亞音速),每個控制器如下注入存儲庫;
public class DefaultController : Controller
{
private readonly iRepository repo;
public DefaultController() : this(new Repository()) { }
public DefaultController(iRepository repository) { repo = repository; }
//
// GET: /Default/
public ActionResult Index()
{
ViewData["HelloWorld"] = repo.GetData("test");
return View();
}
}
Ninject(或simlar)會允許我只建立一次鏈接(在我的全局asax中)以及這個樣子是什麼樣的?
除了在創建新的構造函數時節省時間,還有其他好處嗎?
你認真地用小'i'作爲你的接口前綴?這不完全是「標準」。 – 2011-03-04 09:00:37