简体   繁体   中英

How can categorize properties in a class?

Assume we want to define a class that contains all constants that we will use in our solution. And this class doesn't have any methods.

public class _const
{
    /// Group1: 'DataBase'. We naming "db" group
    public const Server_Name = 'ServerName';
    public const DB_Name = 'DBName';
    public const DB_User = 'UserName';
    public const DB_Password = 'Password';

    /// Group2: 'Default Variable'. We naming "default" group
    public const Title= 'DefaultTitle';
    public const KeyWord = 'DefaultKeyWord';

    /// Group3: 'Status' constans.We naming "status" group
    public const Approved = 'Approved';
    public const Rejected = 'Rejected';
    public const Suspended = 'Suspended';

    /// And so on...
    /// ...
    /// ...
    /// ...
}

We know, these constants will be used anywhere in the solution simply with below instruction. And don't need to create an instance of the class.

/// For example:
string x = _const.Approved;

My question is: for easier use and more code readability, is there any way for grouping related constant together? And access to each group with own prefix name (like namespace)?

Something similar to the following code.

/// For example:
string x = _const.db.DB_Name;
string y = _const.default.KeyWord;
string z = _const.status.Approved;

I think a mix of Enum and Dictionary could help you since, though with a little more verbose call, it assures consistency throughout your code:

public enum Db
{
    Server_Name,
    DB_Name,
    DB_User,
    DB_Password,
}


public enum Default
{
    Title,
    Keyword
}


public enum Status
{
    Approved,
    Rejected,
    Suspened
}

public static class _const
{
    public static Dictionary<Db, string> db = new Dictionary<Db, string>()
    {
        {Db.Server_Name, "ServerName"},
        {Db.DB_Name, "DBName"},
        {Db.DB_User, "UserName"},
        {Db.DB_Password, "Password"}

    };


    public static Dictionary<Default, string> defaults = new Dictionary<Default, string>()
    {
        {Default.Title, "DefaultTitle"},
        {Default.Keyword, "DefaultKeyWord"}
    };


    public static Dictionary<Status, string> status = new Dictionary<Status, string>()
    {
        {Status.Approved, "Approved"},
        {Status.Rejected, "Rejected"},
        {Status.Suspened, "Suspended"}
    };
}

that you can use in your code like:

string string_x = _const.db[Db.Server_Name];
string string_y = _const.defaults[Default.Keyword];
string string_z = _const.status[Status.Suspened];

You can make your class static and define another nested static class like this:

public static class Constants
{
    public static class Group1
    {
        internal const string String1 = "String1";
    }
}

internal class Program
{
    internal static void Main()
    {
        Console.WriteLine(Constants.Group1.String1);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM