DotNet DependencyInjection

The issue

In the past I ve used and abused Dependency Injection with Biztalk ESB Toolkit, MEL and .NET . Today I want to use Dependency Injection with Core .Net. I m using .Net 7.0. My configuration must be dynamic, in a config file. I want to be able to add functionalities to my code by adding new assemblies.

References

A simgle Refenrece has been added to the project : Microsoft.Extensions.DependencyInjection

Config File

To make it very simple, I have just added a new section components that will contain a collection of name-value pairs. each entry contains a Key, which is the name I will use to reference the class I want to use to initiate in my code, and a value which contains the name of the assembly and the fullname of the class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
	<configSections>
		<section name="components"   type="System.Configuration.NameValueSectionHandler" />
	</configSections>
	<components>
		<add key="Checker" value="Project.Name,project.namespace.classname1" />
		<add key="Processer" value="Project.Name,project.namespace.classname2" />
	</components>
</configuration>

In this example, the assembly is Project.Name.dll. And the class project.namespace.classname1.

Reading the config file

In my application, I will read the config file and initialize a dictionnary with the name of the component as Key, and a structure of data with the Name, the Assembly and the ClassName. The Name could be removed. The GetConfig method is called only once, all the

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
        private struct FactoryItem
        {
            public string Name;
            public string Assembly;
            public string Class;
        }
    ...
        private static Dictionary<string, FactoryItem> _components_ = null;
        private static void GetConfig()
        {
            NameValueCollection pending = null;
            string[] strings;

            if (null != ConfigurationManager.GetSection("components"))
            {

                if (null == _components)
                    _components = new Dictionary<string, FactoryItem>();

                pending = ConfigurationManager.GetSection("components") as NameValueCollection;

                foreach (string key in pending.AllKeys)
                {
                    strings = pending[key].ToString().Split(',');
                    _components.Add(key, new FactoryItem()
                    {
                        Name = key,
                        Assembly = strings[0],
                        Class = strings[1]
                    });
                }
            }
            else
                throw new Exception("Missing components section in config file");

            pending = null;
        }     

Factory Create

In my factory, I have created a method to Instatiate dynamically my classes, my constructors requiring some parameters.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
        public static T Create<T>(string assemblyName, string className, Dictionary<string, string> parameters, Dictionary<string, string> context, List<string> result) where T : class
        {
            Assembly assembly = Assembly.Load(assemblyName);
            T? o = assembly.CreateInstance(className, true, BindingFlags.Default, null, new object[] { parameters, context, result }, null, null) as T;

            if (null == o)
            {
                throw new Exception($"Unable to create instance of {className} from {assemblyName}");
            }
            return o;
        }

The parameters required must be provided in : new object[] { parameters, context, result } Next optimization is to keep a list of assemblies already loaded.