我試圖讓結構圖正確地創建我的控制器,我使用DI注入到NewsController INEwsService和多數民衆贊成我唯一的構造函數。ASP.NET MVC,MVCContrib,Structuremap,讓它作爲controllerfactory工作?
public class NewsController : Controller
{
private readonly INewsService newsService;
public NewsController(INewsService newsService)
{
this.newsService = newsService;
}
public ActionResult List()
{
var newsArticles = newsService.GetNews();
return View(newsArticles);
}
}
,我使用此代碼,啓動應用程序
public class Application : HttpApplication
{
protected void Application_Start()
{
RegisterIoC();
RegisterViewEngine(ViewEngines.Engines);
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterIoC()
{
ObjectFactory.Initialize(config => {
config.UseDefaultStructureMapConfigFile = false;
config.AddRegistry<PersistenceRegistry>();
config.AddRegistry<DomainRegistry>();
config.AddRegistry<ControllerRegistry>();
});
DependencyResolver.InitializeWith(new StructureMapDependencyResolver());
ControllerBuilder.Current.SetControllerFactory(typeof(IoCControllerFactory));
}
}
但Structuremap似乎並不想注入INewsService和我得到的錯誤 此對象定義無參數的構造函數。
我錯過了什麼?
我在註冊表中添加所有正確的映射,例如INewsService,所以它不應該是導致問題的原因。 – 2009-02-27 15:31:11
我不是在實例化你的代碼。 – Micah 2009-02-27 15:32:53