Answer the question
In order to leave comments, you need to log in
Asp Net MVC, where is the best place to place queries to the database?
As part of the training, I make my blog on Asp Net MVC. To work with the database, the Entity Framework is used, the Code First approach.
The question is this. Once I heard that when developing web sites on MVC, placing queries to the database in the controller is not good.
Accordingly, then that's the question, here is my controller
public ActionResult ShowOne(string url = null)
{
Article one = repository.Articles
.FirstOrDefault(p => p.Url == url);
one.FullText = Article.CreateTable(one.FullText);
return View(one);
}
Answer the question
In order to leave comments, you need to log in
Typically, applications are divided into three layers:
BL is independent, DAL depends on BL, UI depends on DAL and BL (because interfaces are located there and are bound to real classes).
If we consider the classical data access scheme through repositories, then:
BL contains: domain models, interfaces for repositories, services (interfaces and implementations that work with repositories).
DAL contains: implementations of repository interfaces (from BL), i.e. work with DB, DbContext (from EF) and other specifics about where and how data is retrieved.
UI(for ASP.NET MVC) contains: the actual web project with controllers *Controller.cs (Controllers), views *.cshtml (Views), view models *.cs (Models, those specific models that types views, they can differ from the domain ones (mapped from them in the controller), for example, contain an object with pagination data).
The UI also contains a dependency injection root, where interfaces and their implementations are interconnected, for example, repository interfaces from BL and implementations (tailored to work with the database) from DAL.
Implementation example.
Threat The basics of such an architecture in relation to ASP.NET MVC are in Freeman's wonderful book .
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question