.net 假设与假设
我有一些存储库。通过接口.net 假设与假设,.net,asp.net-mvc,repository,castle-windsor,.net,Asp.net Mvc,Repository,Castle Windsor,我有一些存储库。通过接口IRepository实现与它们的协作 在HomeController中,使用ISession实现构造函数。当我写这篇文章时: public class TestingController : Controller { private ISession session; public TestingController(ISession session) { this.session = session; } //Some
IRepository
实现与它们的协作在
HomeController
中,使用ISession实现构造函数。当我写这篇文章时:
public class TestingController : Controller
{
private ISession session;
public TestingController(ISession session)
{
this.session = session;
}
//Some code here with this.session
}
它工作得很好。当我决定使用IRepository
时:
public class TestingController : Controller
{
private IRepository repository;
public TestingController(IRepository repository)
{
this.repository = repository;
}
//Some code here with this.repository
}
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
throw new HttpException(404, string.Format("The controller for path {0} culd not found!", requestContext.HttpContext.Request.Path));
}
return (IController)this.kernel.Resolve(controllerType);
}
它不适用于其他类的此方法WindsorController Factory
:
public class TestingController : Controller
{
private IRepository repository;
public TestingController(IRepository repository)
{
this.repository = repository;
}
//Some code here with this.repository
}
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
{
throw new HttpException(404, string.Format("The controller for path {0} culd not found!", requestContext.HttpContext.Request.Path));
}
return (IController)this.kernel.Resolve(controllerType);
}
我有例外
无法创建组件“MVCAPApplication1.Controllers.ResultsController”,因为它需要满足依赖关系
“MVCAPApplication1.Controllers.ResultsController”正在等待
以下依赖项:
-未注册的服务“dal.Repository.IRepository”
如何解决
p.S.组件
dal.存储库正在工作。如果我到处写new dal.Repository.Repository()
而不是this.Repository
,它会正常工作。看起来您缺少了IRepository
的绑定
您必须有具体的类来实现IRepository
接口,比如MyRepository
。然后,您应该配置Windsor,以了解IRepository
是关于什么的
大概
container.Register(Component.For<IRepository>().ImplementedBy<MyRepository>());
container.Register(Component.For().ImplementedBy());
我可以通过以下方法解决相同的问题
另一件事是,如果我们从github下载这个项目的新版本,我们将不会看到这个表达式