1

I'm using structuremap 4.5.1. I have two instances of an interface, and I need to know how to retrieve a specific one in my constructor. So in this case, I have one class that requires one of the instances of my interface, then I have a different class that requires the second instance of my interface, but not the first instance. Since interfaces are commonly used to be implemented in multiple classes, I feel like this has to be a common scenario, but I can't understand how to do it.

I found a concept called "Named Instances" in the documentation, so I would have thought that was the solution, but it doesn't work.

Here is my registry:

    For<IFileWatcher>().Add<Watcher>().Named("fileWatcher");
    For<IFileWatcher>().Add<InboundWatcher>().Named("inboundWatcher");

    For<IFileWatcher>().Use<Watcher>();

And here is my constructor, that doesn't work (when I say it doesn't work, I mean structuremap only supplies the default instance, even though I named the constructor parameter the same as in my registry):

private readonly IFileWatcher _fileWatcher;

public InboundWatcherController(IFileWatcher inboundWatcher) //<-- notice the name is the same as in my registry
{
    _watcher = inboundWatcher; //<-- this is set to an instance of "Watcher", instead of "InboundWatcher" even though the name of the parameter is "inboundWatcher"
}

So, how do I get an instance of "InboundWatcher" instead of "Watcher" in my constructor? Do I have to use the container? That seems really backwards to have to bring in the container just so I can get a specific instance of a class.

Tyler Jones
  • 1,283
  • 4
  • 18
  • 38

0 Answers0