.net - How can I safely convert a static class to an instantiated class in C# -


i have (a couple of months ago) changed jobs , have inherited codebase violates every single 1 of solid principles many times possibly can. seems though people wrote code decided study every single coding practice in detail , violate them , radically possibly could.

i sole developer of product - there no 1 left in organisation knows code , codebase large , complex rewrite. looking @ highest value changes can make make codebase flexible , robust. not option abandon product either.

the root of problems in product stems group of classes core business logic data structures. there lot of problems these classes, interested in following:

public static class netcollection {     private static logger logfile { { return logger.getmethodlogger(2); } }     // declare local variables.     private static dictionary<string, netobject> netobjecthashtable;     private static dictionary<string, nettitle> titlepropertyhashtable;     private static dictionary<string, netobject> referencedatahashtable;     private static dictionary<int, sorteddictionary<string, int>> picklisthashtable;      public static ienumerable<netobject> netobjects     {                 {             return netobjecthashtable.values;         }     }      static netcollection()     {         netobjecthashtable = new dictionary<string, netobject>();         titlepropertyhashtable = new dictionary<string, nettitle>();         referencedatahashtable = new dictionary<string, netobject>();         picklisthashtable = new dictionary<int, sorteddictionary<string, int>>();     }      public static void addnetobject(netobject newobject)     {         if (newobject == null)             return;         if (newobject.titletype == "reference data")         {             // check if hash table contains key             if (!referencedatahashtable.containskey(newobject.id.tostring()))             {                 referencedatahashtable.add(newobject.id.tostring(), newobject);             }         }         else         {             // check if hash table contains key             if (!netobjecthashtable.containskey(newobject.id.tostring()))             {                 netobjecthashtable.add(newobject.id.tostring(), newobject);             }         }     } } 

i have snipped quite number of other methods class sake of brevity.

as can see, there huge number of issues around class (storing state in static class huge code smell - writing entire application around said class crazy).

my current intention refactor class proper singleton class (and regular class can enable user open multiple documents simultaneously).

should this?

what biggest risks making change? there approaches can take mitigate risk of making change?

if don't know how type flows in application, it's dangerous task. if really need without, possibly, breaking everything, like:

knowing need distinct devision between documents, , know (it's proved time) type works single document, let's add document slice.

let's assume document has name property, can think (example):

public static void addnetobject(string documentname, netobject newobject) {     .... } 

make fields non static:

   //no static     ...     private logger logfile { { return logger.getmethodlogger(2); } }        private dictionary<string, netobject> netobjecthashtable;     private dictionary<string, nettitle> titlepropertyhashtable;     private dictionary<string, netobject> referencedatahashtable;     private dictionary<int, sorteddictionary<string, int>> picklisthashtable; 

move them internal

private class netdocument {        public string documentname {get;set;} //define document related !        ...        private logger logfile { { return logger.getmethodlogger(2); } }           private dictionary<string, netobject> netobjecthashtable;        ....    } 

so create concrete isolation between single documents , related them data.

after inside main class yo can have:

public static class netcollection {     ...       //key: document name     //value: netdocument data     private dictionary<string, netdocument> documents = new .... } 

this general idea (a sketch) , sure need change fit needs.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -