简体   繁体   English

如何将数据从表示层传递到业​​务逻辑层? (ASP.NET MVC 5)

[英]How do I pass data from presentation layer to business logic layer? (ASP.NET MVC 5)

I'm creating my first web application with ASP.NET MVC 5 and want to do it the right way. 我正在使用ASP.NET MVC 5创建我的第一个Web应用程序,并希望以正确的方式进行操作。 I plan on creating an architecture with a Presentation Layer (MVC), a Data Access Layer (DAL), and a Business Logic Layer (BLL). 我计划创建一个具有表示层(MVC),数据访问层(DAL)和业务逻辑层(BLL)的体系结构。 The DAL and BLL will be separate class libraries from the MVC project. DAL和BLL是与MVC项目分离的类库。 The idea is that the MVC project will reference the BLL to perform business logic which will then reference the DAL for interacting with the database. 这个想法是,MVC项目将引用BLL来执行业务逻辑,然后业务逻辑将引用DAL与数据库进行交互。 I am employing the repository pattern. 我正在使用存储库模式。

My question is, how is data passed between layers? 我的问题是,数据如何在层之间传递? For example, let's say I have an Entity Framework model in the MVC project for a Student class with properties FirstName and LastName. 例如,假设我在MVC项目中为一个具有FirstName和LastName属性的Student类提供了一个实体框架模型。 Then I create a strongly typed view where the user can add a student by filling out a simple form and clicking 'Save'. 然后,我创建一个强类型视图,用户可以在其中通过填写简单表格并单击“保存”来添加学生。 The Student model will be posted to the corresponding action method in the Controller, correct? 学生模型将发布到Controller中相应的操作方法,对吗? Then wouldn't the Controller need to send the Student object to the BLL and from there to the DAL to be inserted into the database? 然后,控制器是否不需要将Student对象发送到BLL,再从那里发送到DAL,以将其插入数据库? But how can that be when the BLL and DAL don't know anything about the Student class? 但是当BLL和DAL对Student类一无所知时怎么办?

I don't understand how this can work without creating a circular dependency. 我不了解如何在不创建循环依赖的情况下工作。 Can someone please explain or provide code examples? 有人可以解释或提供代码示例吗?

Thank you in advance. 先感谢您。

I think your idea of MVC is generally correct although it's a bit confusing as to why you would get a circular dependency. 我认为您对MVC的想法大体上是正确的,尽管对于为什么会产生循环依赖关系有些困惑。 From your explanation this is what I can see: 从您的解释中,我可以看到:

M(model) StudentViewModel (lives in MVC app project) M(模型)StudentViewModel (居住在MVC应用程序项目中)

V(view) Create.cstml (model is StudentViewModel) (lives in MVC app project) V(view)Create.cstml(模型为StudentViewModel) (存在于MVC应用程序项目中)

C(controller) Navigates to the CreatePage using a StudentViewModel (lives in MVC app project) C(控制器)使用StudentViewModel导航到CreatePage (位于MVC应用程序项目中)

When the controller does a Post you get the populated StudentViewModel injected into your method, this is what can happen: 当控制器执行Post时,您将填充的StudentViewModel注入您的方法中,这是可能发生的情况:

  • you simply call StudentViewModel.Save(...) (MVC app project) 您只需调用StudentViewModel.Save(...) (MVC应用程序项目)
  • the save method within the view model can create an instance of your BLL and do the needful 视图模型内的save方法可以创建您的BLL实例并做有需要的事情
  • inside the BLL you can create your Student (actual entity) with the information you have and persist the item by calling the DAL 在BLL中,您可以使用自己拥有的信息来创建您的学生(实际实体),并通过致电DAL保留该项目

So you end up with a dependency graph like this 所以最终得到这样的依赖图

MVC app references BLL references DAL references entity layer. MVC应用程序引用BLL引用DAL引用实体层。

There are lots of other ways to make the above structure better using dependency injection etc but this should at least answer some questions (and create lots of other ones) :) 还有很多其他方法可以使用依赖注入等使上述结构更好,但这至少应回答一些问题(并创建许多其他问题):)

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

相关问题 ASP.NET MVC:如何让我的业务规则验证冒泡到表示层? - ASP.NET MVC: How can I get my business rule validation to bubble up to the presentation layer? 我如何使用 ASP.NET Core 3.1 身份验证和注册来自业务逻辑层的用户 - How do i Authenticate and register users from the business Logic Layer with ASP.NET Core 3.1 Identity 如何使表示层(ASP.NET MVC)的数据访问技术(Entity Framework)无知? - How to make the data access technology (Entity Framework) ignorance from the presentation layer (ASP.NET MVC)? Ninject DI / ASP.Net MVC-如何添加业务层? - Ninject DI / ASP.Net MVC - How do I add a business layer? 如何将当前用户会话信息从 Web 应用程序传递到 asp.net core 中的业务层 - How do I pass current user session information from web application to business layer in asp.net core ASP.NET缓存层与业务逻辑层 - ASP.NET Caching Layer Vs Business Logic Layer 演示文稿,业务和数据层 - Presentation, Business and Data Layer 为什么大多数ASP.NET MVC示例都直接在表示层中访问数据库? - Why do most ASP.NET MVC examples access the database directly in the presentation layer? 使用实体DAL(数据访问层)向ASP.NET网站添加自定义业务逻辑 - Adding custom business logic to ASP.NET WebSite with entity DAL (Data Access Layer) 如何将值从实体层传递到表示层 - How can I pass values from Entity Layer to Presentation Layer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM