简体   繁体   English

在哪里放置业务实体,枚举,自定义例外?

[英]Where to place Business Entities, Enums, Custom exceptions?

I'm trying to figure out how to share my entities between the data, business, and UI tiers. 我正在试图弄清楚如何在数据,业务和UI层之间共享我的实体。 Is it best to create a separate project for these entities that will be referenced by all the tiers? 是否最好为所有层引用的这些实体创建单独的项目? What about Enums and custom exceptions? Enums和自定义异常怎么样? I have some enums used only by the UI project, and some that are used by the Business. 我有一些仅由UI项目使用的枚举,以及一​​些由业务使用的枚举。 Does this mean I should have two separate Enum folders: one in the Business project and one in the UI? 这是否意味着我应该有两个单独的Enum文件夹:一个在Business项目中,另一个在UI中? Similarly with Exceptions? 与Exceptions类似? Till now, I have been maintaining the entities, enums, and exceptions all in one separate project that are referenced by all the 3 tiers. 到目前为止,我一直在一个单独的项目中维护实体,枚举和例外,这些项目由所有3个层引用。

My Business project has Manager classes (like ProductManager.cs) which have methods like List GetProducts() and SaveProduct(Product), etc. My Business项目具有Manager类(如ProductManager.cs),其中包含List GetProducts()和SaveProduct(Product)等方法。

If these entities have a semantic meaning across all of these tiers, I would recommend putting them in a separate project/assembly that all tiers have knowledge of. 如果这些实体在所有这些层中具有语义含义,我建议将它们放在所有层都知道的单独项目/程序集中。

If you have entities that are specific to one layer, I would confine them to that layer. 如果您有一个特定于一个图层的实体,我会将它们限制在该图层中。 So, for example, you wouldn't allow UI code to throw a Business Layer exception, or the Business Layer to use a UI enum. 因此,例如,您不允许UI代码抛出业务层异常,或者允许业务层使用UI枚举。

Depending on what you are doing, there may be no harm in keeping layer-specific types in the same project/assembly that you keep your shared types, but I would consider confining them to their own layer a best practice. 根据您正在做的事情,将特定于图层的类型保留在您保留共享类型的同一项目/程序集中可能没有任何害处,但我会考虑将它们限制在自己的层中作为最佳实践。

You've been doing the right thing. 你一直在做正确的事。 Creating a separate project with all entities are almost always the way to go. 创建一个包含所有实体的单独项目几乎总是可行的方法。 If the enums and exceptions are entity-related, they belong in there as well. 如果枚举和例外与实体相关,它们也属于那里。

Think encapsulation. 想想封装。 Place them at the scope where they are needed, and not in a wider scope. 将它们放置在需要它们的范围内,而不是在更广泛的范围内。

If something is only used within the UI layer, don't expose it to other layers. 如果某些内容仅在UI图层中使用,请不要将其公开给其他图层。 But if it's used across two or three layers, then define it in the lowest-level layer (eg if UI sits on Business Logic which sits on Database, then something used in BL and UI can just be defined in BL). 但如果它在两层或三层中使用,则在最低层中定义它(例如,如果UI位于数据库上的业务逻辑上,则BL和UI中使用的东西可以在BL中定义)。 Or if something is more or less global, move it to a third party shared library. 或者,如果某些内容或多或少是全局的,请将其移至第三方共享库。

Also, try to minimise the need for shared entities in the first place: eg It is recommended (for good reasons) to avoid custom exceptions. 此外,尝试首先最小化对共享实体的需求:例如,建议(出于好的理由)避免自定义异常。 Most of them can usually be represented by an existing system exception with a bit of extra information (eg an InvalidOperationException with details in the message), minimising the need to handle a whole new class of exceptions everywhere - and of cours eliminating the need to decide where to define the custom exception type. 它们中的大多数通常可以由具有一些额外信息的现有系统异常来表示(例如,消息中包含详细信息的InvalidOperationException),最大限度地减少了在任何地方处理全新异常类的需要 - 以及无需决定在哪里定义自定义异常类型。

我通常将枚举和自定义异常与接口定义一起放在一个单独的项目中。

如果您需要所有层中的实体,那么使用单独的项目(imo)是最好的方法。

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

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