asp.net-mvc – 使用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方法中选择哪个处理程序(取决于session的值).

然后构造函数注入在控制器上正常工作:

public MyController(IService service) { ... }

所以我得到了一个有效的解决方案,但我不确定这是否是最好的方法.

意见?建议?

非常感谢.

解决方法

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....