Avoid calling init method on each return method 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?
The first thing I don't like much is that you walk the callstack to find the caller. It's faster and more robust to find the assembly via a type that's part of that assembly. Since your methods are called on a controller, you could use controller.GetType().Assembly to find it.

The next issue is that you only store a single value and you do so in a non threadsafe fashion. I'd use a threadsafe dictionary that maps an assembly to the data instead.

Omitting details like argument validation, my code would look similar to the following:

public class ModuleInformation
{
    private static readonly ConditionalWeakTable&lt;Assembly, ModuleInformation&gt; _dict = new ConditionalWeakTable&lt;Assembly, ModuleInformation&gt;();

    private static ModuleInformation LoadInfo(Assembly assembly)
    {
        // replace with code to load the module information
        return new ModuleInformation();
    }

    public static ModuleInformation Get(Assembly assembly)
    {
        return _dict.GetValue(assembly, LoadInfo);
    }

    public string Id { get; private set; }
}

public static class ModuleInformationExtensions
{
    public static ModuleInformation GetModuleInformation(this Controller controller)
    {
        return ModuleInformation.Get(controller.GetType().Assembly);
    }

    public static string GetAreaName(this Controller controller)
    {
        return controller.GetModuleInformation().Id;
    }
}


Another dubious part of your code is:

var moduleService = new ModulesService();
_module = moduleService.GetByNameSpace(moduleNamespace);


Depending on the implementation of the ModulesService it might be a good idea to pass it in via dependency injection. Database access from static methods is usually a bad idea since the database should be injected. This is incompatible with your static design, so you might want to switch to instances which are then injected into the controllers.