Using StructureMap, when a default concrete type is defined in one registry, can it be redefined in another registry?

拈花ヽ惹草 提交于 2019-12-12 01:30:03

问题


In the project I'm working on I have a StructureMap registry for the main web project and another registry for my integration tests. During some of the tests I wire up the web project's registry, so that I can get objects out of the container for testing.

In one case I want to be able to replace a default concrete type from the web registry with one in the test registry.

Is this possible?

How do you do it?


回答1:


In a similar situation I created a NestedContainer (Container.GetNestedContainer()) and overwrote the type there. I then use the nested container to resolve the type.

That keeps the original configuration intact but allows for such overriding.




回答2:


Yes you can, the container will serve up which ever type is last defined (according to my tests). I theorize (but I am unsure) that my mistake was that I called the IInitializationExpression.Scan mechanism after I registered some type and it got reregistered back to it's original setting.




回答3:


Have you tried the .With() when you use GetInstance()?

Otherwise, you also use Container.Inject().



来源:https://stackoverflow.com/questions/2805938/using-structuremap-when-a-default-concrete-type-is-defined-in-one-registry-can

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!