本文介绍了如何使用 StructureMap 配置文件设置命名实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我在 SO 上做了很多谷歌搜索和搜索,但找不到类似的问题或答案.I've done quite a bit of googling and searching here on SO, but couldn't find a similar question or answer.在典型的 SM 配置中,您可以为单个 PluginType 添加多个命名实例:In typical SM configuration you can add multiple named instances for a single PluginType:ForRequestedType<IFoo>() .AddInstances( x => { x.OfConcreteType<FooA>().WithName( "FooA" ); x.OfConcreteType<FooB>().WithName( "FooB" ); } );没问题.问题是我在创建配置文件时不能做同样的事情.大多数解释如何使用配置文件的示例使用传递的 ProfileExpression 的 For<>() 方法:No problem there. The problem is that I can't do the same when creating a profile. Most examples explaining how to use profiles use the For<>() method of the passed ProfileExpression:CreateProfile( "Default", p => { p.For<IFoo>().UseConcreteType<FooC>(); } );我似乎找不到一种方法来为相同的 PluginType 添加多个命名实例,就像上面使用常规配置所做的那样.通过 ProfileExpression 唯一可用的其他方法是 Type(),但我不确定它是否可以用于此目的.I can't seem to find a way to add multiple named instances for the same PluginType as you can do above with regular configuration. The only other method available through ProfileExpression is Type<>(), but I'm not sure if it can be used for this purpose.我尝试使用 Type<>() 而不是 For<>() 并且它似乎正在我朝着正确的方向前进,但我遇到了另一个问题.为了更好地解释它,这里有一个我正在尝试做的更好的例子(这是我发布到 structuremap-users 组的内容,还没有答案): I tried to use Type<>() instead of For<>() and it seems to be taking me in the right direction, but I bumped into another problem. To better explain it here's a better example of what I'm trying to do (this is what I posted to the structuremap-users group, no answer yet):ObjectFactory.Initialize( x => { x.CreateProfile( "Nissan", p => { p.Type<ICar>().Is.OfConcreteType<NewNissanCar>().WithName( "New" ); p.Type<ICar>().Is.OfConcreteType<OldNissanCar>().WithName( "Old" ); } ); x.CreateProfile( "Honda", p => { p.Type<ICar>().Is.OfConcreteType<NewHondaCar>().WithName( "New" ); p.Type<ICar>().Is.OfConcreteType<OldHondaCar>().WithName( "Old" ); } );} );ObjectFactory.Profile = "Nissan";ICar newCar = ObjectFactory.GetNamedInstance<ICar>( "New" ); // -> returns NewHondaCarICar car = ObjectFactory.GetInstance<ICar>(); // -> returns OldNissanCar因此,即使我将配置文件设置为Nissan",GetNamedInstance<>("New") 从不正确的配置文件中返回了一个实例 - 它应该返回 NewNissanCar 而不是 NewHondaCar.So even though I set the profile to "Nissan", GetNamedInstance<>("New") returned an instance from the incorrect profile - it should've returned NewNissanCar instead of NewHondaCar.有趣的是,GetInstance<>() 使用了正确的配置文件,但由于我无法传递实例名称,它从实现 ICar(我猜它只是返回为该接口添加的最后一个具体类型).Interestingly, GetInstance<>() uses the correct profile, but because I can't pass an instance name, it returns an arbitrary concrete type from that profile that implements ICar (I guess it just returns the last concrete type added for that interface).推荐答案它看起来不像 StructureMap 在配置文件级别尊重命名实例.我刚刚用 SM 2.6.3 尝试了汽车示例并得到了相同的结果.您还可以在 ObjectFactory.WhatDoIHave() 的输出中看到它:It doesn't look like StructureMap respects named instances at the profile level. I just tried the car example with SM 2.6.3 and got the same results. You can also see it in the output of ObjectFactory.WhatDoIHave():ICar (ConsoleApplication1.ICar)Scoped as: Transient "New" Configured Instance of ConsoleApplication1.NewHondaCar, ConsoleApplication1 "Old" Configured Instance of ConsoleApplication1.OldHondaCar, ConsoleApplication1看起来配置文件也可能通过使用名称在内部实现,这可以解释为什么它不受支持.当我为每个配置文件注册一个未命名的实例时,我在输出中看到:It looks like the profiles might be implemented internally by using the name as well, which would explain why it's not supported. When I registered a non-named instance for each profile, I see this in the output:ICar (ConsoleApplication1.ICar)Scoped as: Transient "Default Instance for Profile Nissan" Configured Instance of ConsoleApplication1.NewNissanCar, ConsoleApplication1 "Default Instance for Profile Honda" Configured Instance of ConsoleApplication1.NewHondaCar, ConsoleApplication1注意配置文件的名称实际上是实例键名称的一部分.我还没有完成代码,但这似乎正在发生.Notice how the name of the profile is actually part of the instance key's name. I haven't walked through the code, but this is what appears to be happening. 这篇关于如何使用 StructureMap 配置文件设置命名实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-06 01:20