IoC / 依赖注入 - 如何处理上下文依赖(使用 Structuremap)
在我的应用程序中引入消息传递后,我似乎发现了一些异味。
在我的多租户应用程序中,文件系统针对每个租户进行抽象和限定范围。因此,如果服务需要创建文件,那么我们会注入一个 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...
有想法吗?
After introducing messaging in my application it seems I've found a bit of a smell.
In my multi tenant application, the file system is abstracted and scoped for each tenant. So if a service needs to create files, then we inject an instance of IFileSystem which will be scoped to the tenants directory/container.
This is achieved by configuring structuremap to construct the IFileSystem implementation by getting of a contextual object that has the current users site.
Now we need to use the filesystem when there is no context and no current user (on a background thread). Here's a simple example:
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 :(
}
}
I suppose I could change my FileSystem implementation to expose the FileSystemSettings
as a property so it can be set afterwards.
However, even doing this would still require me to construct my ISomeService object manually, which is a pain as some of my services have a number of dependencies = lots of calls to ObjectFactory.GetInstance...
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用嵌套容器 并配置嵌套容器以具有上下文的虚拟实现。
代码大致如下:
这应该设置
ISiteContext
的自定义(虚拟)实现,而不覆盖全局容器 (ObjectFactory.Container
)。当然,如果没有更多信息,我无法为您提供DummyContext
的适当实现。但这应该让你开始。You could use nested containers and configure the nested container to have a dummy implementation of your context.
The code would approximately be:
This should set a custom (dummy) implementation of
ISiteContext
without overwriting the global container (ObjectFactory.Container
). Of course, I can't give you an appropriate implementation ofDummyContext
without more information. But this should get you started.