Search code examples
c#class-design

Nested classes and business logic in a repository?


For my application I need to store some data in an array with the properties (string main, string[] status, int curParCount, etc.).

I am currently storing it in this class:

class Repository
{
    public static Rep[] rep = new Rep[6];
    public struct Rep
    {
        public string main;
        public string clean;
        public int curParCount;
        public int totalCount;
        public int parStart;
        public int partialStart;
        public double scrollPos;
        public int selectionStart;
        public int selectionEnd;
        public string[] status;
    }    
    public static string repName()
    {
        string name;
        if (MainWindow.repnum == 0)
        { name = "Main Text"; }
        else { name = "Repository " + MainWindow.repnum; }
        return name;
    }
    public static string getStatus(int repNum, int statNum)
    {
        return rep[repNum].status[statNum];
    }                                                                  
 }

Is this the right way for me to do this? It sure doesn't feel like it is.


Solution

  • The basic idea is fine. The implementation could be improved. In particular, I'm concerned that you have a mutable struct; either make it immutable or turn it into a class. I'd also recommend changing the public fields into public properties with automatic backing fields (and then upper-casing the names).

    edit

    Here's my version:

    class Repository
    {
        public class Rep
        {
            public string Main {get; set;}
            public string Clean {get; set;}
            public int CurParCount {get; set;}
            public int TotalCount {get; set;}
            public int ParStart {get; set;}
            public int PartialStart {get; set;}
            public double ScrollPos {get; set;}
            public int SelectionStart {get; set;}
            public int SelectionEnd {get; set;}
            public string[] Statuses {get; set;}
        }                                       
    
    
        public const int StatusCount = 6;
        public static List<Rep> Reps = new List<Rep>();
    
        public static string Name
        {
            get
            { 
                if (MainWindow.repnum == 0)
                  return "Main Text";
    
                return "Repository " + MainWindow.repnum;
            }
        }
    
        public static string GetStatus(int repIndex, int statIndex)
        { return Reps[repIndex].Status[statIndex]; }
    }