C# MVC DAL & BLL 概念

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16802860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:49:03  来源:igfitidea点击:

MVC DAL & BLL Concept

c#.netvb.net

提问by highwingers

I am used to Classic Asp, where I never used DAL/BLL concept, and now I am learning MVC and trying to stay away from BAD habits (Such as writing SQL queries within ASP page itself). I read about Data Access Layer and Business Logic Layer....they make sense, but i am trying to figure out how to put them into my current application.

我习惯了 Classic Asp,我从来没有使用过 DAL/BLL 概念,现在我正在学习 MVC 并试图远离不良习惯(例如在 ASP 页面本身内编写 SQL 查询)。我阅读了有关数据访问层和业务逻辑层的信息……它们是有道理的,但我正试图弄清楚如何将它们放入我当前的应用程序中。

Its a Shopping Cart Application.

它是一个购物车应用程序。

Currently I am NOT using EF or SQL to Entities, plain old ADO.NET, where my Functions return DataTable.

目前我没有使用 EF 或 SQL 到实体,普通的旧 ADO.NET,我的函数返回 DataTable。

Let me give you an example.

让我给你举个例子。

1 - I need to Return Products From SQL Table 
2 - My Products Model Class will hold the SQL Table output 
3 - and then I will show the output to View

Query involved to Bring Products

带货查询

Select *  From Products Where title = 'Bluh'

ProductsModelView.vb

产品模型视图.vb

Class ProductsModelView

 Public title as string
 Public sku as string
 ....etc
End Class

Now my View will simply Render the Result (of List(ProductsModelView))

现在我的视图将简单地呈现结果(列表(产品模型视图))

Now my question is...how should I structure above steps into DAL & BAL layers.

现在我的问题是……我应该如何将上述步骤构建到 DAL 和 BAL 层中。

采纳答案by Kenneth

A basic way to start is to create 3 projects:

一个基本的开始方式是创建 3 个项目:

  • A DAL project
  • A BLL project
  • A UI project (your MVC app)
  • DAL 项目
  • 一个 BLL 项目
  • 一个 UI 项目(你的 MVC 应用程序)

In your DAL project you should create a repository-class. What this class does is execute a query on the database and transform the DataTable into your model.

在您的 DAL 项目中,您应该创建一个repository-class。该类的作用是对数据库执行查询并将 DataTable 转换为您的模型。

Your BLL project should have a service-class. This class has a reference to the DAL and calls the method to obtain the list of objects you need (DAL handles DB-code). In this class you can apply logic. NOTE: At the moment you don't seem to have any real logic in your app. That's OK, your service can just return the list from the DAL immediately. It will provide you with a place where you can add logic safely later, without affecting the data access code.

您的 BLL 项目应该有一个service-class。此类具有对 DAL 的引用并调用该方法以获取您需要的对象列表(DAL 处理 DB 代码)。在本课程中,您可以应用逻辑。注意:目前您的应用程序中似乎没有任何真正的逻辑。没关系,您的服务可以立即从 DAL 返回列表。它将为您提供一个地方,您可以稍后安全地添加逻辑,而不会影响数据访问代码。

In the UI, your controller will call the service and pass the result to the view, which is then in charge of rendering the result.

在 UI 中,您的控制器将调用服务并将结果传递给视图,然后视图负责渲染结果。

This is a basic starting point. You could go further with this and completely make it loosely coupled. At the moment you still have a hard dependency from UI => BLL => DAL.

这是一个基本的起点。你可以更进一步,完全让它松散耦合。目前你仍然有一个来自 UI => BLL => DAL 的硬依赖。

I recently wrote an article on how you can make sure you don't create hard dependencies: http://www.kenneth-truyers.net/2013/05/12/the-n-layer-myth-and-basic-dependency-injection/

我最近写了一篇关于如何确保不创建硬依赖项的文章:http: //www.kenneth-truyers.net/2013/05/12/the-n-layer-myth-and-basic-dependency -注射/