开发者

Domain Model Namespace naming conventions

开发者 https://www.devze.com 2023-02-03 16:59 出处:网络
Issue: For asp.net mvc apps, I ignore the domain folder and write the domain model in a separate library project.

Issue:

For asp.net mvc apps, I ignore the domain folder and write the domain model in a separate library project.

Visual Studio, with such a library project, will namespace on a folder by folder basis. Eg, if my library project is called:

ES.eLearning.Domain

and I have a folder called Services, it will auto namespace all classes in the Services folder as:

ES.eLe开发者_如何学Goarning.Domain.Services

Which is all well and good, but strikes me as overkill. My projects are relatively simple and so there is no chance of class name clashes. Even if there were, the compiler would pick it up.

So I just manually edit the namespace names to the project name, ie:

ES.eLearning.Domain

That way, when developing the front end, the domain model project is just a black box that delivers, well, a domain model. And I don't see the point of coupling the namespace names to the projects folder structure, as opposed to treating it as one functional block.

So two questions arise:

  1. Is this bad practice? If so, why? Remember: these are small to medium projects.

  2. Is there a way to tell VS 2010 how to name my namespaces?

Thanks

Note: In my naming of the project, ES is the client, eLearning is the app, Domain is the component

PS: other questions on namespace naming conventions I have found on SO address different issues. So not a duplicate as far as I can determine.


To prevent a particular folder forcing its contents to use the folder name as a namespace part, you can right-click the folder, choose "Properties" and then change "Namespace Provider" to False. That would need to be done to all the folders to which it applies though, so I'm not sure if that's really answered your question fully.

Edit: looks like ReSharper adds this functionality.


Coaxing your IDE to use a naming convention that suits your particular environment is good practice. You can use the default namespace box in the Application tab of the project properties to change the namespace that VS automatically inserts in your class files.


I'm not sure if it possible to configure in VS2010 to disable this.. but I would not do that.

If your code file placed in Domain folder, its namespace should be Company.Domain. If domain has sub folder Services, namespace should be Company.Domain.Services. It is a good (and whats important - common, practice).

You better stick to the rules, over violating them :)


My suggestions to use singular ending while naming namespaces, folders and classes. For example

namespace ES.eLearning.Domain.Services

would be preffered as

ES.eLearning.Domain.Service // <-- note missing "s"

And another example of usage in source code:

ES.eLearning.Domain.Constants.FileTypes.Text

could be "better" to understand as

ES.eLearning.Domain.Constant.FileType.Text

It is all of the personal preferences and it is not a rule. Just a small thing that might make the code (namespace) shorter and easier to read at the same time.

PS. On the other hand ASP.NET MVC does not follow this "rule":

Content
Controllers
Models
Scripts
Views

But nothing keeps you "renaming" them in the source code anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消