6

When I write my APIs for applications I always follow a set pattern (who doesnt!?) that looks like this:

User (Folder/Namespace)
    User.cs
    UserDao.cs
    UserDaoFactory.cs
    UserService.cs
    UserServiceFactory.cs

I know Resharper has file templates which I already utilize, but it would be great if I didn't have to individually add each file which can get extremely tedious when you are working with upwards of 30 entities in an app. If you can't do this with resharper is there anyway to make some kind of macro with Visual Studio that will execute your resharper file templates?

1
  • 1
    That unfortunately won't work with ReSharper, you have to create your own Visual Studio templates. This link should help getting you started.
    – Nuffin
    Jan 13, 2012 at 21:25

3 Answers 3

6
  1. Create all these classes in a single file (create a file template containing the set of standard classes if you want to)
  2. Write your logic
  3. Press Shift+Alt+L to put focus on the file in Solution Explorer
  4. Press Ctrl+Shift+R to display Refactor this menu with a list of applicable refactorings.
  5. Select Move Types into Matching Files, complete the refactoring wizard, and watch the classes separating into individual files.
  6. To put all the newly created classes into a folder, select them in Solution Explorer, and again invoke Refactor this.
  7. This time, select Move to Folder.
  8. Specify the name of the new folder in the refactoring wizard, press Create this folder, and complete the wizard.
5
  • Isn't it easier to put the file into the folder before refactoring it? Like, when choosing the location for creating the new file?
    – Nuffin
    Jan 13, 2012 at 22:21
  • Starting with a new folder is absolutely fine as well, as long as you know where exactly it should be located. With ReSharper's project refactoring features, you can afford to be unsure ) Jan 13, 2012 at 22:43
  • While I appreciate this answer (+1'd) it still doesn't provide enough automation for how often I do this. I am looking for something that would be essentially right-clicking in a folder in solution explorer, choosing my file type from that menu, inputting the base class name (User in my example) and it would generate the folder and all files above plus the boilerplate code. Jan 19, 2012 at 19:00
  • 1
    I see. There's a feature request asking for this functionality in ReSharper issue tracker, and you're invited to vote for (and watch) it. No guarantee that it will be implemented in the near future but the more votes for an issue the more attention and consideration it's likely to get from the dev team. Jan 19, 2012 at 20:27
  • The feature mentioned has been added to R# 8, see my answer below
    – Rasmus
    Nov 7, 2013 at 20:46
2

Looks like R# 8 added this feature

From the Resharper Help Page

Starting from ReSharper 8.0, each file template can add more than one file when it is applied. This > may be helpful for WPF, Web and other projects where related data can be saved in different files.

Here are a few things you need to know about multi-file templates:

  • A file template always include one main file, and, optionally, any number of secondary files. So as soon as you add a secondary file to any file template you get a multi-file template.
  • When adding a secondary file, you can reference any existing file template (Add file from existing template option). Note that if the referenced template has multiple files too, then only the main file of the referenced template will be added when you apply the template.
  • The set of parameters spans all files of a multi-file template. I.e., if you use the same parameter $PARAM$ in several files of a multi-file template, it will have the same value in all related files when the template is applied.
  • If a multi-file templates has editable parameters, the Hot Spot Session, which deploys when you apply this template, will guide you through all created files where the user input is required.
0

I would consider using .tt templates with ReSharper.

  1. create master file with ReSharper.
  2. using one or many tt files to auto generate dependent (.partial.cs) files with partial keyword in it.
  3. extend generated classes if needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.