Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Setup global settings with abstract class and reflection to avoid xml configuration file

Setup global settings with abstract class and reflection to avoid xml configuration file

Scheduled Pinned Locked Moved Design and Architecture
workspacexmlarchitectureperformancequestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kannan Ar
    wrote on last edited by
    #1

    Hi All, I am developing a library component which requires some global settings. Personally I don’t like to maintain an xml configuration file for this. An idea strikes for me that to write an abstract class with an abstract method which can override and set global setting by the implementers of this library. The abstract class will look like something like this.

    public abstract class LibSetting
    {
    public int MaxCount { get; set; }

     public abstract void SetCount();
    

    }

    The end users will implement this abstract class in their application is as follows.

    public class MySetting : LibSetting
    {
    public override void SetCount()
    {
    MaxCount = 100;
    }
    }

    To get the end user setting from my library, I am using following code.

    public int ShowCount()
    {
    Assembly asm = Assembly.GetCallingAssembly();

       IEnumerable types = asm.GetTypes().Where(t => t.IsClass && t.BaseType == typeof(LibSetting));
    
       LibSetting setting =(LibSetting) asm.CreateInstance(types.First().FullName);
    
       setting.SetCount();
    
       return setting.MaxCount;
    

    }

    This will work perfectly. My doubt is that, is it feasible in architecture level of view? I think the performance of read an xml file and loading a class using reflection would be same ( I didn’t test it). So is this a good practice? Kindly advice me on this. Thanks and Regards, Kannan

    K 1 Reply Last reply
    0
    • K Kannan Ar

      Hi All, I am developing a library component which requires some global settings. Personally I don’t like to maintain an xml configuration file for this. An idea strikes for me that to write an abstract class with an abstract method which can override and set global setting by the implementers of this library. The abstract class will look like something like this.

      public abstract class LibSetting
      {
      public int MaxCount { get; set; }

       public abstract void SetCount();
      

      }

      The end users will implement this abstract class in their application is as follows.

      public class MySetting : LibSetting
      {
      public override void SetCount()
      {
      MaxCount = 100;
      }
      }

      To get the end user setting from my library, I am using following code.

      public int ShowCount()
      {
      Assembly asm = Assembly.GetCallingAssembly();

         IEnumerable types = asm.GetTypes().Where(t => t.IsClass && t.BaseType == typeof(LibSetting));
      
         LibSetting setting =(LibSetting) asm.CreateInstance(types.First().FullName);
      
         setting.SetCount();
      
         return setting.MaxCount;
      

      }

      This will work perfectly. My doubt is that, is it feasible in architecture level of view? I think the performance of read an xml file and loading a class using reflection would be same ( I didn’t test it). So is this a good practice? Kindly advice me on this. Thanks and Regards, Kannan

      K Offline
      K Offline
      Keld Olykke
      wrote on last edited by
      #2

      Hi Kannan, Your inheritance approach seems fine. I have the following things that you can ponder about: 1) Isn't the user's call to a setter a little backwards? Another approach is just to require a getter from the user's concrete class e.g.

      public abstract class LibSetting
      {
      public abstract int MaxCount { get; }
      }

      1. Furthermore, you could supply the user with good default settings e.g.

      public abstract class LibSetting
      {
      /// <summary>
      /// Default set to 100.
      /// /// Don't call base when you override this.
      /// <summary/>
      public virtual int MaxCount
      {
      get { return 100; }
      }
      }

      Hope it makes sense... Regards, Keld Ølykke

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups