Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
Posted

1 solution

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.

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


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

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


NOTE: Please read 1st link for more info. :)
 
Share this answer
 
v4
Comments
sekhar Reddy P 24-Apr-14 2:13am    
Thanks Sampath.

iam using repository pattern, my confusion is whether i can create repo objects in model or controller ?

-Sekhar
Sampath Lokuge 24-Apr-14 2:23am    
Aha..Ok.Please check my answer's UPDATE section. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900