开发者

structuremap does return named instance instead of default one

开发者 https://www.devze.com 2023-03-22 08:45 出处:网络
As the title says, structuremap does not return the default instance when i have also configured named instances.

As the title says, structuremap does not return the default instance when i have also configured named instances.

Here is my type registration:

/// <summary>
  /// Initializes a new instance of the <see cref="CommandProcessingTypeRegistry"/> class.
  /// </summary>
  public CommandProcessingTypeRegistry()
  {
     For<ICommandProcessor>().Singleton().Use<CommandCoordinator>();

     For<ICommandProcessor>().Singleton().Use<SystemCommandSwitch>().Named(typeof(SystemCommandSwitch).FullName);
     For<ICommandProcessor>().Singleton().Use<TelephonyCommandSwitch>().Named(typeof(TelephonyCommandSwitch).FullName);
     For<ICommandProcessor>().Singleton().Use<AudioCommandSwitch>().Named(typeof(AudioCommandSwitch).FullName);
     For<ICommandProcessor>().Singleton().Use<TetraCommandSwitch>().Named(typeof(TetraCommandSwitch).FullName);
     For<ICommandProcessor>().Singleton().Use<RadioCommandSwitch>().Named(typeof(RadioCommandSwitch).FullName);
     For<ICommandProcessor>().Singleton().Use<SnapshotCommandSwitch>().Named(typeof(SnapshotCommandSwitch).FullName);
     For<ICommandProcessor>().Singleton().Use<TakeNextCommandSwitch>().Named(typeof(TakeNextCommandSwitch).FullName);
  }

And this is how i request the instance:

_commandProcessor = _container.GetInstance<ICommandProcessor>(); // _container is the structuremap IContainer instance

I would like that the above line returns me the CommandCo开发者_Python百科ordinator instance but instead the TakeNextCommandSwitch instance is returned. What am i doing wrong here?


You need to use Add instead of Use for the named instances:

For<ICommandProcessor>().Singleton().Add<TelephonyCommandSwitch>().Named(typeof(TelephonyCommandSwitch).FullName);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号