久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

MVC中數據訪問層和模型的區別

Difference between Data Access Layer and Model in MVC(MVC中數據訪問層和模型的區別)
本文介紹了MVC中數據訪問層和模型的區別的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我已經在幾個 Web 應用程序中實現了我認為相當不錯的 MVC 表示,但是自從加入了 Crackeroverflow,我發現我最初的定義可能有點簡單,因此我真的很想澄清一下數據訪問層與網絡應用程序的模型或領域層之間的差異.

I have implemented what I thought was a pretty decent representation of MVC in several web applications but since having joined crackoverflow, I'm finding that perhaps my initial definitions were a bit simplistic and thus I'd really like some clarification on the differences between the Data Access Layer and the Model or Domain Layer of a web application.

對于上下文,我目前使用數據訪問對象為對象表示的表中的單個記錄實現 CRUD 函數,以及返回一個對象的 get() 函數,該對象允許我遍歷所有對象滿足 get() 函數的標準.

For context, I currently use data access objects that implement the CRUD functions for a single record in the table that the object represents as well as a get() function that returns an object that allows me to iterate through all of the objects that met the criteria for the get() function.

這些數據訪問對象直接從包含我的業務邏輯的控制器腳本中引用.

These data access objects are referenced directly from the controller scripts which contain my business logic.

如果重要的話,我正在使用 PHP 和 MySQL,但對可能用其他語言編碼的建議感興趣.

If it matters, I'm working in PHP and MySQL but am interested in suggestions that might be coded in other languages.

更新: 對于更具體的示例,我有一個名為 user 的表(這里的約定是單數表名),其中包含諸如電子郵件地址、活動狀態、用戶名、密碼、哪個公司等信息它們屬于等等.這個基本對象在代碼中看起來像這樣:

UPDATE: For a more specific example, I have table called user (the convention here is singular table names) which holds such information as email address, active state, user name, password, which company they belong to, etc. This basic object would look like this in code:

class User implements DataAccessObject
{
     protected $user_id;
     protected $email;
     protected $username;
     protected $password;
     protected $company_id;
     protected $active // Bool that holds either a 0 or 1

     public function __construct ( $user_id ) // Uses Primary Key to know which record to construct
     {
          $sql = //Sql to get this information from the database.

          // Code necessary to assign member variables their values from the query.
     }

     public function insert(){}
     public function update(){}
     public function delete(){}
     public static function get($filters, $orderVals, $limit){}

     // An object such as user might also contain the following function definition
     public static function login($username, $password){}
}

聽起來我可能已經將 DAO 層和模型層混為一談,將任何現實世界類型的功能(例如用戶登錄)與數據訪問功能結合在一起.

It sounds like I might have bastardized the DAO Layer and Model layer into a simplified form that combines both any real-world type functions (such as login for a user) with the data access functions.

推薦答案

model 類作為真實世界實體的良好、干凈、高保真模型獨立存在.如果它是一個業務領域,它們可能是客戶、計劃、產品、付款等等.您的應用程序使用這些類.這個想法是你的應用程序是一個真實世界處理域對象的模型.您的應用程序可以具有看起來像人們真正使用的動詞的方法函數,而這些方法函數的實現看起來像是對真實世界對象的真實描述.

The model classes stand alone as a good, clean, high-fidelity model of real-world entities. If it's a business domain, they might be customers, plans, products, payments, all that kind of stuff. Your application works with these classes. The idea is that your application is a model of real-world handling of the domain objects. Your application can have method functions that look like verbs people really do, and the implementation of those method functions looks like a real-world description of real-world objects.

重要:這(理想情況下)獨立于大多數技術考慮.它是您可以定義的域對象的最純粹模型.[是的,你確實有外鍵查找問題,是的,你必須讓你的模型對象知道一些數據訪問組件,以便模型對象可以找到僅給定外鍵而不是實際對象的其他對象.一個好的 ORM 層會為您處理這個導航問題.]

Important: This is (ideally) independent of most technical considerations. It's the purest model of the domain objects you can define. [Yes, you do have foreign-key lookup issues, and yes, you do have to make your model objects aware of some data access components so that a model object can find each other objects given just foreign keys instead of the actual object. A good ORM layer handles this navigation issue for you.]

一個充滿 SQL 的模型不是一個好的模型.現實世界也不全是 SQL.發票是包含一些名稱、地址和項目、發貨日期以及諸如此類的一堆內容的文檔.

