[英]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注入您的方法中,这是可能发生的情况:
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.