'What is best way to implement fix pre-define values of List<string>?
I am working on .NET CORE 6 Renci.SshNet
library. When I read files using SftpClient.ListDirectory I also read .
& ..
as files. I want to create list of const outside service class that just deal with this list. I want to know best approach from architecture point of view i.e. using const, value object, struct, or readonly
I have manage to pull the prototype as
public static class RejectedFileName
{
private static readonly string[] FileNames = {".", ".."};
public static string[] GetNames()
{
return FileNames;
}
}
then I can use in my service class as
var filteredFiles = files.Except(RejectedFileName.GetNames()).ToList();
Solution 1:[1]
You could use a static read-only auto-property with an initializer:
public static class Rejected
{
public static string[] FileNames { get; }
= { ".", ".." };
}
Usage::
var filteredFiles = files.Except(Rejected.FileNames).ToList();
Before C# 6, the implementation required much more verbose code as explaoned here.
Solution 2:[2]
From architecture point it is hard to discuss due to inability to determine potential change/pain points and testing requirements without better domain knowledge.
In general your apporoach seems fine, personally I would use a readonly auto-property with initializer and typed with immutable collection interface, for example IReadOnlyList
:
public static class Rejected
{
public static IReadOnlyList<string> FileNames { get; } = new [] { ".", ".." };
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mm8 |
Solution 2 | Guru Stron |