-1

Here is part of an ASP.NET MVC program, from https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/controller-methods-views?view=aspnetcore-3.1 :

A model class Movie:

public class Movie
{
    public int Id { get; set; }
    public string Title { get; set; }
    [Display(Name = "Release Date")]
    [DataType(DataType.Date)]
    public DateTime ReleaseDate { get; set; }
    public string Genre { get; set; }
    [Column(TypeName = "decimal(18, 2)")]
    public decimal Price { get; set; }
}

and a method from a controller class MoviesController is:

// GET: Movies/Edit/5
public async Task<IActionResult> Edit(int? id)
{
    if (id == null)
    {
        return NotFound();
    }
    var movie = await _context.Movie.FindAsync(id);
    if (movie == null)
    {
        return NotFound();
    }
    return View(movie);
}

If I want to write an ASP.NET MVC program into three layers: presentation, business logic and data access layers,

  • Since presentation layer is responsible to provides output to users and interact with users, should both view and controller belong to the presentation layer?

  • Should the business layer be implemented by the model Movie?

    "The Model in an MVC application represents the state of the application and any business logic or operations that should be performed by it" seems to say so.

    Does the business layer need to be implemented as some method, whereas the model doesn't have any method? (I guess the model class is used as an entity class by the Entity Framework. Does EF require every entity class have no method?)

  • In the method of the controller, _context.Movie.FindAsync(id) is using Entity Framework (ORM) to make a query. So does the controller also implement the data access layer, besides implementing part of the presentation layer?

  • How shall I separate presentation, business logic and data access layers?

Thanks.

1 Answer 1

0

I think the question is a little too broad. The code samples tend to be somewhat brief and simplistic and usually do not represent real world applications. Often layers are omitted because they do not add value. For example, the code you quote pulls data from the database in the controller and that's ok - you could add a data layer, but it would not add any value. In real world applications, there usually is a data layer and it does some processing. For example you would have Movie and Director tables but for the view, you would have the Director fields as part of the Movie. This would be technically MVVM (when the view model is different from the persisted object in the database)

I'd recommend a book or other training source to cover a more fleshed out example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.