2
我使用MVC4和DependencyResolver與Unity。就像Brad Wilson的博客頁面http://bradwilson.typepad.com/blog/2010/07/service-location-pt3-views.html一樣,我想爲我的視圖使用依賴注入。但似乎MVC4視圖引擎不會嘗試通過DependencyResolver創建視圖頁面類。是否,或者我做錯了什麼? 這裏是我的代碼:查看頁面dependencyresolver
public static void Register() {
var unity = new UnityContainer();
unity.RegisterType<SpaceProject.Models.SpaceShipEntities>(new RequestLifetimeManager());
DependencyResolver.SetResolver(new UnityDependencyResolver(unity));
}
public abstract class SharedView : WebViewPage
{
[Dependency]
public SpaceProject.Models.SpaceShipEntities Context { set; get; }
}
和我_Layout.cshtml:
@inherits SharedView
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head></head>
<body>
@foreach (var menu in Context.Menu) {
<li>
<a href="#">@menu.Title</a> |
</li>
}
</body>
</html>
我有錯誤這方面是零。