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

旧巷老猫 提交于 2019-11-29 04:18:41

问题


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.


回答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:

  • http://ayende.com/Blog/archive/2008/10/05/windsor-ihandlerselector.aspx
  • http://mikehadlow.blogspot.com/2008/11/multi-tenancy-part-2-components-and.html
  • http://bartreyserhove.blogspot.com/2009/02/building-multi-tenant-applications-with.html


来源:https://stackoverflow.com/questions/5153418/what-is-the-best-practice-to-implement-multi-tenancy-in-asp-net-mvc-using-castle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!