Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# IoC/依赖项注入-如何处理上下文依赖项(使用Structuremap)_C#_Dependency Injection_Inversion Of Control_Structuremap_Multi Tenant - Fatal编程技术网

C# IoC/依赖项注入-如何处理上下文依赖项(使用Structuremap)

C# IoC/依赖项注入-如何处理上下文依赖项(使用Structuremap),c#,dependency-injection,inversion-of-control,structuremap,multi-tenant,C#,Dependency Injection,Inversion Of Control,Structuremap,Multi Tenant,在我的应用程序中引入消息之后,我似乎发现了一点异味 在我的多租户应用程序中,文件系统被抽象,并为每个租户确定范围。因此,如果一个服务需要创建文件,那么我们将注入一个IFileSystem实例,该实例的作用域将限定为租户目录/容器 这是通过配置structuremap来实现的,通过获取具有当前用户站点的上下文对象来构造IFileSystem实现 现在我们需要在没有上下文和当前用户(在后台线程上)的情况下使用文件系统。下面是一个简单的例子: public class SiteContext

在我的应用程序中引入消息之后,我似乎发现了一点异味

在我的多租户应用程序中,文件系统被抽象,并为每个租户确定范围。因此,如果一个服务需要创建文件,那么我们将注入一个IFileSystem实例,该实例的作用域将限定为租户目录/容器

这是通过配置structuremap来实现的,通过获取具有当前用户站点的上下文对象来构造IFileSystem实现

现在我们需要在没有上下文和当前用户(在后台线程上)的情况下使用文件系统。下面是一个简单的例子:

    public class SiteContext
    {
        public string SiteId { get { return "Site123"; } }
    }

    public class FileSystemSettings
    {
        public string BaseDirectory { get; set; }
    }

    public interface IFileSystem { }

    public class DefaultFileSystem : IFileSystem
    {
        public DefaultFileSystem(FileSystemSettings settings)
        {

        }
    }

    public interface ISomeService { }

    public class SomeService : ISomeService
    {
        public SomeService(IFileSystem fileSystem)
        {

        }
    }

    public class TestMessageHandler : IMessageHandler<TestMessage>
    {
        public TestMessageHandler(ISomeService someService)
        {
            // oO we don't have access to site context here :(
        }
    }
公共类SiteContext
{
公共字符串SiteId{get{return“Site123”;}
}
公共类文件系统设置
{
公共字符串BaseDirectory{get;set;}
}
公共接口IFileSystem{}
公共类DefaultFileSystem:IFileSystem
{
公共默认文件系统(文件系统设置)
{
}
}
公共接口服务{}
公共类SomeService:ISomeService
{
公共服务(IFileSystem文件系统)
{
}
}
公共类TestMessageHandler:IMessageHandler
{
公共TestMessageHandler(ISomeService someService)
{
//oO我们无法访问此处的站点上下文:(
}
}
我想我可以更改文件系统实现,将
FileSystemSettings
作为一个属性公开,以便以后可以对其进行设置

但是,即使这样做,我仍然需要手动构造ISomeService对象,这是一个难题,因为我的一些服务有许多依赖项=大量调用
ObjectFactory.GetInstance…

想法?

您可以创建和配置嵌套容器,使其具有上下文的虚拟实现

代码大致如下:

using (var container = ObjectFactory.Container.GetNestedContainer())
{
    container.Configure(config => {
        config.For<ISiteContext>().Use<DummyContext>();
    });

    return container.GetInstance<TestMessageHandler>();
}
使用(var container=ObjectFactory.container.GetNestedContainer())
{
container.Configure(配置=>{
config.For().Use();
});
返回container.GetInstance();
}

这应该设置
ISiteContext
的自定义(虚拟)实现,而不覆盖全局容器(
ObjectFactory.container
)。当然,如果没有更多信息,我无法为您提供适当的
DummyContext
实现。但这应该可以让您开始。

您能澄清这个问题吗?您打算如何使用SiteContext?显示有效的代码,并指出无效的代码。