简体   繁体   English

REST体系结构:数据访问层作为模型,REST的目的是调用调用DAL的业务逻辑层

[英]Rest Architecture: Data Access Layer as Model, REST purpose is to call Business Logic Layer that calls DAL

I'm using SOAP web service in my company and I'm used to their architecture which is Service > BLL > DAL. 我在公司中使用SOAP Web服务,并且习惯于它们的体系结构,即Service> BLL> DAL。 Currently, I'm studying REST and based from my observation, I think that Models are the same with DAL. 目前,我正在研究REST,根据我的观察,我认为Model与DAL相同。

So what I'm planning to do is that my Controllers in REST will bypass the Models and will instead call the BLL (and do some logic) that calls the DAL. 所以我打算做的是让我的REST中的Controller绕过Models,而是调用BAL(并执行一些逻辑)来调用DAL。 Which means my Models will be unused. 这意味着我的模型将不被使用。 Will this be okay? 这样可以吗 If not, please give some suggestions and tips. 如果没有,请给出一些建议和提示。

EDIT: I'm not upgrading from SOAP to REST. 编辑:我不是从SOAP升级到REST。 I'm just studying REST and doing a separate project for this 我正在研究REST,并为此做一个单独的项目

Below is my sample REST architecture: 以下是我的示例REST体系结构:

MyProjectDAL > UserDAL MyProjectDAL> UserDAL

namespace DAL
{
    public class UserDAL
    {
        public class User
        {
            public int Id { get; set; }
            public string UserName { get; set; }
            public string Password { get; set; }
            public string First_Name { get; set; }
            public string Last_Name { get; set; }
            public int User_Type { get; set; }
        }


        public User User_GetRecById(string id)
        {
            string cnStr = Utilities.Common.dbConnStr;

            MySqlConnection cn = new MySqlConnection(cnStr);
            MySqlCommand cmd = new MySqlCommand();
            DataTable dt = new DataTable();
            MySqlDataAdapter adapter = new MySqlDataAdapter();

            try
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "User_GetRecById";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@p_Id", id);

                cn.Open();
                cmd.Connection = cn;
                adapter.SelectCommand = cmd;
                adapter.Fill(dt);

                var userList = Utilities.Common.ToList<User>(dt);
                return userList.FirstOrDefault();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                cn.Close();
                cn.Dispose();
                cmd.Dispose();
                dt.Dispose();
                adapter.Dispose();
            }
        }
    }


}

MyProjectBLL > User MyProjectBLL>用户

namespace BLL
{
    public class User
    {
        public UserDAL.User User_GetRecById(string username, string password)
        {
            var objDAL = new UserDAL();
            var user = objDAL.User_GetRecById(username, password);
            //add more logic here
            return user;
        }
    }
}

RestService > Controllers > UserController RestService>控制器> UserController

namespace RestService.Controllers
{
    public class UserController : ApiController
    {
        // GET
        public UserDAL.User Get(string user, string encryptedPW)
        {
            var objBll = new BLL.User();
            return objBll.User_GetRecById(user, encryptedPW);
        }
    }
}

Correct the question First of all, MVC and REST are two different concepts. 纠正问题首先,MVC和REST是两个不同的概念。 your question doesn't have any relation with REST concept. 您的问题与REST概念没有任何关系。 It is related to MVC. 它与MVC有关。


Difference between MVC and REST: I am not listing all the difference in detail, but giving basic overview of both that can help you at this moment. MVC和REST之间的区别:我没有详细列出所有区别,但是现在提供两者的基本概述可以为您提供帮助。 You can find n number of sources on both the topic by your own. 您可以在自己的两个主题上找到n个来源。

1. Rest talks about: How to form proper enpoints? 1.休息谈论:如何形成适当的观点? How to accept data? 如何接受数据? (in body, url path or url query param). (在正文中,网址路径或网址查询参数中)。 etc.... 等等....

2. MVC talks about: How to modularize your web application code? 2. MVC谈论:如何模块化您的Web应用程序代码?

Your can apply MVC on any web application, irrespective of what type web service architecture or guideline (SOAP/REST) this web application is using. 您可以将MVC应用于任何Web应用程序,而不管此Web应用程序使用的是哪种类型的Web服务体系结构或准则(SOAP / REST)。


Answer to your question Now coming to MVC, M represent (business logic + data base logic both). 回答您的问题现在来看MVC,M代表(业务逻辑+数据库逻辑都)。 So ideally you should not merge these two in to single once. 因此,理想情况下,您不应该将这两者合并为一个。 While implementing any api you should atleast create three layer at back-end (controller, business layer, data base layer). 在实现任何api时,您都应至少在后端创建三层(控制器,业务层,数据库层)。 Controller should never talk to Database layer directly there should be a proxy service for API. 控制器永远不要直接与数据库层对话,应该有API的代理服务。
----------MVC---------is----- M (Business layer+ Data Layer) + V (View) + C-----(Controller) ---------- MVC ---------是----- M(业务层+数据层)+ V(视图)+ C -----(控制器)

Ex: 例如:
Controller 控制者

@GetMapping(value = "myapi/Books")
public Books getBook(String bookId) {
   bookService.getBook(bookId); 
}

Service or Business Layer 服务或业务层

To apply logic before any after DB calls like logging, error handling, buisness logic on the basis of if and else etc... 要在数据库调用之后(例如日志,错误处理,基于if和else等的商务逻辑)之前应用逻辑...

 public Book getBook(String bookId) {
   bookDAL.getBook(bookId); 
 }

Database Layer 数据库层

public Book getBook(String bookId) {
   dbClient.getBook(bookId); 
 }

Regarding your code snippet, I see your BLL (which I consider your app domain) depending from the DAL. 关于您的代码段,我看到您的BLL(我认为是您的应用程序域)取决于DAL。 If you change the DAL structure in the future, you'll also be forced to change the BLL too (which is not the best). 如果将来要更改DAL结构,您也将不得不更改BLL(这不是最好的方法)。 So, a quick win could be to decouple them a bit by adding Dto's or common interfaces. 因此,一个快速的胜利可能是通过添加Dto或通用接口来使它们分离一些。 Skipping "the models" can easily become so hard to maintain in the future. 跳过“模型”将来很容易变得很难维护。

I'd also like to give you a more generic suggestion. 我也想给你一个更一般的建议。 If you really want to push your knowledge forward and you have plenty of time and resources to dedicate on refactoring, I'd like to suggest you to have a look at Clean Architecture philosophy. 如果您真的想推动知识的进步,并且您有大量的时间和资源用于重构,那么我建议您看一下Clean Architecture哲学。
My personal starting point was this video Clean Architecture with ASP.NET Core 2.1 and then you could also buy Robert C. Martin's book "Clean Architecture", or take a look at his blog HERE 我的个人出发点是这段视频,它具有ASP.NET Core 2.1的Clean Architecture,然后您还可以购买Robert C. Martin的书“ Clean Architecture”,或在这里浏览他的博客。

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

相关问题 具有业务层和DAL的WPF体系结构 - WPF Architecture with Business Layer and DAL 使用实体DAL(数据访问层)向ASP.NET网站添加自定义业务逻辑 - Adding custom business logic to ASP.NET WebSite with entity DAL (Data Access Layer) 将数据访问逻辑从业务层移到数据访问层 - Move Data Access logic from the business layer to the data access layer 业务逻辑层应该访问数据库/数据访问层吗? - Should Business Logic Layer access the DB/Data Access Layer? 有关项目中的业务逻辑层和数据访问层的问题 - Questions about business logic layer and data access layer in a project 业务逻辑层和数据访问层:循环依赖 - Business Logic Layer and Data Access layer: circular dependency 数据访问层 (DAL) 设计 - Data Access Layer (DAL) design 业务层(BLL)数据访问层(DAL)和UI之间的通用结构? - Common structures between business layer (BLL) data access layer (DAL) and UI? DAL,模型层,EF代码优先和业务逻辑,它们如何组合在一起? - DAL, Model Layer, EF code-first and business logic, how do they fit together? 我应该在业务层还是数据访问层新建数据库 model - Should i new database model in business layer or data access layer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM