How do I use StructureMap with generic unclosed types using Scan with a “greedy” constructor

本小妞迷上赌 提交于 2019-11-30 02:44:47

Are there any concrete implementations of IToCsvService? Or just the open type ToCsvService?

ConnectImplementationsToTypesClosing is for connecting something like a concrete CustomerViewModelToCsvService to IToCsvService<>. If you want to connect open classes to open interfaces, you'll need:

For(typeof(IToCsvService<>)).Use(typeof(ToCsvService<>));

Here I'm connecting the open interface type to the open class type.

Actually in the current version it should be very simple. All you have to do is provide the argument when you call to get a new instance of the object. To do this you use the "With" method on the objectfactory.

This allows you to use the greedy constructor. However, it also means you have to know that you need the collection in this example. So it is not the optimal method of injecting state.

var newObject = ObjectFactory.With<ICollection<CustomerViewModel>>(SomeCollection)
                .GetInstance<IToCsvService<CustomerViewModel>>();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!