How to override MapMap 3

We use StructureMap 3.1.6.186 and are faced with the problem of overriding a specific plugin. In our case, we have a console application that we deploy to a remote server. The console application has a very distant implementation dependency, which requires some unwanted COM objects on the server. Since we know for sure that this particular dependency is not actually used in our Console application, we are trying to override a specific implementation that is problematic (IImageManipulator), with a fake implementation that I created directly in my main console application. This would make it easier to install these COM objects on the server and not break the console application.

As you will see from the code and comments below, neither explicit registration of IImageManipulator at the bottom of the container constructor, nor display injection after container assembly works as expected. Interestingly, when I get an instance of IImageManipulator in a console application, it gives me the desired implementation of FakeImageManipulator. However, when I get an instance of IEndItemService, then the main implementation is a different type that I don't want.

Any idea to override all implementations of a particular interface, even if there are other registries that have registered that interface? Thanks a ton!

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>(); 

}

Edit: also see this google group post that asks the same question: https://groups.google.com/forum/#!topic/structuremap-users/RAR_0JbOVGQ

+7
structuremap structuremap3
source share

No one has answered this question yet.

See related questions:

5
How can I get an instance in the StructureMap Registy constructor?
2
ViewModels as handlers with MediatR, StructureMap, Caliburn.Micro
2
StructureMap: how to properly configure default dependencies
one
Define a default constructor using StructureMap without providing arguments or using the DefaultConstructor attribute
one
Force object creation in StructureMap
one
StructureMap error - default instance not registered
0
Structuremap (or any IoC, really) is an architecture issue
0
The default instance is not registered and cannot be automatically determined for the type - StructureMap
0
Passing additional constructor arguments using StructureMap
0
Overriding the StructureMap registry from another project

All Articles