Here is an example of how to write and use a NanoContainer.NET script written in XML.
The code below demonstrates how to registered classes with the Container through NanoContainer.NET Xml scripts. As with the Attributes Usage Example the MovieFinder and MovieLister example Martin Fowler described in his article Inversion of Control Containers and the Dependency Injection pattern is utilized for this Xml script example.
The XML Script
A few important comments on the above example:
- Xml Scripts are parsed and the associated C# code is generated. This generated code is then compiled and executed dynamically at runtime.
- All assemblies required to compile this script must be referenced under the assemblies section.
- Notice how the
keyattribute on the second
component-implementationnode uses the
typeof()syntax. When this script is converted into C# code that
typeof()statement will be included as a snippet in the generated code. Meaning that the
ColonMovieFinderwill be stored under the
Type IMovieFinder. If the
typeof()was not included the
ColonMovieFinderwould have been stored under the key as the string value "IMovieFinder".
- the body of the
parameternode will be added verbatim to the generated C# code (as a snippet). Now since this
parameternode does NOT include the attribute "key" this parameter is a
ConstantParameter. So the string value "movies1.txt" will be used. Any valid C# syntax could be embeded within a constant parameter node. For example: new StringBuilder(), 10, 99.9f, etc...
For full details and examples of how to use the Xml syntax you would probably be best suited to look at the comprehensive test cases provided with NanoContainer.NET