将 StructureMap 与派生接口结合使用
我有一个类似于以下内容的对象层次结构:
interface IVideoStream { }
abstract class VideoStream : IVideoStream { }
interface IVideoStreamTypeA : IVideoStream { /* Specific for type A */ }
interface IVideoStreamTypeB : IVideoStream { /* Specific for type B */ }
class VideoStreamTypeA : IVideoStreamTypeA { }
class VideoStreamTypeB : IVideoStreamTypeB { }
应该有 VideoStreamTypeA 和 VideoStreamTypeB 的单个实例,因为它们包装了一些资源。有些类直接使用 IVideoStreamTypeA 或 IVideoStreamTypeB,有些类则获取 IVideoStream 列表。
注册代码如下所示:
class MyRegistry: Registry
{
public MyRegistry()
{
For<IVideoStreamTypeA>().Use<VideoStreamTypeA>()
.Ctor<>() // Specific initialization
For<IVideoStreamTypeB>().Use<VideoStreamTypeB>()
.Ctor<>() // Specific initialization
For<IVideoStreamTypeA>().Singleton();
For<IVideoStreamTypeB>().Singleton();
}
}
最后,有一些类采用 IVideoStream 列表:
class MyClass
{
public MyClass(IEnumerable<IVideoStream> streams) { }
}
在当前注册代码中,“streams”参数为空。如何让 StructureMap 从上面注入两个实例?
I have an object hierarchy similar to the following:
interface IVideoStream { }
abstract class VideoStream : IVideoStream { }
interface IVideoStreamTypeA : IVideoStream { /* Specific for type A */ }
interface IVideoStreamTypeB : IVideoStream { /* Specific for type B */ }
class VideoStreamTypeA : IVideoStreamTypeA { }
class VideoStreamTypeB : IVideoStreamTypeB { }
There should be a single instance of both VideoStreamTypeA and VideoStreamTypeB, as they wrap some resources. Some classes consume IVideoStreamTypeA or IVideoStreamTypeB directly, and some classes take a list of IVideoStream.
The register code looks like this:
class MyRegistry: Registry
{
public MyRegistry()
{
For<IVideoStreamTypeA>().Use<VideoStreamTypeA>()
.Ctor<>() // Specific initialization
For<IVideoStreamTypeB>().Use<VideoStreamTypeB>()
.Ctor<>() // Specific initialization
For<IVideoStreamTypeA>().Singleton();
For<IVideoStreamTypeB>().Singleton();
}
}
Finally, there are some classes that take a list of IVideoStream:
class MyClass
{
public MyClass(IEnumerable<IVideoStream> streams) { }
}
With the current registry code, the "streams" parameter is empty. How do I get StructureMap to inject the two instances from above?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我目前的方法是使用以下方法:
但我不确定这是最好的解决方案
My current approach is to use the following:
But I'm not sure it's the best solution