简体   繁体   English

3-Tier - 模型重用?

[英]3-Tier - Models reusing?

I am creating an application that has those three different projects:我正在创建一个具有这三个不同项目的应用程序:

ApiService (Web API 2)
BusinessLogic (Class Library)
DataAccess (Class Library)
  • ApiService has a reference to BusinessLogic ApiService有对BusinessLogic的引用
  • BusinessLogic has a reference to DataAccess BusinessLogic引用了DataAccess

DataAccess uses Entity Framework with code first approach so it contains the models for the database tables. DataAccess使用具有代码优先方法的实体框架,因此它包含数据库表的模型。

My question is, what is the best approach or best practice regarding the models for Business and Service Projects?我的问题是,关于业务和服务项目模型的最佳方法或最佳实践是什么?

I have read that Service project should not be using the models of the DataAccess project, so where should I create that models, in Service or in Business?我已经读到 Service 项目不应该使用DataAccess项目的模型,那么我应该在 Service 还是 Business 中创建该模型?

Thanks in advance.提前致谢。

Separate BL(Business logic)/Presentation layer models from DAL(Data access layer) models always.始终将BL(Business logic)/Presentation layer模型与DAL(Data access layer)模型分开。

Add one more layer between them which will do the mapping, use Automapper or somethogn custom.加入他们之间多了一个层,它会做的映射,使用Automapper或somethogn定制。 So when you are passing data to DAL models will be mapped to entity models, and when BL is getting the data from DAL same thing, map entity models to BL models,因此,当您将数据传递给DAL模型时,将映射到实体模型,而当BLDAL获取数据时,将实体模型映射到BL模型,

Why?为什么?

The way how you are persisting your data in your database may be rather different from how you are going to present it to the user.您在数据库中持久保存数据的方式可能与您将其呈现给用户的方式大不相同。 The data may have to be obtained from several entities, joined by relationships, constructed at run time again by joining from other tables, etc. How you are going to present it to a user may be simplified and different than it is persisted, so you can hide that complexity which is needed for the database.数据可能必须从多个实体中获取,通过关系连接,在运行时通过从其他表连接再次构建等。您将如何将其呈现给用户可能会被简化并且与持久化不同,因此您可以隐藏数据库所需的复杂性。

I don't know if this is best practice, but I have made many projects that contain a lot of shared logic and functionality between windows services, Web APIs, etc. They have all followed something similar to this:我不知道这是否是最佳实践,但我制作了许多项目,其中包含许多 Windows 服务、Web API 等之间的共享逻辑和功能。它们都遵循类似的内容:

Wrapper - Contains interaces, models, and code to make calling the WebAPI from another .NET project easier.包装器 - 包含接口、模型和代码,使从另一个 .NET 项目调用 WebAPI 更容易。 Has no references to the other projects at all根本没有对其他项目的引用

Core - Contains all the meaty business logic.核心 - 包含所有丰富的业务逻辑。 Service layer, data access layer, helper classes, etc. References Wrapper and anything else needed to function服务层、数据访问层、辅助类等。引用 Wrapper 和任何其他功能所需的东西

WebAPI - Contains only code necessary for creating a WebAPI around the service layer functions in Core References Wrapper for models/interfaces, and Core for business logic WebAPI - 仅包含围绕模型/接口的Core References Wrapper 和业务逻辑的 Core 中的服务层函数创建 WebAPI 所需的代码

Other projects that use Core would be similar to the WebAPI one.其他使用 Core 的项目与 WebAPI 类似。 Examples would be a console app for scheduled tasks, Windows service for continual data processing, etc.例如用于计划任务的控制台应用程序、用于持续数据处理的 Windows 服务等。

This results in what I've seen some people refer to as a "mega solution" or similar, but so long as you're keeping your code to one domain you're not creating a mess.这导致我看到有些人称之为“大型解决方案”或类似的东西,但只要您将代码保留在一个域中,您就不会造成混乱。

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

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