올바르게 컨트롤러를 만들려면 구조 맵을 얻으려고합니다. DI를 사용하여 INewsService를 NewsController에 주입하고, 가지고있는 유일한 생성자를 만듭니다.ASP.NET MVC, MVCContrib, Structuremap, 컨트롤러 팩터로 작동합니까?
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를 주입 할 것 같지 않습니다 나는 오류 이 객체에 대해 정의 된 매개 변수가없는 생성자 수 .
내가 무엇을 놓쳤는가?
고마워!,이게 많은 도움이 되었어. 내 문제를 해결할 수 있었다. :) –
@Erv 어떤 네임 스페이스가 Scan에 있습니까? 나는 사용 또는 참조가 누락 된 것 같습니다. – ahsteele
우리가 사용하고있는 버전 (8 개월이 지났습니다. 최근 변경된 사항을 보지 않았습니다)에서 Scan은 RepositoryRegistry가 예제에서 상속 한 레지스트리 기본 클래스의 메소드입니다. –