patterncsharpMinor
Parsing an INI file to runtime properties
Viewed 0 times
filepropertiesruntimeparsingini
Problem
Currently I am developing an application which uses a few variables which should be changable in a .ini file. Since I want to make the application to be able to continue if the user "forgets" to correctly implement the variables (read: "retard-proof"), I figured I would have to create an object for each variable which holds a default value and an optional set value. My implementation looks like this at the moment;
IProperty.cs
Property.cs
PropertyFactory.cs
```
public static class PropertyFactory
{
private static Dictionary CreateDefaults()
{
Dictionary dic = new Dictionary();
List propList = new List();
#region String properties
propList.Add(new Property(){ DefaultValue = @"C:\...\...", PropertyName = "log_path"});
//...
#endregion
#region Int32 properties
propList.Add(new Property() { DefaultValue = 1337, PropertyName = "some_value"});
//...
#endregion
#region Boolean properties
propList.Add(new Property() { DefaultValue = false, PropertyName = "log_enabled" });
//...
#endregion
//Map to the dictionary based on their name
foreach(IProperty
IProperty.cs
public interface IProperty
{
bool IsSet { get; set; }
string PropertyName { get; set; }
object GetValue();
}Property.cs
public class Property : IProperty
{
public bool IsSet { get; set; }
public Property()
{
IsSet = false;
}
public string PropertyName { get; set; }
public T DefaultValue { get; set; }
private T _CurrentValue;
public T CurrentValue
{
get
{
return _CurrentValue;
}
set
{
IsSet = true;
_CurrentValue = value;
}
}
private T GetActualValue()
{
return IsSet ? CurrentValue : DefaultValue;
}
public object GetValue()
{
return (object)GetActualValue();
}
}PropertyFactory.cs
```
public static class PropertyFactory
{
private static Dictionary CreateDefaults()
{
Dictionary dic = new Dictionary();
List propList = new List();
#region String properties
propList.Add(new Property(){ DefaultValue = @"C:\...\...", PropertyName = "log_path"});
//...
#endregion
#region Int32 properties
propList.Add(new Property() { DefaultValue = 1337, PropertyName = "some_value"});
//...
#endregion
#region Boolean properties
propList.Add(new Property() { DefaultValue = false, PropertyName = "log_enabled" });
//...
#endregion
//Map to the dictionary based on their name
foreach(IProperty
Solution
Naming
Variables named
I also prefer to declare my variables where I'm going to be using them (e.g. s inside your first while loop) so that the declaration is never too far from the utilisation.
Var
Use
e.g.
should be
You should also always use
Lines
Lines like this:
Could do with expanding across multiple lines, it eases readability and stops you having to scroll.
Result:
Regions
Regions are controversial outside of methods, but whenever I see a region inside a method it's almost a sure-fire guarantee that the contents of that region should be refactored into its own method. You're missing some lines from your original code, so that would be a judgement call left to you.
Generics
As for your complaint:
Why on earth does
Why not just use the generic
Then call
If it's the wrong type, either throw an exception, or sort something out with null values.
Variables named
s and sr are unhelpful to a maintenance programmer, prefer longer, more explanatory names.I also prefer to declare my variables where I'm going to be using them (e.g. s inside your first while loop) so that the declaration is never too far from the utilisation.
Var
Use
var when declaring local variables when the right hand side of the declaration makes the type obvious.e.g.
Dictionary map = new Dictionary();should be
var map = new Dictionary();You should also always use
var when declaring a loop variable.foreach(var pair in values)Lines
Lines like this:
propList.Add(new Property(){ DefaultValue = @"C:\...\...", PropertyName = "log_path"});Could do with expanding across multiple lines, it eases readability and stops you having to scroll.
Result:
propList.Add(new Property()
{
DefaultValue = @"C:\...\...",
PropertyName = "log_path"
});Regions
Regions are controversial outside of methods, but whenever I see a region inside a method it's almost a sure-fire guarantee that the contents of that region should be refactored into its own method. You're missing some lines from your original code, so that would be a judgement call left to you.
Generics
As for your complaint:
if((bool)Properties.PropertyList["log_enabled"].GetValue()) //log...Why on earth does
GetValue() even exist?Why not just use the generic
GetActualValue() in your interface?public interface IProperty
{
bool IsSet { get; set; }
string PropertyName { get; set; }
T GetValue();
}Then call
GetValue() and have it return a boolean, no boxing or unboxing needed.If it's the wrong type, either throw an exception, or sort something out with null values.
Code Snippets
Dictionary<string, string> map = new Dictionary<string, string>();var map = new Dictionary<string, string>();foreach(var pair in values)propList.Add(new Property<string>(){ DefaultValue = @"C:\...\...", PropertyName = "log_path"});propList.Add(new Property<string>()
{
DefaultValue = @"C:\...\...",
PropertyName = "log_path"
});Context
StackExchange Code Review Q#71312, answer score: 3
Revisions (0)
No revisions yet.