Saturday, 15 March 2014

c# - Refactoring a static class to use with dependency injection -



c# - Refactoring a static class to use with dependency injection -

we need utilize unmanaged library in our code has static methods. i'd introduce library operation dependency in code. , apart having static methods, library has initialization method , settings method, both global. can't wrap in instance class, because if 1 instance changes setting, other instances affected, , if 1 instance gets initialized, other instances reinitialized.

i thought introducing singleton class. way in instance class, there 1 instance won't have worry changing settings or initialization. think approach? i'm pretty new dependency injection pattern , i'm not sure if singleton pattern solution? solution similar case?

edit: initialization takes parameter too, can't lock method calls , re-initialize , alter settings every time called.

edit 2: here signatures of methods:

public static void initialize(int someparameter) // parameter can changed re-initalization // reset settings default values. public static float[] method1(int somenumber, float[] somearray) public static void changesetting(string settingname, int settingvalue)

if need set settings 1 time @ start up, recommend making non-static wrapper class initialization of static class in own static constructor. way can assured happen once:

public class mywrapper { public mywrapper() { // necessary instance initialization here } static mywrapper() { unmanagedstaticclass.initialize(); unmanagedstaticclass.settings = ...; } public void method1() { unmanagedstaticclass.method1(); } }

however, if need alter settings each time phone call it, , want create instances thread-safe, recommend locking on static object don't accidentally overwrite static settings while they're still in utilize thread:

public class mywrapper { public mywrapper() { // necessary instance initialization here } static mywrapper() { unmanagedstaticclass.initialize(); } static object lockroot = new object(); public void method1() { lock (lockroot) { unmanagedstaticclass.settings = ...; unmanagedstaticclass.method1(); } } }

if need pass initialization parameters class's instance constructor, having static flag field:

public class mywrapper { public mywrapper(initparameters p) { lock (lockroot) { if (!initialized) { unmanagedstaticclass.initialize(p); initialized = true; } } } static bool initialized = false; static object lockroot = new object(); public void method1() { lock (lockroot) { unmanagedstaticclass.settings = ...; unmanagedstaticclass.method1(); } } }

if need re-initialize each time, concerned performance because re-initializing slow, other alternative (outside of dreaded singleton) auto-detect if need re-initialize , when necessary. @ to the lowest degree then, time happen when 2 threads using 2 different instances @ same time. this:

public class mywrapper { public mywrapper(initparameters initparameters, settings settings) { this.initparameters = initparameters; this.settings = settings; } private initparameters initparameters; private settings settings; static mywrapper currentownerinstance; static object lockroot = new object(); private void initializeifnecessary() { if (currentownerinstance != this) { currentownerinstance = this; unmanagedstaticclass.initialize(initparameters); unmanagedstaticclass.settings = settings; } } public void method1() { lock (lockroot) { initializeifnecessary(); unmanagedstaticclass.method1(); } } }

c# dependency-injection refactoring singleton static-classes

No comments:

Post a Comment