When using lightinject, is there any way to use a string value from an external config file to tell the container to register the service as mentioned in the file? This would be in an xml file or a web.config file.
For example, within a previous project in my web.config I would have the following line
<add key="PersonRepository.Interface.IPersonRepository" value="PersonRepository.Db.DbRepository,
PersonRepository.Db, Version=1.0.0.0, Culture=neutral"/>
I could get the value through a WebConfigurationManager call. This example gets the concrete class PersonRepository.Db.DbRepository name as a string, which then can be used to resolve the dependency. This is a method I've used in the past, but can't figure out how to do the same with lightinject. Is this possible?
Apologies if this is a little muddled, this is my first proper foray into dependency injection.
Many thanks.
Hello again. I've tried for a couple of days to implement the method as you described and it's not worked. To demonstrate this succinctly;
var myString = "hello";
Type stringType = Type.GetType(myString);
container.Register<ICloneable, stringType>();
I get the error from the compiler 'stringType' is a variable, but is used like a type.
Upon further reading here;
How to use typeof or GetType() as Generic's Template?
Seems this is limitation the language. Any thoughts at all please? Many thanks.
LightInject does not work directly with XML files, but you should be able to get to both types, service type and implementing type through the configuration manager. You would then need to resolve the types using something like Type.GetType(typeName) and then register the service with the container.
That being said, I would strongly recommend that you move the configuration of the container from the config file into code (Composition root). The mapping between service and implementing types rarely change after compilation and you get the benefit of a strongly typed configuration.