简体   繁体   English

不使用Entity Framework的MVC架构

[英]MVC architecture without using Entity Framework

I am completely new to MVC architecture and having some doubts regarding the architecture and they are mainly due to not using entity framework, instead i've been using Data access with datatables and datasets to fetch data to the application. 我对MVC架构完全陌生并且对架构有一些疑问,它们主要是因为没有使用实体框架,而是我一直在使用带有数据表和数据集的数据访问来获取应用程序的数据。 I would like to know the best practices regarding MVC pattern in case if someone can help out with certain links or pdfs(without entity framework). 如果有人可以帮助某些链接或pdf(没有实体框架),我想知道有关MVC模式的最佳实践。 One more thing i would like to know and that is, from where do we call the DAL methods that obtain data from the database? 我想知道的另一件事是,我们从哪里调用从数据库获取数据的DAL方法? from the Model classes or from the Controller Actions? 来自Model类还是来自Controller Actions?

This is a brief demo of how one would implement data access code using MVC. 这是一个如何使用MVC实现数据访问代码的简要演示。 Note that data access typically occurs in the controller action, not the model as someone indicated above: 请注意,数据访问通常发生在控制器操作中,而不是上面指示的模型:

[HttpPost]
public ActionResult Update(MyModel model){
    //You call your data access code here and retrieve your entity
    DataAccessObject dao = new DataAcessObject();
    var entity = dao.Get(model.Id);
    //Now that you have your entity, update its properties from the model that
    //has been posted to this action
    entity.Name = model.Name;
    entity.Phone = model.Phone;
    //Once your entity has been updated, pass it to the Update method of the DAO
    dao.Update(entity);
}

There are plenty of ways to skin this cat, but something like the above should give you the idea. 有很多方法可以给这只猫上皮,但是上面提到的东西可以给你一个想法。 And unless your application is trivially small, you should look at implementing the Repository pattern to decouple your UI and data layers. 除非你的应用程序很小,否则你应该考虑实现Repository模式来分离你的UI和数据层。

MVC Pattern good practice: Views: Should be pure HTML and no logic Controller: This is the HTTP handler and should not contain business logic but only presentation logic (IF conditions for display etc). MVC模式的良好实践:视图:应该是纯HTML而不是逻辑控制器:这是HTTP处理程序,不应包含业务逻辑,只能包含表示逻辑(用于显示的IF条件等)。 It is not aware of where data comes from or how data is obtained. 它不知道数据来自何处或如何获得数据。 It is only aware of the Model objects Model: Represents data and its access. 它只知道Model对象Model:表示数据及其访问权限。 Model should access database and get data and populate object which controller can then use to pass to View. 模型应该访问数据库并获取数据并填充对象,然后控制器可以使用该对象传递给View。

EntityFramework: not related to MVC and hence when you use EntityFramework within MVC project you may not see the the good practices mentioned followed. EntityFramework:与MVC无关,因此当您在MVC项目中使用EntityFramework时,您可能看不到所提到的良好实践。

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

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