有人可以解释温莎城堡在我的应用程序中如何工作吗?
我已经开始使用温莎城堡,不知怎的,我的应用程序已经全部启动并运行,但我不太明白它是如何工作的。不要让我参考文档,否则我就不会在这里。
在我的 Global.asax.cs 中,我有这个:
private static IWindsorContainer container;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
BootstrapContainer();
}
protected void Application_End()
{
container.Dispose();
}
private static void BootstrapContainer()
{
container = new WindsorContainer()
.Install(FromAssembly.This());
var controllerFactory = new WindsorControllerFactory(container.Kernel);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
}
现在这是注册一个我理解的新控制器工厂。我认为从当前程序集安装 WindsorContainer 会注册所有安装程序,例如我有一个存储库安装程序。我假设在 Global.asax 中创建的容器已传递给安装程序。
public class RepositoriesInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(AllTypes.FromThisAssembly()
.Where(type => type.Name.EndsWith("Repository"))
.WithService.DefaultInterface()
.Configure(c => c.LifeStyle.PerWebRequest));
}
}
在我的控制器中,我创建了一个构造函数并传入了 IRepository 参数。我不明白的是控制器如何接受这个论点。
其次,作为测试,我创建了 2 个实现 IRepository 的存储库类。在控制器构造函数中放置一个断点,它会传入这些类之一。如何映射应该将实现 IRepository 的类传递给构造函数?
我还启动并运行了 Fluent NHibernate。对于下一阶段,我希望 IRepository 依赖于 ISession。我该怎么做?
感谢您的帮助
I have begun using Castle Windsor and somehow my app is all up and running but I dont really understand how its working. Don't refer me to the documentation as I wouldn't be here otherwise.
In my Global.asax.cs I have this:
private static IWindsorContainer container;
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
BootstrapContainer();
}
protected void Application_End()
{
container.Dispose();
}
private static void BootstrapContainer()
{
container = new WindsorContainer()
.Install(FromAssembly.This());
var controllerFactory = new WindsorControllerFactory(container.Kernel);
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
}
Now this is registering a new controller factory which I understand. The installation of a WindsorContainer from the current assembly I think registers all installers for example I have a repository installer. I assume that the container that is created in Global.asax is passed to the installers.
public class RepositoriesInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(AllTypes.FromThisAssembly()
.Where(type => type.Name.EndsWith("Repository"))
.WithService.DefaultInterface()
.Configure(c => c.LifeStyle.PerWebRequest));
}
}
In my controller I have created a constructor and passed in a IRepository argument. What I dont understand is how the controller accepts this argument.
Secondly as a test I created 2 repository classes that implement a IRepository. Putting a breakpoint in the controller constructor it passes in one of these classes. How do I map what class that implements IRepository should be passed to the constructor?
I also have Fluent NHibernate up and running. For the next stage I would like the IRepository to have a dependency on the ISession. How do I do that?
Thanks for your help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您已经注册了一个使用 Windsor 的控制器工厂,因此 Windsor IoC 容器负责在需要时解析所有控制器实例。
也就是说,当您访问 MVC 项目中指向“HomeController”上的操作“Index”的 URL 时,MVC 框架将向您的 WindsorControllerFactory 询问 HomeController 的实例。
如果该控制器有一个接受 IRepository 实例的构造函数,并且您已将 IRepository 注册为容器的服务,那么 Windsor 将知道如何满足 HomeController 类的依赖关系。因此,它可以首先将 IRepository 解析为某个具体类,实例化它,并将其作为参数传递给 HomeController 构造函数,然后将 HomeController 实例返回到 MVC 框架。
如果您需要 IRepository 的不同实现用于不同的目的(即 UserRepository 和 ProductRepository),您可以为这些创建单独的接口,每个接口都扩展 IRepository,例如:
然后您可以使用 Windsor 的流畅注册 API 来注册实现 IRepository 的所有具体类,并通过它们提供的特定服务(例如 IProfileRepository)注册它们。
如果您这样做,Windsor 将自动为您解析实现 IRepository 的所有实例,而无需在添加新实现时编写任何新的注册代码。
至于使您的存储库类依赖于 ISession,您可以通过多种方式来实现。我建议不要让它们直接依赖于会话,而是让它们依赖于一个类,通过该类它们可以获取当前会话(以便您可以在存储库之间共享会话)。网上有很多关于为什么这是良好实践的信息,只需进行搜索即可。
现在,要真正实现它,您可以:
注册一个类的实例(通过接口),该实例将为您检索 Windsor 的会话,并让 Windsor 将该类解析为存储库构造函数的参数。< /p>
向 Windsor 注册 ISession,并在解析时使用工厂方法检索它。
Since you have registered a controller factory that uses Windsor, it is the Windsor IoC container that is responsible for resolving all your controller instances as and when they are needed.
That is, when you access a URL in your MVC project that points to the action "Index" on your "HomeController" your WindsorControllerFactory will be asked, by the MVC framework, for an instance of HomeController.
If that controller has a constructor which takes an instance of IRepository and you have registered IRepository as a service with the container then Windsor will know how to satisfy the dependency of the HomeController class. Therefore it can first resolve IRepository into some concrete class, instantiate this, and pass it in as a parameter to the HomeController constructor before returning the instance of HomeController to the MVC framework.
If you need different implementations of IRepository for different purposes (i.e. a UserRepository and a ProductRepository) you could create separate interfaces for these, each of which extend IRepository, e.g.:
Then you can use Windsor's fluent registration API to register all concrete classes that implement IRepository, and have them registered by the specific service they provide, e.g. IProfileRepository.
If you do this, Windsor will automatically resolve all instances that implement IRepository for you without you having to write any new registration code when you add a new implementation.
As for making your repository classes depend on ISession, you can do this in a number of ways. I would recommend not letting them depend directly on a session, but rather let them depend on a class through which they can obtain the current session (so that you can share sessions between repositories). There's lots of information on why this is good practice out there on the web, just do a search.
Now, as for actually making it happen, you can either:
Register an instance of a class (by interface) that will retrieve a session for you with Windsor and let Windsor resolve this class as a parameter to your repository constructors.
Register ISession with Windsor and use a factory method to retrieve it when it is resolved.