I am going to use StructureMap as a way to do DI. I’ll use xml as my DI configuration. I have two questions.
The first one is how to use a customaized xml file name instead of StructureMap.Config? For example, in my console application, if I want to use myDIStructure.config file, what class and method should I use in the main():
private static void main(string[] args) { //? OjbectFactory.UserConfigurationFile = 'myDIStructure.config'; Program program = ObjectFactory.GetInstance<Program>(); ... }
The second question is how I define generic interface with a class type in my configuration file. For example, I have the following interface and class:
public interface ProcessData<T> { void ReadData(T data); void ProcessData(T data); void SaveData(T data); } ... public class Data1 {....} // this class will be used for ProcessData<T> .... public class MyProcessData : ProcessData<Data1> {....} // implementation class
Then in my console application, I would like to use PlugInFamily for ProcessData interface type, and Plugin for MyProcessData. Not sure if this is possible in StructionMap and how?
First – I’m increasingly of the opinion that the xml configuration option is now mainly left for backwards compatibility. The main StructureMap code now uses the code-based registry approach.
Re using custom files; I haven’t tried it, but
StructureMapConfiguration.IncludeConfigurationFromFile(...)looks promising.Re generics… indeed, that is one of the complexities of xml ;-p
Edit: based on this post in the user group, it looks like maybe the back-tick notation will work: