lundi 8 juillet 2019

What is the best practice for storing and retrieving paths in C#?

I am refactoring some legacy code. The code makes heavy use of hard-coded file path strings. I am unsure what to do with these and, more generally, what the best practice is for storing and retrieving file paths.

Abstracting away from the details, I have an abstract base class which contains a public string property for each required file path:

public abstract class Base
{
    public string Path1 { get; set; }
    public string Path2 { get; set; }
    ⋮
    public string Pathn{ get; set; }

    public Base()
    {
    }
}

I then have several child classes inheriting from this base which set the parent path properties via their respective constructors:

public class Child : Base
{
    public Child()
    {
        Path1 = @"some\1st-path\file.cs"
        Path2 = @"some\2nd-path\file.cs"
        ⋮
        Pathn = @"some\nth-path\file.cs"
    }
}

Is there a more elegant way of storing and retrieving this kind of path data (e.g., would it be better for me to store it inside a config/settings file)?

More generally, what is generally considered to be the best practice for refactoring this sort of code?

Aucun commentaire:

Enregistrer un commentaire