简体   繁体   English

构建N层应用程序时,应如何组织名称空间?

[英]When Building an N-Tier application, how should I organize my names spaces?

So when I started trying to build my websites in an n-tier architecture, I was worried about performance. 因此,当我开始尝试以n层架构构建网站时,我担心性能。

One of the guys who answered the question told me if you applied a good architecture you'd end up with even a better performance. 回答问题的一个人告诉我,如果您应用了良好的体系结构,最终将获得更好的性能。 It's related to compiling the dlls and stuff, but now I'm not sure how to name my namespaces. 它与编译dll和东西有关,但是现在我不确定如何命名我的命名空间。

Like I have the main Namespace for my data access layer so let's say I have this namespace as my data layer ..DAL 就像我的数据访问层拥有主要的命名空间一样,假设我将此命名空间作为数据层..DAL

but now I have more than entity in the application that needs to be served by this layer, and each entity has it's own smaller entities. 但是现在,在应用程序中我需要的不仅仅是实体,还需要由该层提供服务,并且每个实体都有自己的较小实体。

so should I include all the data code under one Namespace (DAL) or should I each entity have it's own namespace like DAL.E1 and DAL.E2 or each main or child entity should have it's own namespace like DAL.E1.c1, DAL.E2, DAL.E3.c1, DAL.E3.c2 .. last question should DAL itself include any classes or not ? 因此,我应该将所有数据代码都包含在一个命名空间(DAL)下,还是每个实体都具有自己的命名空间,例如DAL.E1和DAL.E2,或者每个主实体或子实体都应该具有其自己的命名空间,例如DAL.E1.c1,DAL .E2,DAL.E3.c1,DAL.E3.c2 ..最后一个问题DAL本身是否应包含任何类?

Humm... it more like an organization problem. 嗯...它更像是一个组织问题。

I usually prefer to organize the items in one namespace (classes, structs, ...) by the functionality on the system, and then, if needed, by grouping types of objects. 我通常更喜欢通过系统上的功能,然后在需要时通过对对象类型进行分组来在一个名称空间(类,结构等)中组织项目。

For example: 例如:

  • App.Domain; App.Domain; // Domain entities //域实体
  • App.Domain.Authentication; App.Domain.Authentication; // Domain entities related for the authentication //与身份验证相关的域实体
  • App.Domain.Repository; App.Domain.Repository; // The Repository implementation //存储库实现
  • App.Domain.Repository.Mapping; App.Domain.Repository.Mapping; // The Mappings (NHibernate HBM, for example, of the repository) //映射(例如,存储库的NHibernate HBM)
  • App.Services; 应用服务; // exposed functionality of the system //公开的系统功能
  • App.Controllers; App.Controllers; // controllers of view //视图控制器
  • App.Controllers.Flex; App.Controllers.Flex; // controllers specialized for the flex //专门针对flex的控制器

Performance is always a point to consider but not the essential one, at least on non real-time applications. 至少在非实时应用程序上,性能始终是要考虑的一点,但不是必需的。 The maintainance and realibility is the one that I think are the most important. 我认为最重要的是维护和实用性。

This is really a subjective question, and every organization will have a completely different, or very similar pattern. 这确实是一个主观问题,每个组织都会有一个完全不同或非常相似的模式。 There's not a best answer, but there are good and bad approaches. 没有最佳答案,但是有好办法。 A common pattern in the industry is to base your library names off of features. 行业中的一种常见模式是使库名称基于功能。 For example, in one of our products we have: 例如,在我们的一种产品中,我们有:

  • ProductName 产品名称
  • ProductName.Activation ProductName.Activation
  • ProductName.Activation.Service ProductName.Activation.Service
  • ProductName.Core 产品名。核心
  • ProductName.Data 产品名称。数据
  • ProductName.Data.Customers 产品名称。数据。客户
  • ProductName.Data.Engine 产品名称。数据。引擎
  • ProductName.Instrumentation 产品名称。仪器
  • ProductName.Security 产品名称。安全性
  • ProductName.ShellUI 产品名称.ShellUI
  • ProductName.ShellUI.Windows 产品名称.ShellUI.Windows
  • ProductName.Win32 产品名称.Win32

Generally following a pattern similar to the .NET Framework is a good approach, or by feature is another. 通常,遵循类似于.NET Framework的模式是一种好方法,或者按功能是另一种方法。 Some may argue that you would not want to give your assemblies meaningful names that may expose vulnerable parts of your application or draw attention, but you will never stop pirates from being pirates. 有人可能会争辩说,您不希望为程序集提供有意义的名称,而这些名称可能会暴露应用程序的易受攻击的部分或引起注意,但是您永远不会阻止海盗成为海盗。

Others prefer to give their assemblies very short names, which is still done even today by Microsoft. 其他人则希望为其程序集使用非常短的名称,即使到了今天,Microsoft仍然使用该名称。 (mscorlib.dll for example). (例如,mscorlib.dll)。

I suppose it all depends on the project and what's going on. 我想这一切都取决于项目以及正在进行的事情。 I don't always abide to the same rule of thumb, but 99% of the time I follow a common pattern, and the former company I worked for had their set patterns and practices as well. 我并不总是遵循相同的经验法则,但是我有99%的时间遵循共同的模式,而我曾任职的前一家公司也有固定的模式和实践。

As far as logical organization inside your projects, well, good luck. 就项目内部的逻辑组织而言,祝您好运。 Most other developers I've talked to say the same thing I do. 我交谈过的大多数其他开发人员都在说同样的话。 'I just picked a structure/name and went with it'. “我只是选择了一个结构/名称并使用了它”。 Of course not blindly, but with some thought into it, but its hard to have a best approach, only guidelines. 当然不是盲目地,而是经过一些思考,但是很难有最好的方法,只有指南。

My suggestion is to organize it by feature, because it makes management of the project easy. 我的建议是按功能组织它,因为它使项目的管理变得容易。 You know that Module1 handles Part1 of the system and Module2 handles Part2, and so on. 您知道Module1处理系统的Part1,而Module2处理Part2,依此类推。 An example would be ProductName.Data.dll. 例如ProductName.Data.dll。 In my project, it handles all data-bound operations such as Settings, Preferences, and Database interaction, while ProductName.Data.Engine is the framework that allows ProductName.Data to communicate easily with the data tier. 在我的项目中,它处理所有数据绑定操作,例如设置,首选项和数据库交互,而ProductName.Data.Engine是允许ProductName.Data轻松与数据层通信的框架。 (In this case ProductName.Engine is the Entity Framework stuff with other custom classes and required framework parts). (在这种情况下,ProductName.Engine是带有其他自定义类和必需框架部分的Entity Framework东西)。

I guess another rule of thumb I go by is if Module1 has many parts that make up Part1 of the application, I would keep it all in Module1. 我猜想我要遵循的另一条经验法则是,如果Module1具有构成应用程序Part1的许多部分,则将其全部保留在Module1中。 Unless like in ProductName.Data.Engine where that feature was so large, it was suited to its own library for easier management. 除非像ProductName.Data.Engine中那样具有如此大的功能,否则它适合于自己的库以简化管理。

All in all, good luck, because organization and structure is constant struggle as projects become large, but if you keep everything tidy, organized, and well found and understood then your project will be easy to manage. 总而言之,祝您好运,因为随着项目规模的扩大,组织和结构将一直处于斗争之中,但是如果您将所有事情保持整洁,有条理,易于发现和理解,那么您的项目将易于管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM