C# 将StructureMap与派生接口一起使用
我有一个类似于以下内容的对象层次结构:C# 将StructureMap与派生接口一起使用,c#,interface,structuremap,instances,C#,Interface,Structuremap,Instances,我有一个类似于以下内容的对象层次结构: interface IVideoStream { } abstract class VideoStream : IVideoStream { } interface IVideoStreamTypeA : IVideoStream { /* Specific for type A */ } interface IVideoStreamTypeB : IVideoStream { /* Specific for type B */ } class V
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 { }
Forward<IVideoStreamTypeA, IVideoStream>();
Forward<IVideoStreamTypeB, IVideoStream>();
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从上面注入两个实例?我当前的方法是使用以下方法:
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 { }
Forward<IVideoStreamTypeA, IVideoStream>();
Forward<IVideoStreamTypeB, IVideoStream>();
但我不确定这是不是最好的解决方案我认为这是唯一的解决方案。