使用 Castle Windsor 在 ASP.NET MVC 中实现多租户的最佳实践是什么?
我有一个具有两种不同实现的服务,我想注入到控制器构造函数中,具体取决于一个标准(目前该标准是存储在会话中的一个简单值)。
这是我现在得到的...
服务接口:
public interface IService
{
string GetSampleText();
}
实现 #1:
public class FirstService : IService
{
string GetSampleText()
{
return "First Service";
}
}
实现 #2:
public class SecondService : IService
{
string GetSampleText()
{
return "Second Service";
}
}
在 Windsor 安装程序类中注册:
container.Register(AllTypes
.FromAssemblyInDirectory(new AssemblyFilter(HttpRuntime.BinDirectory))
.BasedOn<IService>()
.WithService.FromInterface()
.Configure(c => c.LifeStyle.Transient));
container.Kernel.AddHandlerSelector(new ServiceHandlerSelector());
我已经实现了一个 IHandlerSelector:
public class ServiceHandlerSelector : IHandlerSelector { ... }
在此 IHandlerSelector 实现的 HasOpinionAbout 方法中,我可以决定将选择哪个处理程序SelectHandler 方法(取决于会话中的值)。
然后构造函数注入在控制器上运行良好:
public MyController(IService service) { ... }
所以我得到了一个可行的解决方案,但我不确定这是否是执行此操作的最佳方法。
意见?建议?
非常感谢。
I have a service with two different implementations and I would like to inject into the controllers constructor, depends on a criteria (at the moment the criteria is a simple value stored in session).
Here is what I got now...
Service interface:
public interface IService
{
string GetSampleText();
}
Implementation #1:
public class FirstService : IService
{
string GetSampleText()
{
return "First Service";
}
}
Implementation #2:
public class SecondService : IService
{
string GetSampleText()
{
return "Second Service";
}
}
Registration in a Windsor installer class:
container.Register(AllTypes
.FromAssemblyInDirectory(new AssemblyFilter(HttpRuntime.BinDirectory))
.BasedOn<IService>()
.WithService.FromInterface()
.Configure(c => c.LifeStyle.Transient));
container.Kernel.AddHandlerSelector(new ServiceHandlerSelector());
I have implemented an IHandlerSelector:
public class ServiceHandlerSelector : IHandlerSelector { ... }
In the HasOpinionAbout method of this IHandlerSelector implementation I can decide which handler will be selected in the SelectHandler method (depends on the value from session).
Then the constructor injection is working fine on the controllers:
public MyController(IService service) { ... }
So I got a working solution, but I am not sure if it is the best way to do this.
Opinions? Suggestions?
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用处理程序选择器走在正确的轨道上。这里有一些关于使用它们进行多租户的好文章,您可以参考它们:
You're on the right track with handler selectors. Here are some good articles on using them for multi-tenancy, you can use them for reference: