1

I've recently been reading Microsoft's tutorial on ASP.NET Core and seen that the controller classes of that tutorial include LINQ queries to fetch data from a database. A code snippet of that tutrorial for example looks like this:


// GET: Movies
public async Task<IActionResult> Index(string movieGenre, string searchString)
{
    // Use LINQ to get list of genres.
    IQueryable<string> genreQuery = from m in _context.Movie
                                    orderby m.Genre
                                    select m.Genre;

    var movies = from m in _context.Movie
                 select m;

    if (!string.IsNullOrEmpty(searchString))
    {
        movies = movies.Where(s => s.Title.Contains(searchString));
    }

    if (!string.IsNullOrEmpty(movieGenre))
    {
        movies = movies.Where(x => x.Genre == movieGenre);
    }

    var movieGenreVM = new MovieGenreViewModel
    {
        Genres = new SelectList(await genreQuery.Distinct().ToListAsync()),
        Movies = await movies.ToListAsync()
    };

    return View(movieGenreVM);
}

My question is the following: What exactly does a model class contain? In all the articles I read about MVC pattern I am advised to put the functions where I fetch data from a database into a model class. So are they doing a mistake in this tutorial?

  • Your model should be your entties. In your example the model is the `Movie` entity in which you query the database. This should exist inside the Models folder. Not the db query. Just the Movie class with all its properties. – panoskarajohn Dec 22 '19 at 19:47
  • I think the "MVC" framework hides this a bit, but the fetching/updating usually happens much later and sort of behind the scenes... (not when you execute the LINQ query... the lambda hides a lot there... the fetch is probably done when .ToList is called? So there is a super model in the framework handling all those database calls) The "movieGenreVM" model is a "View Model" and is just data to be used in the view. – pcalkins Dec 23 '19 at 22:12
  • Also see this post from someone who has a deeper understanding of what MVC means in a web context: https://stackoverflow.com/questions/5863870/how-should-a-model-be-structured-in-mvc/5864000#5864000 – pcalkins Dec 23 '19 at 22:15

1 Answers1

1

A model class is a DTO and you can read more about them here: https://learn.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5

There is not an error in the code that you have, it is only a bad practice. The main difference between an entity and a DTO is that you do not mention the relation to another DTO. For example, when we talk about Entities we have

public class Author{
public string name{get; set;}
public List<Book> Books {get; set;}
}

But when we talk about DTO we will have only

public class Author{
    public string name{get; set;}
    }

The code will run but you do not want to get the whole properties of the entity, only a few and clearly not the entities related to it. If you do the version they presented, the code will take more to compile and will be prone to error.