A model full of SQL isn't a good model. The real world isn't full of SQL, either. An invoice is a document with some names and addresses and items, and a shipping date, and a bunch of stuff like that.

access 類處理持久存儲.這通常包括將模型對象映射到關系數據庫表.面向 SQL 的數據訪問層將從關系數據庫重建您的模型,并將您的模型保存在關系數據庫中.YAML 數據訪問層將從您的模型讀取和寫入 YAML 文件.

The access classes handles persistent storage. This usually includes mapping your model objects to relational database tables. A SQL-oriented data access layer would reconstruct your model from a relational database and persist your model in a relational database. A YAML data access layer would read and write YAML files from your model.

有時,對象關系映射 (ORM) 設計模式用于在 SQL 的世界和您的模型之間進行清晰的分離.有時,數據訪問對象 (DAO) 會處理 SQL 和模型之間的這種分離.一個 ORM 或 DAO 對象可以裝滿 SQL.

Sometimes, the object-relational mapping (ORM) design pattern is used to make a clean separation between SQL's world and your model. Sometimes a Data Access Object (DAO) handles this separation between SQL and model. A ORM or DAO object can be packed full of SQL.

確實,當您更改數據庫產品時,唯一的更改是在 DAO 或 ORM 中.模型永遠不會改變,因為它獨立于 SQL、YAML、JSON、XML 或其他一些序列化技術.

Indeed, when you change database products, the only change is in the DAO or ORM. The model never changes, because it is independent of SQL, YAML, JSON, XML or some other serialization technique.

如果您的 DAO 創建并保留模型對象,我認為您已經相當好地實現了 MVC 的模型部分.您可以查看 ORM 包以獲取有關最新技術的更多想法.我本人是 iBatis 的粉絲.

If your DAO creates and persists model objects, I think you've got the model parts of MVC implemented reasonably well. You can look at ORM packages to get additional ideas of the state of the art. I'm a fan of iBatis, myself.

但僅占整個 MVC 世界觀的 1/3.而且——當然——純粹主義者會告訴你 MVC 只是桌面或只是閑聊或與 MVC 的常見 Web 實現不同.

But's only 1/3 of the whole MVC world-view. And -- of course -- purists will tell you that MVC is only desktop or only smalltalk or different from the common web implementation of MVC.

這篇關于MVC中數據訪問層和模型的區別的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Action View Helper in Zend - Work around?(Zend 中的動作視圖助手 - 解決方法?)
Is this a good way to match URI to class/method in PHP for MVC(這是將 URI 與 PHP 中用于 MVC 的類/方法匹配的好方法嗎)
Where do I save partial (views) in Zend Framework, to be accessible for all Views in my App?(我在哪里保存 Zend Framework 中的部分(視圖),以便我的應用程序中的所有視圖都可以訪問?) - IT屋-程序員軟件開發技術
Having a single entry point to a website. Bad? Good? Non-issue?(有一個網站的單一入口點.壞的?好的?沒問題?)
Is MVC + Service Layer common in zend or PHP?(MVC + 服務層在 Zend 或 PHP 中常見嗎?)
Hello World example in MVC approach to PHP(PHP MVC 方法中的 Hello World 示例)
主站蜘蛛池模板: 国产1区 | 作爱视频免费观看 | www.久| 亚洲福利一区二区 | 黄片毛片| 久99久视频 | 亚洲成人久久久 | 色婷婷一区二区三区四区 | 三级av在线| 久久不卡 | www国产成人 | 夜夜爆操 | 国产美女视频黄 | 日韩一二区 | 国产高清美女一级a毛片久久w | 欧美日韩三级在线观看 | 久久国产亚洲精品 | 一区在线免费视频 | 午夜丰满寂寞少妇精品 | 日韩一区和二区 | 国产电影精品久久 | 欧美一区二区另类 | 韩日在线观看视频 | 天天射中文 | 日韩欧美不卡 | 91精品在线观看入口 | 久艹网站 | 国产精品美女www爽爽爽视频 | 免费欧美 | 中文字幕在线观看一区二区 | 国内在线视频 | 中文字幕乱码一区二区三区 | 久久99精品久久久久久国产越南 | 午夜精品久久久久久久久久久久 | 亚洲午夜精品一区二区三区他趣 | 久久久成人网 | 玖玖国产精品视频 | 一区二区免费看 | 成年人在线观看 | 亚洲不卡在线观看 | 日本91av视频 |