简体   繁体   English

在3层架构中使用MVC模式

[英]Using MVC Pattern With 3 Layer Architecture

I am using ASP.NET MVC in my application. 我在我的应用程序中使用ASP.NET MVC。 I have divided my application into three layer architecture. 我已将应用程序分为三层体系结构。 1) Data Access Layer (using Entity Framework), 2) Application/Business Layer, 3)Presentation Layer (ASP.NET MVC). 1)数据访问层(使用实体框架),2)应用程序/业务层,3)表示层(ASP.NET MVC)。

Because I am using MVC framework on my presentation layer, I am confused about the business logic. 因为我在表示层上使用MVC框架,所以我对业务逻辑感到困惑。 I need to know where do I put my business logic in MVC pattern. 我需要知道如何将我的业务逻辑放在MVC模式中。 In other words we can say from where I need to call my middle layer. 换句话说,我们可以说从哪里开始调用我的中间层。 From the Models or from the Controllers? 从模型还是从控制器?

If I call my business logic from Controller then It seem like the Models are useless. 如果我从Controller调用我的业务逻辑,那么这些模型似乎毫无用处。 Other wise if I call business logic from the model then it seems like unnecessary bidden on the system because Business Object maps with model and then model is passed to the Controller. 否则,如果我从模型中调用业务逻辑,那么这似乎在系统上不必要了,因为Business Object与模型进行映射,然后将模型传递给Controller。 Model does exactly what DTO is doing. 模型完全可以执行DTO的工作。

Any help will be appreciated 任何帮助将不胜感激

ASP.NET MVC layer or tier doesn't contains neither business logic nor business model. ASP.NET MVC层或层既不包含业务逻辑也不包含业务模型。 M in MVC stands for UI model, not the model of your application core and MVC (as well as other MV* patterns) is generally pattern for separating UI concerns. MVC M代表UI模型,而不是应用程序核心模型, MVC (以及其他MV*模式)通常是用于分离UI问题的模式。 You should send messages (call) your Business Layer (BL) from Controllers, aggregate data, create or map it results into UI model and pass it to view. 您应该从Controllers发送消息(调用)您的业务层(BL),聚合数据,创建结果或将其映射到UI模型并将其传递给视图。 Your UI model should know nothing of BL model - this distinction makes loosely coupled layers of your application. 您的UI模型应该对BL模型一无所知-这种区别使应用程序的各层松散耦合。

In other words we can say from where I need to call my middle layer. 换句话说,我们可以说从哪里开始调用我的中间层。 From the Models or from the Controllers? 从模型还是从控制器?

Definitely from Controllers. 绝对是来自控制器。 You inject dependencies to it and call them from your Action methods. 您向其注入依赖项,并从Action方法中调用它们。 ASP.NET MVC provide lots of mechanisms of injection dependencies into controllers and integrates nicely with NInject, StructureMap and some other IoC containers. ASP.NET MVC提供了许多向控制器注入依赖的机制,并与NInject,StructureMap和其他一些IoC容器很好地集成在一起。

Dependencies between components in MVC is given below 下面给出了MVC中组件之间的依赖关系

在此处输入图片说明

Picture is takes from Martin's Fowler article on GUI Architecture , which by the way, very nice reading regarding MVC and MVP. 图片摘自Martin的Fowler关于GUI Architecture的文章,顺便说一句,关于MVC和MVP的阅读非常好。

Also, Pluralsight has set of videos on Software Practices, which covers Design Patterns. 此外, Pluralsight还提供了一系列有关软件实践的视频,其中涵盖了设计模式。 I've learned a lot from their definition of MVVM and MVP. 我从他们对MVVM和MVP的定义中学到了很多东西。

Reading this materials should increase your understanding not only on patterns itself, but also in how they fit into application environment and interact with it. 阅读这些材料不仅可以提高您对模式本身的理解,还可以使您更好地了解它们如何适应应用程序环境并与之交互。

This is purely design/architecture decision you have to make based on your requirements. 这纯粹是您必须根据需求做出的设计/体系结构决策。

If you want to scale up your application to support other services/applications, it is advised not to write any business logic in Controller/Model. 如果要扩展应用程序以支持其他服务/应用程序,建议不要在Controller / Model中编写任何业务逻辑。 You could write it in business/application layer. 您可以在业务/应用程序层中编写它。 This will help you to scale up your architecture in future. 这将帮助您将来扩展体系结构。 Say if you want to create restful service for you mobile application, you could just write services as wrapper to re-use existing business/application layer. 假设您要为移动应用程序创建静态服务,则可以将服务编写为包装器以重用现有的业务/应用程序层。

Also just have a look at Domain Driven Design, Eric Evans book is worth reading. 也只要看看域驱动设计,Eric Evans的书就值得一读。

http://dddsample.sourceforge.net/architecture.html http://dddsample.sourceforge.net/architecture.html

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

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