3

My C# .NET solution files are a mess and I am trying to find a way of getting things in order.

I tried to put all close files together in the same folder I am creating for that purpose. For example, I put interfaces, abstract classes, and all their inherited classes at the same folder. By the way - when I do that, I need to write a "using" statement pointing to that folder so I can use those classes in other files (also a mess I guess).

  1. Is there an elegant way of doing things more clean, and not a list of files that I find very confusing?
  2. Is it a good idea to (let's say) open a abstract class file and add nested classes for all the classes derived from it?
  3. Is there a way of telling the solution to automatically set the folder "using" statements above every class I create?
alex
  • 6,818
  • 9
  • 52
  • 103
galhajaj
  • 125
  • 1
  • 10

4 Answers4

2

The best way is when your solution file system structure reflects your program architecture and not your code architecture.

For example: if you define an abstract class and after have entities that implement it: put them into the same "basket" (solution folder) if they make a part of the same software architectual unit.

In this case one by looking on your solution tree can see what is your architecture about (more or less) from very top view.

There are different ways to enforce the architecture vision, understanding and felling of the code file system. For example if you use some known frameworks, like NHibernate, or (say) ASP.NET MVC tend to call the things in the name the technolgy calls them, in this way one who is familiar with that technology can easily find itself in your architecture.

For example WPF force you define in code things in some way, but also you need to define byb the way Model, ModelView, View.. which you will do intuitively in seprate files. The technology enforcce you to define your file system in way it was thought.

By the way the topic you're asking for, is broad known dilema/question, not resolved, cuase the code is just characters sequence and nothing else.

Good luck.

Tigran
  • 61,654
  • 8
  • 86
  • 123
1

It sounds like you're hitting the point where you actually need to break things up a bit, but you're resisting this because more files seems like more complexity. That's true to a point. But there's also a point where files just become big and unmanageable, which is where you might end up if you try to do nested classes.

Keeping code in different namespaces is actually a good thing--that's the "issue" you're running into with the folders and having to add using statements at the top of your files. Namespacing allows you to logically divide your code, and even occasionally reuse a class name, without stepping on other parts of your code base.

What version of Visual Studio are you using? One little known feature of Visual Studio is that it can automatically create the using directive when you type a class name. That would eliminate one pain point.

If I was in your shoes, I'd start looking for logical places to segment my code into different projects. You can definitely go overboard here as well, but it's pretty common to have:

  • A "core" project that contains your business logic and business objects.
  • UI projects for the different user interfaces you build, such as a website or Windows Forms app.
  • A datalayer project that handles all interactions with the database. Your business logic talks to the datalayer instead of directly to the database, which makes it easier to make changes to your database setup down the road.

As your code base grows, a tool like ReSharper starts to become really important. I work on a code base that has ~1 million lines and 10 or so projects in the solution, and I couldn't live without ReSharper's go-to-file navigation feature. It lets you hit a keyboard shortcut and start typing a file name and just jump to it when it finds a match. It's sort of like using Google to find information instead of trying to bookmark every interesting link you come across. Once I made this mental shift, navigating through the code base became so much easier.

Community
  • 1
  • 1
Josh Earl
  • 18,151
  • 15
  • 62
  • 91
0

Try using multiple projects in the same solution to bring order. Seperate projects for web, entity, data access, setup, testing, etc.

IF the files are in the same namespace you won't need a using statement. If you're breaking your code into multiple projects you'll need to reference the other projects with using statements.

  1. Its up to you. Break things apart logically. Use subfolders where you deem necessary.
  2. Not sure.
  3. Yes, but you'll need to create a template. Search for tuturorials on that.
ScottLenart
  • 1,160
  • 1
  • 12
  • 15
0

1) Your solution folders should match your namespace structure. Visual Studio is set up to work this way and will automatically create a matching namespace. Yes, this requires a using for stuff in the folders but that's what it's for.

So yes, group common stuff together under an appropriate namespace.

2) Yes, subclasses should probably live in the same namespace/folder as their abstract base, or a sub folder of it. I'm not sure if you mean all in the same file? If so I would say generally not unless they're very very simple. Different files, same folder.

3) Not that I'm aware of. If you right click the classname when you use it you can get Studio to automatically resolve it and add a using (Ctrl + . also does this)

GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103