在MVC中连接数据库的更好方法 [英] Better way to connect database in MVC

查看:67
本文介绍了在MVC中连接数据库的更好方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在做ASP.net MVC应用程序。

i我通过控制器连接数据库实例,我正在使用的模型处理数据并提供数据结构以供查看。



我发表了一些文章说它控制器就像模型和视图之间的界面,所有其他过程和数据库连接必须是模型的一部分



现在我的困惑是我使用控制器连接数据库的原因。
哪种方法让我遵循更好的编码标准,通过模型或控制器连接DB?



问候,

Sekhar

解决方案

您可以使用Repository模式。请阅读以下文章了解更多信息。



什么是存储库模式?



存储库模式:

简单来说,存储库基本上作为我们之间的中介业务逻辑层和应用程序的数据访问层。有时,将数据访问机制直接暴露给业务逻辑层会很麻烦,它可能导致用于访问类似实体的数据的冗余代码,或者可能导致难以测试或理解的代码。为了克服这些问题,并编写一个接口驱动和测试驱动的代码来访问数据,我们使用Repository Pattern。存储库对数据的数据源进行查询,然后将数据从数据源映射到业务实体/域对象,最后将业务实体中的更改持久保存到数据源。根据MSDN,存储库将业务逻辑与与底层数据源或Web服务的交互分开。数据和业务层之间的分离有三个好处:



1.集中数据逻辑或Web服务访问逻辑。



2.它为单元测试提供了一个替换点。



3.它提供了一个灵活的架构,可以作为整体设计进行调整应用程序的演变。



使用MVC中的存储库模式进行CRUD操作 [ ^ ]



学习MVC第6部分:带有实体框架的MVC3应用程序中的通用存储库模式 [ ^ ]



UPDATE



在控制器的构造函数中实例化你的repo对象如下。



  private  IBookRepository _bookRepository; 
public BookController()
{
this ._ bookRepository = < span class =code-keyword> new BookRepository( new BookContext());
}





之后在你的行动方法中使用它如下。



  public  ActionResult 索引()
{
var books = 来自 book _bookRepository.GetBooks()
< span class =code-keyword>选择 book;
return 查看(图书);
}





注意:请阅读第一个链接了解更多信息。 :)


Hi,

Iam doing ASP.net MVC application.
i am connecting database instance through controller, models i am using just to process the data and give data structure to view.

I red some articles it saying "controller is just act like interface between model and view, all other process and database connections has to be part of model"

Now my confusion is why i am using controller to connect DB.
which approach is make me to follow better coding standards, connecting DB through model or controller ?

Regards,
Sekhar

解决方案

You can use Repository pattern with it.Please read below articles for more info.

What is Repository pattern ?

Repository pattern:

In simple terms, a repository basically works as a mediator between our business logic layer and our data access layer of the application. Sometimes, it would be troublesome to expose the data access mechanism directly to business logic layer, it may result in redundant code for accessing data for similar entities or it may result in a code that is hard to test or understand. To overcome these kinds of issues, and to write an Interface driven and test driven code to access data, we use Repository Pattern. The repository makes queries to the data source for the data, thereafter maps the data from the data source to a business entity/domain object, finally and persists the changes in the business entity to the data source. According to MSDN, a repository separates the business logic from the interactions with the underlying data source or Web service. The separation between the data and business tiers has three benefits:

1.It centralizes the data logic or Web service access logic.

2.It provides a substitution point for the unit tests.

3.It provides a flexible architecture that can be adapted as the overall design of the application evolves.


CRUD Operations Using the Repository Pattern in MVC[^]

Learning MVC Part 6: Generic Repository Pattern in MVC3 Application with Entity Framework[^]

UPDATE

Instantiate your repo object inside the controller's constructor is as below.

private IBookRepository _bookRepository;
public BookController()
{
    this._bookRepository = new BookRepository(new BookContext());
}



After that use it inside your action method is as below.

public ActionResult Index()
{
    var books = from book in _bookRepository.GetBooks()
    select book;
    return View(books);
}



NOTE: Please read 1st link for more info. :)


这篇关于在MVC中连接数据库的更好方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