Named Instances and a Default Instance in StructureMap?

后端 未结 1 1582
耶瑟儿~
耶瑟儿~ 2020-12-31 12:04

In my StructureMap bootstrapping code I\'m using a custom convention to scan assemblies and add interface/implementation pairs to the object graph as named instances. Essen

相关标签:
1条回答
  • 2020-12-31 12:36

    The Add method will add instances (if you need to add named instances or add multiple instances for to use with collections/enumerations). If no explicit default is registered (using the Use method), the last instance added will become the default instance. The Use method is intended for setting the default instance. If you invoke Use multiple times, the last instance registered will become default.

    In order to set a default instance and then register further named instances you should be able to do it like this:

    registry.For(typeof(Logger)).Use(typeof(Log4Net)).Named("Log4Net");
    registry.For(typeof(Logger)).Add(typeof(Mock)).Named("Mock");
    

    This will make the Log4Net instance the default and also accessible as a named instance. The Mock instance will be available as a named instance.

    0 讨论(0)
提交回复
热议问题