Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Not sure if this follows the rules or not, but I need some help with names. How should I approach project name vs namespace vs class. For example, I want to make a datacleaner program. So I name it Datacleaner, and then DC for the class, and then Cleaner.cs for the file name, and it just gets all confusing. Is there some best practice I can be following here!? A helpful mindset or naming theory would be exceptionally helpful.
It is slightly off-topic, but I'll give you a shot here.
The first thing we need to look at, is the root namespace. Depending on what you're doing, this root-namespace may be shared across multiple projects. A good example of this is System. You might put your company name there, or you might choose something more eclectic. If I am writing library code, I avoid things like DataCleaner because libraries are supposed to be generic and don't pertain to a common form.
So, you're writing a data cleaner. Great! There's nothing wrong with having a common library and having a Data namespace (maybe you want to add more things pertaining to data in the future), and then you have your Cleaner class.
If I were a user of your API, I'd happily understand that <library>.Data was a namespace and I need to be looking for Cleaner.
Microsoft has an excellent set of guidelines - See the next link:
Guidelines for Names
Also see the Naming section
I think you'll find there all the info you need.
This question already asked in StackOverflow, see here
You should Mike Roberts' series on How to Set Up A .Net Development Tree. It's a bit dated, but the concepts still hold true. Links to his articles are in my answer here: https://stackoverflow.com/a/9982500/467473 (he seems to have rearranged his blog and broken the links therein, though the content is still there). Also see Tree Surgeon, a tool for creating solutions using the principles Mike Roberts espoused.
In a nutshell, lay out your source tree thusly:
Fundamentally, your directory structure should look like this:
Meta/Development RootUsually mapped to root of source control system.
SolutionOne directory, contain your entire solution. Should be named to match the solution.
Solution.slnThe solution file itself.
nant.buildThe nAnt build file for the solution.
libThe lib directory contains 3rd party assemblies/dlls that are referenced by the different projects in your solution. It is under source control. Project references should point here.
toolsThe tools directory contains all 3rd party tools required to build your solution. It, too, is under source control. The tools directory should contain the versions of nAnt, nUnit etc. used by your project — and your build scripts should reference these, rather than the version(s) installed on a developer's machine.
binThe bin directory contains the output of the build process for the solution. Each projects must be configured to point here.
debugdebug build
releaserelease build
objIn the ideal world, each project's obj would be pointed here as well as this has no place in the source tree. Sadly, Visual Studio doesn't offer an official way to do that (though, I'm told, VS can be hacked to do so if you're resourceful enough).
srcThe src directory is the root directory for the actual source code of your solution.
project1The directory for project1.
project.csproj`The project file.
*.cs, etc. files. The source files.
...
project-n
The src directory contains the actual source code. Each project should be named with its full namespace. Whether you lay them out flat or build out the whole namespace structure in the file system is up to you. The idea is that the namespace should guide you to the source file.
Here is a short summary of conventions from another SO question:
Naming Convention in c#
The other answers here are all spot on, for C# you probably want to follow the naming conventions put forth by Microsoft (and are automatically used by default if you are developing with Visual Studio) unless you have a compelling reason not to.
Project Namespace should match the Project Name or should match the Project Name with some standardized prefix prepended.
Conventions for class and files are 1 class per file and the file name should match the class name.
You are forbidden to name give a class the same name as it's namespace, which I assume is the issue you ran into that prompted this question. The solution to that is probably to give the project a more general name.
To give a more concrete example of what we do where I work.
Any product we produce here we typically have a solution with two or more projects in it.
The default namespace for any project follows: ...
If the DataCleaner product had a windows service and a command line tool that dealt with the same domain you might have three projects: Console, Service, and Domain with name spaces of
Company.Team.DataCleaner.Console
Company.Team.DataCleaner.Service
Company.Team.DataCleaner.Domain
For naming of classes (and by extension the files they reside in), if you follow the above scheme you already somewhat get around the problem of having a DataCleaner class in your DataCleaner namespace, but you may find application of the Single Responsibility Principle useful. To put it simply, any class should only do one thing. If you have a class named Cleaner or DataCleaner they might be trying to do too much, and breaking it up would result in names specific to the resulting classes
Related
we are changing the name of our product, so i also want to rename the namespaces of our
framework-classes. But now i have the problem, that i don't know in which programms and scripts our namespaces are used. Is there a way in c#, to locate the same class in two different namespaces?
I know the solution, that i could inherited from my classes in the new namespace, but this is a very bad solution i think. So I have no idea how to solve this problem, because simply renaming all namespaces doesn't help and will cause a lot trouble.
Thank you!
If external scripts are referencing your assembly using the old namespace names then those names will have to remain in your assembly in you wish to continue to use those scripts. If you also want to create new namespace names to reflect the new name of your product, those names will also need to be hardcoded into your assembly. This will inevitably lead to problems!
I would recommend one of the following:
Leave the namespace names as they are.
Rename the namespaces in full and update the Python scripts at the same time.
I would definitely not recommend the faux 'inheritance' method, or any other solution which results in duplication within the assembly.
You could search the whole project / solution of course, but that seems sort of messy and time-consuming too, if you've got more than a trivial project.
Are you using Resharper? For this type of task, you definitely should be. If so, there is a chance this could at least help you on your way:
Rename the folders your source files are in in the Visual Studio Solution Explorer (this should in theory be easier than looking at each source file one by one, right?).
Now open one source file that you know will have the wrong namespace due to a renamed folder. It should appear with a blue squiggly line, as in the picture below.
Use the Resharper tip (pyramid to the left, or Alt + Enter) to open the context meny thingy also shown below.
Select Find all issues of this type in scope, and select Solution as your scope. That might at least help you get an overview of which classes you need to change the namespaces for, and go through them and change them systematically.
As for your scripts, I would guess that you best bet is to do a plain text search for the old namespaces - possibly a search and replace. Perhaps you can include your scripts in a VS solution, and use the built in search there to scan and fix them. That might at least ease the pain a little..
Is it possible to create a namespace hierarchy in C++ that resembles how it works in C#. For instance if I were to need a type to deal colors within C#, I could dive down through the namespaces to get the appropriate type by using the:
System.drawing.color;
type. In this case, it appears the namespaces are resolved at a project level and determined by the namespaces that the type is contained within. For the developer that this code targets, they get all of this in their auto-complete and aren't left searching through folders and files for that type. In C++ I might have to first include the appropriate header file, at which point I feel like we've already gone through the trouble of grepping source code for the appropriate types and finding which file includes those types. In C++ it would look like:
#include "Core/Color.h"
Color c = new Color();
The IDE offers me little help here. I have to do a directory search for the correct type. The directory paradigm seems to break down unless everyone specifically uses the right filenames and directory structure, which isn't always the case.
To fix this, it looks like not only would I have to come up with a namespace hierarchy for all of my types, which isn't such a large problem, but I'd have to also come up with a header hierarchy to eliminate the problem of constantly grepping the code to find the correct files that include those types.
Does a master header hierarchy present a problem for the compiler, preprocessor, or resulting compiled code since we'd essentially have every other header up the chain (up to a point of course) included in new files?
In the end I want a way to assist the developers who use this code by giving their IDEs a way to dive down to all the types without having to do all of the grepping that we currently have to do. There may be a way to quickly do this within IDEs already, at which point I wouldn't need to utilize the language itself to solve this sort of development problem, however I can't seem to find it.
See the following SO discussion and how this was handled by one of the SO users
C++ namespaces advice
http://www.adamjamesnaylor.com/2012/10/23/NestedNamespacesInC.aspx
I can't even get off the ground.I DO KNOW HOW TO ADD REFERENCES, (this."""mshtml""" NOT[working])
using System;
using system.text;
using mshtml; // i cant seem to access this
I'm confused on why when I type mshtml it's in capitals, MSHTML. The problem I'm having is I'm just using a variety of tutorials but can't seem to even start them as this is my first problem.
I have manually browsed when adding references to my application but it doesn't seem to work. I keep getting the capital letter version, except there is no capital letter version of MSHTML.dll in my directories on my PC. If for some reason I don't have this file can it been downloaded for free safely or are my tutorials just too old?
Also just to let you know I understand where these files are meant be, in regards to folders locations ect. I thought if I just add the reference in to my project it should just be there.
Thank you for pointing out that this is your first C# program. It helps us understand the level at which to try to answer.
First, I'd like to address a statement you made:
there is no Capital letter version of MSHTML.dll in my directories
It seems that you're confusing Namespaces with .dll names, which is something I struggled with myself at first. There's a full explanation here, but it may be too technical for beginner level developers.
In a nutshell, at the top of the file where your "using" statements are, you're telling the compiler where to look for certain classes and code by Namespace.
A Namespace is a logical grouping of code. For convenience and clarity, developers group similarly functioning code into Namespaces. For example, Data Access code is in the System.Data Namespace.
When adding a .dll you're adding an actual file reference. In a less-confusing world, .dlls would be named to reflect the Namespaces contained within them. However, it's not always that simple. It' is perfectly possible for me to create a dll named "DaveStratton.dll" that contains Booyah.Encryption, Simple.Functions or any other Namespace I want. There really is no correlation except by convention, and it's not enforced."
For example, if you look in the MSDN Library at the System.Data.SqlConnection class.
The Class name is actually SqlConnection, and it lives in the System.Data Namespace. The System.Data Namespace is contained in the System.Data.dll. (because the developers were following convention and did it this way for clarity.) Screenshot below:
If you look at other classes, you may find discrepancies.
For example, the System.Configuration.SettingsBase class: The SettingsBase class in in the System.Configuration namespace, but if you look at the assembly information, you'll see that it's in System.dll. And the System.Configuration.ConfigurationManager is in the System.Configuration.dll.
So, long story short, you need to know the Assembly (.dll) name when adding a reference in Visual Studio, but you need the class/assembly name when writing your code. In your using statement, you need capital letters because the Namespace is capitalized, ewven if the .dll isn't.
I think you have a misunderstanding by your terms 'capital letter version'.
The name of a dynamic link library, although usually indicative of its purpose and aptly named, isn't strictly tied to the contents of the assembly. You can call it what you want (within reason and limits of the system) and the code inside remains the same.
If you've added a reference to a 'MSHTML.dll' file, then it is very probable that a namespace of 'mshtml' is defined - I didn't say ideal, but probable. Casing within the code does matter though - so if, for instance, Intellisense is showing you a case-variant version of what you're typing then it's probably that. Either way, it won't be what you've been typing if you ignore it.
If you could reference the tutorials you were following then it would likely be easier to see what the actual problem was. Other than that, go ahead with the code that it provides.
As an aside, I'd recommend some reading up on Assemblies in .NET.
it sounds like you need to add a reference to your project first. Open up your solution, in the right project explorer, expand references, right click references to add a reference. Now find the Microsoft.mshtml and add that as a reference.
See here for your using problem, you need to add a reference (right click project, add reference) to this particular COM lib 1: How do I use MSHTML in VB.NET?
I'm doing an autocomplete editor for C# language, and need to get all the words/methods/namespaces/proprieties in C#.
Didn't found anything useful in google.
Also tried with reflection but can't get all items like namespaces after System or other namespaces.
Is there a dictionary with all this on internet, or is there a method to do it with reflection?
for exemple:
User is typing System.
The autocomplete found the System as a namespace and showing all the types/methods and namespaces inside it.
or user is typing Bitmap (if I will not find the Bitmap as a root type, then I will try all the combinations of the using XXX.YYY, like XXX.YYY.Bitmap...)
Thanks
P.S. Please don't recommend me MSDN because I already know about it and this will be the last and worst option, to parse recursively all information on MSDN and save it in a database.
As per #Steve Wellens' comment, there is difference between C# and .NET type names. You have two very different problems to deal with:-
Gaining knowledge of C# - will allow your editor to know about C# keywords, etc. This can be found in the C# language spec, as per #Cody Gray's answer. This does not vary according the context of the particular file you are editing (unless you want your editor to have the option to be able to restrict to older version of C# in which case you will need to build in knowledge of previous versions of the spec).
Gaining knowledge of the types available in the current editing context. For this, you need to know which namespaces have been declared in using statements in the current file and which libraries have been referenced by the project containing the current file. There is no point trying to find out all this information globally for every single library available since the amount of information will be too huge and continuously changing. You could, perhaps, build in knowledge of all type names available in the GAC. In the case of a partial typename, e.g. Bitmap, a simple implementation would use the using statements contained in the file to attempt to determine which type name is being referred to by examining the relevant assemblies referenced by the project containing the current file (conflicts can occur and will need user resolution, e.g. prefixing the partial type name with some more elements of the actual namespace). This is how the Visual Studio editor works. A richer implementation can examine all assemblies referenced by the project containing the current file plus all those contained in the GAC and, if required, suggest either addition of the full name space to the type name or the addition of a using statement. This is how Resharper works.
Did you try the MSDN documentation, for both the .NET Framework and the C# language? This is the closest you'll come to a "directory with all this on [the] internet".
You might also peruse the C# language spec.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm working on an MVVM project, so I have folders in my project like Models, ViewModels, Windows, etc. Whenever I create a new class, Visual Studio automatically adds the folder name to the namespace designation instead of just keeping the project-level namespace. So, adding a new class to the ViewModels folder would result in the namespace, MyProject.ViewModels instead of just MyProject.
When I first encountered this, it annoyed me. My class names are pretty clear, sometimes even containing the name of the folder in them (e.g., ContactViewModel). I quickly found myself manually removing the folder name on the namespaces. I even tried at one point to create a custom class template (see this question), but I couldn't get that to work, so continued doing it manually.
I've begun to wonder, though, if this convention exists for a good reason that I'm just not seeing. I could see it being useful if you for some reason had lots of sets of identical class names organized into folders, but that doesn't seem like a particularly common scenario.
Questions:
Why is it common convention for namespace names to reflect folder structure?
Do you abide by this convention? Why?
Same as you - I fought this for the longest time. Then I started considering why I created folders. I found myself starting to create folders to represent namespaces and packages instead of arbitrary buckets.
For instance, in an MVVM project, it might be helpful to put views and view models in a separate namespace. MVC will have a separate namespace for Models, Controllers, and Views. It is also beneficial to group classes by their feature.
Suddenly, the project feels more organized. It is easier for other developers to find where features are implemented.
If you standardize on your namespace practices, all of your projects will have the same predictable structure which will be a big win for maintenance.
If you want some solid advice I'd recommend buying Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries which gives you all you need to know from the actual framework design team.
...the goal when naming namespaces is creating sufficient clarity for the programmer using the framework to immediately know what the content of the namespace is likely to be...
<Company>.(<Product>|<Technology>)[.<Feature>][.<Subnamespace>]
And importantly
Do not use the same name for a namespace and a type in that namespace
Fragmenting every 1/2 types into namespaces would not meet the first requirement as you would have a swamp of namespaces that would have to be qualified or used, if you followed the Visual Studio way. For example
Core
- Domain
- Users
- Permissions
- Accounts
Would you create
MyCompany.Core.Domain.Users
MyCompany.Core.Domain.Permissions
MyCompany.Core.Domain.Accounts
or just
MyCompany.Core.Domain
For Visual Studio's way it would be the former. Also if you use lowercase file/folder naming you're looking at renaming the class each time, as well as making one big namespace tangle.
Most of it is common sense and really down to how you would expect to see the namespaces organised if you were a consumer of your own API or framework.
i was annoyed by this as well but working with and refactoring projects with large codebases quickly taught me otherwise. Having embraced the concept i think that it's a very good way to structure your code "physically" as well as logically. When you have a large project and the namespaces do not match up to the folders it becomes difficult to locate files quickly. It's also that much more difficult to remember where things are...
Also, if ReSharper recommends it, then it's probably a good idea. E.g. R# will complain if your class' namespace does not match its folder name.
File system folders and namespaces both represent a hierarchy. I seems perfectly natural to me to match the two. I go even one step further and use a 1:1 relationship between files and classes. I even do so when I program in other languages such as C++.
Now that you question the relation between these two hierarchies, I seriously wonder what you would like to represent by the file system hierarchy.
One way of not following the convention is to create the file in the project root folder and then move it to the final sub-folder.
Anyhow, it is a convention I actually like. If I am splitting types into folders, then probably those types have some kind of conceptual grouping related to the folder. Therefore, it ends making some sense, their namespaces are also similar. Java takes this approach and enforces it with its package system. The biggest difference is that VS is only "suggesting" it to you, since neither the language or the CLR enforces it.
While I agree with everyone else, that a physical structure matching the logical structure is helpful, I have to say I also fight with Visual Studio's auto-naming. There are half a dozen reasons why I have to rename classes:
I use a root "src" folder to visually separate my code from embedded resources
I want different capitalization
I'll organize my code into subfolders for organization within a namespace
I like to separate interfaces from implementations and base classes
I feel like it
With thiose reasons, I've resigned myself to having to adjust those for every class I create. My strategy to avoid the issue is copying a file that has the namespace declaration I want, and then immediately delete the contents.
I think there are indeed valid reasons for having different structures for namespaces and project folders. If you are developing a library, the namespace structure should first and foremost serve the users of your API: it should be logical and easy to grasp. On the other hand, the folder structure should be primarily there to make life easy for you, the API designer. Some goals are indeed very similar, like that the structure should be logical, too. But there may also be different ones, e.g. that you can quickly select related files for tooling, or that it is easy to navigate. I myself for example tend to create new folders when a certain file threshold is reached, otherwise it just takes too long to locate the file I'm looking for. But respecting the designer's preference can also mean strictly following the namespace - if that is their preference.
So overall, in many cases it makes sense that both match, but I think there are valid cases to deviate.
What has been helpful in the past for me was creating a file (e.g. WPF UserControl) in one place to get the namespace right and then moving it to the "right" folder.
Before namespaces were introduced in C++ all C types were in the global namespace. Namespaces were created to segregate types into logical containers so it was clear what type is being referred to. This also applies to C#.
Assemblies are a deployment decision. If you look at the .Net framework a given assembly will contain multiple different namespaces.
Folder are to organize files on disk.
The three have nothing to do with each other, however, it's often convenient that the assembly name, namespace and folder names are the same. Note that Java collapses folders and namespaces to be the same thing (limiting the developer's freedom to organize files and namespaces).
Often we choose to organize files in a project into multiple folders because it's easier for me or my team to navigate the files. Usually this file organization has nothing to do with the namespace design we use. I wish the VS team would not default the namespace to be the same as the folder name or at least give the option back to not have this be the default.
Don't suffer, either change the template for new classes or correct the namespace after the new file gets created.
I also feel the pain with this 'by default' behaviour in Visual Studio.
Visual Studio also tries to set a namespace/directory match when you put your LinqToSql .dbml files in their own directory. Whenever I edit the .dbml, I have to remember to:
open the .dbml.designer.cs file
remove the directory/folder name from the namespace declaration
There's a way to stop this behaviour, though. It involves creating a custom class template.
While I agree that matching the namespace hierarchy to the folder hierarchy is handy, and a good idea, I think the fact that Visual Studio doesn't seem to support switching this feature off is disgusting. Visual Studio has a lot of applications, and there are plenty of coding styles and ways of structuring the source file folders that are perfectly fine.
Let's say there's thousands of files that belong in a namespace, but the programmer just wants to group them into folders to make the hierarchy easier to navigate. Is this really such a bad idea? Will this really make things so un-maintainable that it should be forbidden by the IDE???
Let's say I'm using Visual Studio to work with Unity. Now, all my scripts are in the "Assets.Scripts" namespace. Not only is there a useless Assets namespace which contains no scripts now, but "Assets.Scripts" is meaningless - it does not describe what project or part of project the source file belongs to. Useless.