-
What is MVC? Define each. What are the benefits of MVC?
Mnemonic which stands for Model-View-Controller, it is a design pattern which separates an application into 3 distinct, loosely coupled software layers.
Model (database), View (presentation) such as Android app or website, and Controller which mediates the handoff between these two layers (plumbing, that is, only mediates flow of objects).
The main benefits include the ability of several developers each programming separate layers simultaneously, ability to test each layer without the other(s), and the ability to switch out a layer (ie, scalability). For example, the model can be switched out from a website and be used for an Apple/Android cell application without modification of other layers, or the model layer can be switched from a SQL database to an Oracle without affecting the others.
-
When dealing with MVC applications, developers often have confusion as to where to put their business logic. Explain and state where the business logic should be located in an MVC application. What are the benefits of this?
MVC structure is often confused with an n-tier application structure and the result is having bits of business rules and data access logic mixed in with the rest of the logic. N-tiers have a BAL, DAL, and PAL (presentation) layer but they are not interchangeable to MVC patterns.
The correct methodology for handling business logic in an MVC application is to not have business logic in ANY of the 3 MVC layers but rather within a separate library, which decouples the layers altogether, whereby the controller can consume this library/assembly into its action methods, allowing the presentation and database layers to work wholly independently of one another. In your controller, you should be able to use dependency injection to inject the business logic layer.
The benefit of this is decoupling of each layer into separate parts, allowing for simple domain objects, simple pass-through controllers, simple testing of business logic and other layers, and scalability in that a web application can be ported more efficiently, for example, converting a Microsoft app to an Apple app would allow us to only change the presentation layer, the business logic, database, and controller layers could all be reused without change.
-
Explain object oriented business logic, procedural business logic, and controller logic.
Object Oriented Business Logic: Gets modeled as objects (in the model), usually injected as repositories. For example, a domain class may contain a constructor for Student which contains DOB which is passed when first initiating the object.
Procedural Business Logic: Goes in an assembly service with an interface that can be injected into a controller. For example, the IStudentSchedule interface might contain relevant business logic methods such as AddClass(); the interface can be later injected from assembly to this controller, and the associated service used as needed.
Controller Logic: Logic that controls how commands are received and passed to the models/services, and how those results are passed to the view.
Controllers should have no business logic, it's a very specific part of a design pattern for controlling how a user interface passes input off to the models that handle business logic (or services if your problems are more procedural in nature).
-
How do we decide to use a Model, ViewModel, or DTO?
Actual database objects should be in the Model or Entities folder, a ViewModel should be used for anything in the view, and a DTO should be used to transfer between remote interfaces (web services), that is, they are built for low data consumption and contain no implementation or methods, simply just data.
-
What is a DTO?
Data Transfer Object. Their purpose is to send data between two systems and ONLY data, contain only fields and should only be found in web services where each call to the server is expensive. For example, a web service call which requires numerous calls to the database can be shortened to include data from only a unique DTO which can then minimize the number of calls and input our data without the heavy overhead.
-
What is a ViewModel?
Object which is designed to be used in the View of a webpage such as a Razor page. It typically contains information from one or more entities along with additional information not found in an entity along. We use this because a web page can only contain a single model, so we create a custom ViewModel which can contain additional information not found in a single table of the database.
-
What is MVC routing?
In ASP.NET, every web page refers to a specific URL. However, in an MVC architecture we can use routing to eliminate the need for this, allowing us to define patterns which map to request handlers (controllers using action methods).
-
Provide a valid and typical MVC map route.
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new {controller = "home", action = "index", id = UrlParameter.Optional }
- );
-
What is an Action result in a controller? How many action result types are there and what are they?
MVC framework includes various action result classes which can be returned from an action method. The result classes represent different types of responses, such as HTML, file, string, JSON, javascript, etc. Below is a list of the 10 action result types.
- (1) ViewResult - HTML and markup.
- (2) EmptyResult - No response.
- (3) ContentResult - string literal.
- (4) FileContentResult/ FilePathResult/ FileStreamResult - the content of a file.
- (5) JavaScriptResult - JavaScript script.
- (6) JsonResult - JSON that can be used in AJAX.
- (7) RedirectResult - a redirection to a new URL.
- (8) RedirectToRouteResult - another action of same or other controller.
- (9) PartialViewResultReturns - HTML from Partial view.
- (10) HttpUnauthorizedResult - Returns HTTP 403 status.
-
Create a very simple 3 part MVC component which displays a list of students.
- (1) Controller Class
- public class StudentController: Controller
- {
- public List<Student> model = new List<Students>(
- {
- new Student() { Id = 1, Name = "John" },
- new Student() { Id = 2, Name = "Becky" }
- });
- public ActionResult Index()
- {
- return View(model);
- }
- }
- (2) Model Class
- public class Student
- {
- public int Id { get; set; }
- public string Name {get; set; }
- }
- (3) View Component
- @model List<Project.Models.Student>
- <table class="table">
- <tr><th>@Html.DisplayNameFor(model => model.Name)</th></tr>
- @foreach(var item in Model)
- {
- <tr><td>@item.Name</td></tr>
- }
-
What is model binding? How is this achieved in an MVC application?
Binding is the ability of an action method to get the name values of similar items declared in a view through the methods parameter list. This can be implied or explicit and we can take some or all.
For example, the action method below includes only two bound parameters.
public ActionResult Edit([Bind(Include = "Id, Name") Student std)
|
|