Skip to content

Commit

Permalink
Creating new services: IRestaurantData, InMemoryRestaurantData, regis…
Browse files Browse the repository at this point in the history
…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
gorohoroh committed Jul 4, 2018
1 parent d80c2e6 commit 47fe289
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 11 deletions.
13 changes: 11 additions & 2 deletions OdeToFoodRider/OdeToFoodRider/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
using Microsoft.AspNetCore.Mvc;
using OdeToFoodRider.Models;
using OdeToFoodRider.Services;

namespace OdeToFoodRider.Controllers
{
public class HomeController : Controller
{
// VSRD: This time, "Initialize field from constructor" does exactly what we want.
IRestaurantData _restaurantData;

public HomeController(IRestaurantData restaurantData)
{
_restaurantData = restaurantData;
}


public IActionResult Index()
{
var model = new Restaurant() {Id = 1, Name = "Scott's Pizza Place"};

var model = _restaurantData.GetAll();
return View(model);
}
}
Expand Down
11 changes: 11 additions & 0 deletions OdeToFoodRider/OdeToFoodRider/Services/IRestaurantData.cs
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 OdeToFoodRider/OdeToFoodRider/Services/InMemoryRestaurantData.cs
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);
}
}
}
1 change: 1 addition & 0 deletions OdeToFoodRider/OdeToFoodRider/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class Startup
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IGreeter, Greeter>();
services.AddScoped<IRestaurantData, InMemoryRestaurantData>();
services.AddMvc();

}
Expand Down
17 changes: 14 additions & 3 deletions OdeToFoodRider/OdeToFoodRider/Views/Home/Index.cshtml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using OdeToFoodRider.Models
@model Restaurant
@model IEnumerable<Restaurant>

<!DOCTYPE html>

Expand All @@ -8,7 +8,18 @@
<title>My title</title>
</head>
<body>
<h1>@Model.Name</h1>
<div>The ID value is @Model.Id</div>
@* VSRD: No table snippet in Rider. *@
<table>

@* VSRD: No foreach live template here, just keyword completion like in Visual Studio *@
@foreach (var restaurant in Model)
{
<tr>
<td>@restaurant.Id</td>
<td>@restaurant.Name</td>
</tr>
}
</table>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
using Microsoft.AspNetCore.Mvc;
using OdeToFoodVisualStudio.Models;
using OdeToFoodVisualStudio.Services;

namespace OdeToFoodVisualStudio.Controllers
{
public class HomeController : Controller
{
private IRestaurantData _restaurantData;

// VSRD: Visual Studio doesn't have import items in completion, which means that here and in other cases when referencing an unimported type, you have to make sure to spell
// and capitalize it correctly, and then use a quick action to add an import. In Rider, import items are available in completion, which allows using camelHumps and abbreviations
// without being precise with naming, and additionally, accepting an import symbol suggestion adds the necessary using statement without the need to explicitly invoke a quick action.
public HomeController(IRestaurantData restaurantData)
{
// VSRD: VS provides a set of quick actions to generate _restaurantData (as a full or read-only field, full or read-only property, local variable), as well as explicit actions
// to change _restaurantData to IRestaurantData or restaurantData
_restaurantData = restaurantData;
}

public IActionResult Index()
{
var model = new Restaurant { Id = 1, Name = "Scott's Pizza Place" };
var model = _restaurantData.GetAll();

return View(model);

Expand Down
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();
}
}
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);
}
}
}
3 changes: 2 additions & 1 deletion OdeToFoodVisualStudio/OdeToFoodVisualStudio/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public void ConfigureServices(IServiceCollection services)
{
// RDVS: Rider's completion works better with this generic method as it additionally adds the parentheses; additionally, VS overlaps the completion list inside the generic brackets
// with with parameter info
services.AddSingleton<IGreeter, Greeter>();
services.AddSingleton<IGreeter, Greeter>(); // singleton scope
services.AddScoped<IRestaurantData, InMemoryRestaurantData>(); // per-HTTP-request lifeime
services.AddMvc();
}

Expand Down
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>

0 comments on commit 47fe289

Please sign in to comment.