jak*_*don 7 structuremap structuremap3
我们正在使用StructureMap 3.1.6.186并且遇到覆盖特定插件的问题.在我们的例子中,我们有一个控制台应用程序,我们正在部署到远程服务器.控制台应用程序对实现的依赖性非常高,需要在服务器上安装一些不需要的COM对象.由于我们确实知道在控制台应用程序中实际上没有使用此特定依赖项,因此我们尝试使用我在控制台应用程序主Program.cs中创建的虚假实现来覆盖有问题的特定实现(IImageManipulator).这将减少在服务器上安装这些com对象的需要,并且不会破坏控制台应用程序.
正如你从代码和下面的评论,既不IImageManipulator的在容器构造底部的显式注册,也不容器建成后的映射注射看到显示为预期工作.有趣的是,当我在控制台应用程序中获得IImageManipulator的实例时,它为我提供了所需的FakeImageManipulator实现.但是,当我获得IEndItemService的实例时,底层实现是我不想要的另一种类型.
任何想法如何覆盖特定接口的所有实现,即使有其他注册表已注册此接口?万分感谢!
public class FakeImageManipulator : IImageManipulator
{
public Dictionary<ImageMetaDataEnum, string> GetMetaData(Image image, params ImageMetaDataEnum[] desiredMetaData)
{
throw new NotImplementedException();
}
}
public override void ProgramLogic()
{
IContainer container = new Container(registry =>
{
registry.ForSingletonOf<ITypeResolver>().Use<StructureMapTypeResolver>();
registry.ForSingletonOf<ILogger>().Use(() => MessageLogger.GetInstance());
registry.ForSingletonOf<IConfigParser>().Use(() => ConfigParser.GetInstance());
registry.Scan(scan =>
{
scan.AssemblyContainingType<ServiceRegistry>();
scan.LookForRegistries();
});
//--hoping this would override anything that was already set in another registry
registry.For<IImageManipulator>().Use(() => new FakeImageManipulator());
});
container.Model.For<IImageManipulator>().Default.EjectAndRemove();
//--hoping this would override anything that was already set in another registry
container.Inject(typeof(IImageManipulator), new FakeImageManipulator());
//--this gets back the FakeImageManipulator as expected
IImageManipulator actualImplementation = container.TryGetInstance<IImageManipulator>();
//--the implementation of IImageManipulator created in here is NOT a FakeImageManipulator like I want, but is
// instead the instance that is registered in some down stream registry
var someOtherImplementationThatUsesAnIImageManipulatorDownStream = container.GetInstance<IEndItemsService>();
Run Code Online (Sandbox Code Playgroud)
}
编辑:另请参阅此google论坛帖子,这是同一个问题:https://groups.google.com/forum/#!topic/structuremap-users/RAR_0JbOVGQ
| 归档时间: |
|
| 查看次数: |
120 次 |
| 最近记录: |