-1
\$\begingroup\$

When ever I load a new class I measure the time it takes to setup, initializing and load that class, it helps me debug the time it takes to complete actions within my application.

Here I have a pretty nice feature that does it all for me.

public static T CreateInstanceOf<T>() where T : new()
{
    var stopwatch = Stopwatch.StartNew();
    var result = new T();

    stopwatch.Stop();
    Logger.Trace("Loaded " + result.GetType().Name + " [took " + stopwatch.ElapsedMilliseconds + "ms]");

    return result;
}

It also has really simple usage, like so..

ConfigHandler = CoreUtilities.CreateInstanceOf<ConfigHandler>();

But, the issues come when I want to load the class. Now you could say that I could put this in the constructor and have my above method measure the time the method takes as well as initiating the class, but as C# has stated, constructors aren't for calling methods.

While I want to measure the time effiently, I also don't want to break any language rules that are recommended to follow, can anyone think of a work around.

Here is all I do to load the classes, just a simple method.

ConfigHandler.Load("resources/config/server.config.json");

I could just add a stopwatch between every 2 lines of everything I initialize, but lets be honest, who wants to do that, especially when you have around 30 classes to initiate in your project.

\$\endgroup\$
  • \$\begingroup\$ Create a delegate that can be executed after initializing the class. \$\endgroup\$ – Nkosi Jan 25 '18 at 1:48
  • \$\begingroup\$ A code example would help with this, I'm not entirely sure of your concept. \$\endgroup\$ – ropuxil Jan 25 '18 at 1:48
  • 2
    \$\begingroup\$ can anyone think of a work around for sure but you did not implement it yourself yet and Code Review is not a give meh teh codez site but an improve meh codez one thus I'm voting to close for code not yet written. \$\endgroup\$ – t3chb0t Jan 26 '18 at 5:49
2
\$\begingroup\$

Create a delegate that can be executed after initializing the class.

public static T CreateInstanceOf<T>(Action<T> configure = null) where T : new() {
    var stopwatch = Stopwatch.StartNew();

     var result = new T();       

    if(configure != null) {
        configure(result);
    }

    stopwatch.Stop();
    Logger.Trace("Loaded " + result.GetType().Name + " [took " + stopwatch.ElapsedMilliseconds + "ms]");

    return result;
}

and used

var handler = CoreUtilities.CreateInstanceOf<ConfigHandler>(_ => 
    _.Load("resources/config/server.config.json")
);

Having the optional parameter means your original use case still applies

CoreUtilities.CreateInstanceOf<ConfigHandler>(); //parameter defaults to null

Room for possible improvements like async overload as well

public static async Task<T> CreateInstanceOfAsync<T>(Func<T, Task> configure = null) where T : new() {
    var stopwatch = Stopwatch.StartNew();

    var result = await Task.Run(() => return new T());
    if(configure != null) {
        await configure(result);
    }

    stopwatch.Stop();
    Logger.Trace("Loaded " + result.GetType().Name + " [took " + stopwatch.ElapsedMilliseconds + "ms]");

    return result;
}

used like

var handler = await CoreUtilities.CreateInstanceOf<ConfigHandler>(_ => 
    _.LoadAsync("resources/config/server.config.json") //Assuming LoadAsync returns Task
);
\$\endgroup\$
  • \$\begingroup\$ For some reason the improvement isn't working. Here is a snapshot of the error inside of visual studio. i.imgur.com/d734TAa.png \$\endgroup\$ – ropuxil Jan 25 '18 at 2:04
  • \$\begingroup\$ @ropuxil You are using the async version. That will work if you are using Task. Use the first suggestion. The second one is if you are using async/await syntax. \$\endgroup\$ – Nkosi Jan 25 '18 at 2:07
  • \$\begingroup\$ @ropuxil I've renamed the second one to avoid any confusion. \$\endgroup\$ – Nkosi Jan 25 '18 at 2:07
  • \$\begingroup\$ Would changing my methods to async and using the second one be an improvement over the first one? \$\endgroup\$ – ropuxil Jan 25 '18 at 2:11
  • \$\begingroup\$ It allows for non blocking calls in case the loading take some time to invoke. It will keep the UI responsive and not cause it to hang. \$\endgroup\$ – Nkosi Jan 25 '18 at 2:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.