I am writing a plugin as part of the plugin architecture. The way to create plugins is through reflection and CreateInstance . Therefore, the default constructor is called. I cannot touch this code, and I am trying to find a reasonable way to use DI without the ability to use a framework.
I believe that I have 3 options:
i) Poor DI (PMDI)
ii) Factory Template
iii) TinyIOC or similar (one cs file that processes DI)
I started to study PMDI, but then the dependent needed another dependency, so I got something similar to this, which was ugly and could get worse:
public MyMainPluginClass() : this(new Repo(new Logger())) { } public MyMainPluginClass(IRepo repo) { }
Then I moved on to the idea of ββthe Factory template, but could not find a decent demo code. I assume that I will have something like this:
public static FactoryUtility { public static IRepo GetRepo() { return new Repo(GetLogger()); } public static ILogger GetLogger() { return new Logger(); } } public MyMainPluginClass() : this(FactoryUtility.GetRepo()) { } public MyMainPluginClass(IRepo repo) { }
What does it look like?
Then I stumbled upon TinyIOC , which is one class that does all the registration of dependencies, but I believe that this requires installing in the program .cs, which I do not have in the class library. If anyone has experience using this, it can be used like this:
public MyMainPluginClass() { var container = TinyIoCContainer.Current; container.AutoRegister(); var implementation = container.Resolve<IRepo>(); MyMainPluginClass(implementation); } public MyMainPluginClass(IRepo repo) { }
Are there any alternative approaches to achieve DI without using a third-party library, and if any approach were not chosen from the above?
NOTE. . The above code has not been compiled and is just an idea of ββwhat, in my opinion, will work. Please make corrections if they are valid.