I'm implementing a module system into one of mine ASP.NET MVC applications and I am currently writing a module helper which all the modules can use to get some basic information about itself.
I'm using the namespace of the caller to identify the module and search it up in the database and retrieve the information.
Now I want to make everything static, so I don't have to instantiate it each time.
public static class ModuleHelper
{
private static ModuleInstallations _module;
static ModuleHelper()
{
Init();
}
private static void Init()
{
var stackTrace = new StackTrace();
MethodBase method = stackTrace.GetFrame(2).GetMethod();
var calledNamespace = method.ReflectedType.Assembly.GetName().Name;
if (_module == null || calledNamespace != _module.Namespace)
{
LoadModuleInfo(calledNamespace);
}
}
private static void LoadModuleInfo(string moduleNamespace)
{
var moduleService = new ModulesService();
_module = moduleService.GetByNameSpace(moduleNamespace);
}
public static string GetPathModule(this Controller controller, string pathFile)
{
Init();
return string.Format("/Modules/{0}/{1}", _module.AreaName, pathFile);
}
public static string GetModuleAreaName(this Controller controller)
{
Init();
return _module.AreaName;
}
public static int GetModuleId(this Controller controller)
{
Init();
return _module.Id;
}
}
The problem is that a previous module could get stored into the class and I have to check on each method if it's another module. If it's another module I have to retrieve the information of it.
What would be a good solution for this problem?