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. 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? I have some enums used only by the UI project, and some that are used by the Business. Does this mean I should have two separate Enum folders: one in the Business project and one in the UI? Similarly with 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.

My Business project has Manager classes (like ProductManager.cs) which have methods like List GetProducts() and SaveProduct(Product), etc.

问题回答

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.

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.

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.

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. But if it s used across two or three layers, then define it in the lowest-level layer (e.g. if UI sits on Business Logic which sits on Database, then something used in BL and UI can just be defined in 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: e.g. 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 (e.g. 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.

I usually put enums and custom exceptions together with the interface definitions in a separate project.

If you need the entities in all tiers then using a separate project is (imo) the best way.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签