M
M
Matsun2017-03-02 10:56:35
ASP.NET
Matsun, 2017-03-02 10:56:35

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);
        }

This controller fetches an article from the database with the given URL.
Then, using the static method public static string CreateTable(strStartText) (located in the domain model), it processes the text of the article, extracting h2 headings from it and creating a table of contents based on them.
Here are the questions.
1. Is it necessary to transfer the query to the database to the model, if so, to which one, to the view model or domain model? It's just that I have my doubts. After all, the domain model is the essence of the article (Code First approach), and will it be correct that in addition to properties, the model will also have methods?
2. Is the method public static string CreateTable(string strStartText) correct?make it static and put it there, are there more "elegant" ways to process the data received from the database?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
F
Fat Lorrie, 2017-03-02
@Matsu

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 .

O
OnYourLips, 2017-03-02
@OnYourLips

https://martinfowler.com/eaaCatalog/serviceLayer.html
ServiceLayerSketch.gif

B
BohdanchukVL, 2017-11-20
@BohdanchukVL

Rectordecryptor gave something, but file through file

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question