Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to nest interfaces using File Nesting in ASP.Net Core

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.

Files in Solution Explorer

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?

like image 576
MathuSum Mut Avatar asked Jun 22 '19 16:06

MathuSum Mut


1 Answers

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.csto IInvestigationRepository.cs,
  • IReporterRepository.DefaultImplementation.csto IReporterRepository.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"

like image 104
blenderfreaky Avatar answered Sep 27 '22 23:09

blenderfreaky