在我的应用程序中引入消息传递之后,我似乎发现了一点味道。
在我的多租户应用程序中,文件系统是为每个租户抽象和限定作用域的。因此,如果服务需要创建文件,那么我们将注入一个IFileSystem实例,该实例的作用域将被限定为租户目录/容器。
这是通过配置结构图来通过获取具有当前用户站点的上下文对象来构造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 :(
}
}我想我可以更改我的FileSystem实现,以便将FileSystemSettings公开为一个属性,这样之后就可以设置它了。
然而,即使这样做,我仍然需要手动构造我的ISomeService对象,这很痛苦,因为我的一些服务有许多依赖关系=对ObjectFactory.GetInstance...的大量调用。
想法?
发布于 2012-08-11 00:52:31
您可以对嵌套容器进行使用嵌套容器和配置,使其具有上下文的虚拟实现。
守则大致如下:
using (var container = ObjectFactory.Container.GetNestedContainer())
{
container.Configure(config => {
config.For<ISiteContext>().Use<DummyContext>();
});
return container.GetInstance<TestMessageHandler>();
}这应该设置ISiteContext的自定义(虚拟)实现,而不覆盖全局容器(ObjectFactory.Container)。当然,如果没有更多的信息,我就无法给出DummyContext的适当实现。但这应该能让你开始。
https://stackoverflow.com/questions/6725717
复制相似问题