-2

I have a Configuration class that stores certain variables which serve as settings. And I'm struggling to find easiest and most optimal way to save it to file - would be weird if user had to configure it every time.

Example class:

public static Configuration 
{
   public static bool A = false;
   public static bool B = false;
   public static int C = 100;
}

Serializing collection is not an issue, but i cannot really make collection out of these variables, since they have not matching data types.

I'm certain solution is simple, but for some reason I'm stuck here.

csdev
  • 3
  • 1
  • 2
    Why not save it as a json file? There is a popular nuget package called Newtonsoft.Json that can easily save your class settings to a human-readable file, and also read them back. – Tam Bui Dec 19 '20 at 22:21
  • There is to many option, Json,Text or XML. All of them depend on what you need. Xml is an option if you want a user to be able to edit and understand the file. `Configuration` Class should contain a method `SaveChanges` that saves the changes of the class to the file. – Alen.Toma Dec 19 '20 at 22:24
  • @TamBui I will look into Json then. – csdev Dec 19 '20 at 22:27
  • @TamBui is there an easy way to serialize it to Json, or I have to add it to the json manually one by one? It's static class, can't create instance. – csdev Dec 19 '20 at 22:30
  • See top answer here for how to serialize static properties with Json: https://stackoverflow.com/questions/24336597/why-cant-json-net-serialize-static-or-const-member-variables – Tam Bui Dec 19 '20 at 22:35
  • @TamBui but static class cannot be serialized. So as I thought, i will have to make exact same copy of this class, but not static. A lot of code, but there is no other way it would seem. – csdev Dec 19 '20 at 22:43
  • A lot of code why? Just deserialize once and put the deserialized instance in a static class. Please do some research before asking questions here – Camilo Terevinto Dec 19 '20 at 22:46
  • @CamiloTerevinto because if settings class have 1000 fields, i need non-static copy, so now instead of 1000 lines i have 2000 lines. And i need to manually, line by line rewrite values from static fields to non static fields, and then serialize. – csdev Dec 19 '20 at 22:50
  • @csdev, you can easily alleviate your issues by moving your static properties into instance variables. To update your code with a simple "Find/Replace" would take just a few minutes and not be a lot of work. It would also, eliminate "a lot of code" with managing the data that you are facing by making everything static. I'll elaborate in an answer. – Tam Bui Dec 19 '20 at 23:26

1 Answers1

0

Elaborating on my comment, you're better off converting your static class into an instance class for minimizing manual coding to store/read the property values in the future. This refactoring can be done in minutes. So do that as a first step, it shouldn't take too long to do, and a simple "Find/Replace" can fix all of your declarations everywhere in your code where you previously used "Configuration".

  1. Keep your implementation static, but change to a single instance that you are accessing.
public class Configuration
{
    private static Configuration instance;
    public static Configuration Instance
    {
        get
        {
            if (instance == null)
            {
                instance = new Configuration();
            }
            return instance;
        }
        set
        {
            instance = value;
        }
    }

    public bool A { get; set; }
    public bool B { get; set; }
    public int C { get; set; }
}
  1. Do a Find/Replace where ever you declared your static class and replace "Configuration." with "Configuration.Instance.". Also, where you previously declared static properties like public static bool A; public static bool B; ... just select all of the text, do a Find/Replace and replace "static " with "".
  2. Save/Read your data
// To Save
File.WriteAllText(@"c:\temp\myconfig.json", Newtonsoft.Json.JsonConvert.SerializeObject(Configuration.Instance));

// To Read
using (var file = File.OpenText(@"c:\temp\myconfig.json"))
{
    JsonSerializer serializer = new JsonSerializer();
    Configuration.Instance = (Configuration)serializer.Deserialize(file, typeof(Configuration));
}
Tam Bui
  • 2,940
  • 2
  • 18
  • 27
  • So basically we make the class static but fool the environment to think it's not. – csdev Dec 19 '20 at 23:42
  • No @csdev, Tam did not make a static class and you shouldn't either. Static classes have [very specific purposes](https://stackoverflow.com/q/241339/22437) and configuration classes are not one of them. – Dour High Arch Dec 20 '20 at 00:19