c# - When working base and derived classes what is the best way to deal with AppSettings, that will allow for testability -


i hope decent question, background have abstract base class, base class has concrete method implementations accessible derived classes. base class , of derived classes have set of unique variables. of common functionality in concrete methods of base class (which not virtual cannot overridden) each of derived classes getting of these values appsettings in app.config file.

so question this, best place put these const values following best practices , allow code testable (this last piece important)?

the 2 options know are:

1) create static config class has of values each of classes. great normal consts, retrieving app.config values fail unless, either test project has values in own app.config file (this seems dirty hack me)

2) other option declare them , values settings in using class, adheres declare use principle, makes class un-testable unless again add values app.config of test project.

public abstract class baseclassa {      private const string path = "\parentclasspath\" // option 2     private int _var1;     private string _var2;     public baseclassa(int param1, string param2)     {         _var1 = param1;         _var2 = param2;     }     public int param1prop { get; private set;}     public string param2prop { get; private set; }      protected string method1(string value1, string value2, string value3)     {         directory.createdirectory(staticclass.path); //option 1          return path.getdirectoryname(path) //option 2     } }  public class derivedclassb              : base(1, "param2") {      private const string path = "\derivedclasspath\" // option 2      public baseclassa(int param1, string param2)     {         _var1 = param1;         _var2 = param2;     }     public int param1prop { get; private set;}     public string param2prop { get; private set; }      protected string method1(string value1, string value2, string value3)     {         directory.createdirectory(staticclass.derived_path); //option 1          return path.getdirectoryname(path) //option 2     } } 

create custom class wraps call configurationmanager.appsettings["key"] interface. mock interface in test can define values need test. rough example explain (untested):

public interface iconfigurationservice {     string get(string key); }   public class configurationservice :iconfigurationservice {     public string get(string key)     {         return configurationmanager.appsettings[key];     } } 

in tests can mock interface such:

public void naivetest() {     var key = "somekey";     var result = "somevalue";     var mockconfigurationservice = new mock<iconfigurationservice>();     mockconfigurationservice.setup(x => x.get(key)).returns(result);     // pass service class being tested , continue } 

include configuration service in base class, each derived class can pull values needed. can test value through use of mocks. more here: https://github.com/moq/moq4

please let me know if need further advice.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -