简体   繁体   English

使用Castle Windsor在ASP.NET MVC中实现多租户的最佳实践是什么?

[英]What is the best practice to implement multi tenancy in ASP.NET MVC using Castle Windsor?

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: 实施#1:

public class FirstService : IService
{
    string GetSampleText()
    {
        return "First Service";
    }
}

Implementation #2: 实施2:

public class SecondService : IService
{
    string GetSampleText()
    {
        return "Second Service";
    }
}

Registration in a Windsor installer class: 在Windsor安装程序类中注册:

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: 我已经实现了一个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). 在此IHandlerSelector实现的HasOpinionAbout方法中,我可以确定将在SelectHandler方法中选择哪个处理程序(取决于会话中的值)。

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. 非常感谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM