-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating new services: IRestaurantData, InMemoryRestaurantData, regis…
…tering one of them in Startup.cs, modifying the Home controller to receive restaurant data from an IRestaurantData service, updating the view to accept an enumerable model and iterate through the collection of restaurants
- Loading branch information
Showing
10 changed files
with
142 additions
and
11 deletions.
There are no files selected for viewing
13 changes: 11 additions & 2 deletions
13
OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System.Collections.Generic; | ||
using OdeToFoodRider.Models; | ||
|
||
namespace OdeToFoodRider.Services | ||
{ | ||
public interface IRestaurantData | ||
{ | ||
// VSRD: Complete Statement at GetAll{caret} generates both the parentheses and the semicolon | ||
IEnumerable<Restaurant> GetAll(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
OdeToFoodRider/OdeToFoodRider/Services/InMemoryRestaurantData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using OdeToFoodRider.Models; | ||
|
||
namespace OdeToFoodRider.Services | ||
{ | ||
class InMemoryRestaurantData : IRestaurantData | ||
{ | ||
// VSRD: Quick-fix "Initialize field from constructor" is available after declaring the _restaurants field. | ||
// However, the created constructor takes a list of restaurants as a parameter; what we need instead is a parameterless constructor | ||
// with a field inside that is initialized with a new list. No context action or refactoring to convert parameter to | ||
// field initialization, and Change Signature doesn't do that, too. No big deal to do this by hand though. | ||
private List<Restaurant> _restaurants; | ||
|
||
public InMemoryRestaurantData() | ||
{ | ||
_restaurants = new List<Restaurant> | ||
{ | ||
// VSRD: Rider's code completion after "new" results in "new Restaurant()", where parentheses become redundant once braces are added for initializing properties. | ||
// A typing assistant that removes redundant parentheses wouldn't hurt here. | ||
new Restaurant {Id = 1, Name = "Scott's Pizza Place"}, // VSRD: Rider's Complete Statement here doesn't add a comma either but instead, moves the caret beyond the scope of the collection initializer - looks like a bug | ||
new Restaurant() {Id = 2, Name = "Tersiguels"}, | ||
new Restaurant() {Id = 3, Name = "King's Contrivance"} | ||
}; | ||
} | ||
|
||
public IEnumerable<Restaurant> GetAll() | ||
{ | ||
return _restaurants.OrderBy(r => r.Name); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 14 additions & 1 deletion
15
OdeToFoodVisualStudio/OdeToFoodVisualStudio/Controllers/HomeController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/IRestaurantData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using OdeToFoodVisualStudio.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace OdeToFoodVisualStudio.Services | ||
{ | ||
public interface IRestaurantData | ||
{ | ||
IEnumerable<Restaurant> GetAll(); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
OdeToFoodVisualStudio/OdeToFoodVisualStudio/Services/InMemoryRestaurantData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using OdeToFoodVisualStudio.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace OdeToFoodVisualStudio.Services | ||
{ | ||
public class InMemoryRestaurantData : IRestaurantData // VSRD: GetAll() implementation generated with Visual Studio's "Implement interface" quick action | ||
{ | ||
// VSRD: Scott creates this constructor with the ctor code snippet - that's the only option with VS as there's no context action to initialize a field from constructor. | ||
public InMemoryRestaurantData() | ||
{ | ||
_restaurants = new List<Restaurant> | ||
{ | ||
new Restaurant {Id = 1, Name = "Scott's Pizza Place"}, | ||
new Restaurant {Id = 2, Name = "Tersiguels"}, // VSRD: Visual Studio's Shift+Enter doens't add a comma after an object initializer | ||
new Restaurant {Id = 3, Name = "King's Contrivance"} | ||
|
||
}; | ||
// VSRD: in Scott's video, when creating the collection initializer (_restaurants = new List<Restaurant> {<Enter here>}), Visual Studio adds the closing semicolon, | ||
// but my VS fails to do so. | ||
} | ||
|
||
List<Restaurant> _restaurants; | ||
|
||
public IEnumerable<Restaurant> GetAll() | ||
{ | ||
return _restaurants.OrderBy(r => r.Name); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 12 additions & 4 deletions
16
OdeToFoodVisualStudio/OdeToFoodVisualStudio/Views/Home/Index.cshtml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
@* VSRD: Visual Studio doesn't suggest importing the Restaurant model when typing @model Restaurant, so you have to type in the FQN. Let's see what Rider can do here :) *@ | ||
@model OdeToFoodVisualStudio.Models.Restaurant | ||
@model IEnumerable<OdeToFoodVisualStudio.Models.Restaurant> | ||
|
||
<html xmlns="http://www.w3.org/1999/xhtml"> | ||
<head> | ||
<title></title> | ||
</head> | ||
<body> | ||
@* VSRD: Visual Studio doesn't seem to provide Extend/Shrink selection in cshtml editor *@ | ||
<h1>@Model.Name</h1> | ||
<div>The ID value is @Model.Id</div> | ||
@* VSRD: Table generated in VS with a "table" code snippet *@ | ||
<table> | ||
@* VSRD: No 'foreach' code snippet in VS, just keyword completion *@ | ||
@foreach (var restaurant in Model) | ||
{ | ||
<tr> | ||
<td>@restaurant.Id</td> | ||
<td>@restaurant.Name</td> | ||
</tr> | ||
} | ||
</table> | ||
</body> | ||
</html> |