A
A
Alexey Chernyavsky2020-01-09 10:27:44
ASP.NET
Alexey Chernyavsky, 2020-01-09 10:27:44

Creating an Instance class in Global.asax. Not dangerous?

There is a class library FilterPlugin.
All classes implement a certain interface.
In Global.asax, I decided to collect all Instance of these classes into a global variable for further use.
Here is the function to write Instance in global.asax:

private void FilterPluginConfig()
        {
            string[] addInAssemblies = Directory.GetFiles(HttpContext.Current.Server.MapPath("~/bin"), "FilterPlugin.dll");
            foreach (var file in addInAssemblies)
            {
                Assembly addInAssembly = Assembly.LoadFrom(file);
                foreach (var t in addInAssembly.GetExportedTypes())
                {
                    if (t.IsClass && typeof(IFilterPlugin).IsAssignableFrom(t))
                    {
                        GeneralSettings.FilterPluginsInstances.Add((IFilterPlugin)Activator.CreateInstance(t));
                    }
                }
            }
        }

Further, already in the code of the ASP.NET application itself, certain functions of these classes are performed.
More or less like this:
foreach (var filter in GeneralSettings.FilterPluginsInstances)
{
filter.GiveResult();
}

This code works great. But I have doubts that this is acceptable in principle.
What is your opinion?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Korotenko, 2020-01-09
@firedragon

State your doubts. I personally don't see any crime, unless the download fails or someone replaces FilterPlugin.dll

R
Roman, 2020-01-09
@yarosroman

The only thing, when unit testing, there may be problems when initializing your filter array. Yes, and many singletons (and the static class is actually a singleton) are considered an antipattern for this reason, the code becomes more tied, and in principle a normal solution.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question