问题
I want to use Microsoft's custom file nesting option in Visual Studio to nest implementation class files with their "parent" interfaces, so in this example, I would like ReportRepository.cs
to appear nested under IReportRepository.cs
in the solution explorer. The same should be for all repositories.

I have created the .filenesting.json
file in the root of the solution, but I have no idea how to use it and documentation is very sparse.
Does anyone know how I can achieve this in Visual Studio 2019?
回答1:
As far as I can gather it is not possible to group by prefixes. You can only group by extensions or suffixes, or entire file names.
This means you can't define a rule for this. However there are 2 options:
- Define a rule for each file
- Change your naming scheme
Defining a rule for each file
To define a rule for each file you would use the fileToFile
provider.
This would look like so:
{
"help": "https://go.microsoft.com/fwlink/?linkid=866610",
"root": true,
"dependentFileProviders": {
"add": {
"fileToFile": {
"add": {
"IInvestigationRepository.cs": [ // Parent file
"InvestigationRepository.cs" // Nested file
],
"IReporterRepository.cs": [ // Parent file
"ReporterRepository.cs" // Nested file
]
...
}
}
}
}
}
Using a different naming scheme
Alternatively, if the first option is not to your liking, you could use a different naming scheme for your files, and let the classes keep their old names.
Then you could use the fileSuffixToExtension
provider like such:
{
"help": "https://go.microsoft.com/fwlink/?linkid=866610",
"root": true,
"dependentFileProviders": {
"add": {
"pathSegment": {
"add": {
".DefaultImplementation.cs": [
".cs"
]
}
}
}
}
}
This would map
IInvestigationRepository.DefaultImplementation.cs
toIInvestigationRepository.cs
,IReporterRepository.DefaultImplementation.cs
toIReporterRepository.cs
and so on.
This would also make the purpose of your files clearer, as just having the same name doesn't tell you what it's doing with the Interface, just that is has something to do with it.
If you don't want to use a notation with .
and would prefer to use -
, you can do that by replacing "pathSegment"
with "fileSuffixToExtension"
, and replacing ".DefaultImplementation.cs"
with "-DefaultImplementation.cs"
来源:https://stackoverflow.com/questions/56717419/how-to-nest-interfaces-using-file-nesting-in-asp-net-core