Setup global settings with abstract class and reflection to avoid xml configuration file
-
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
-
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
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; }
}- 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