Notes from building a first ASP.Net Core App (part 8)

  1. To change it to a list of hotels, the first thing I want to do is set up a dummy data source for the hotels. That’s because I’m not actually getting the data from a database. I want to be able to switch the code over to a database at a later stage, and I don’t want the application to know when I do that. This is important for later, as it is this concept that allows us to independently unit test components.
  2. I have added a HotelDataService class to a Services folder at the top level of my Solution:
    my-first-asp-net-core-app-folder-structure-3x
  3. Inside the HotelDataService, I have the following code:
    namespace MyFirstAspNetCoreApp.Services
    {
        public interface IHotelDataService
        {
            IEnumerable<Hotel> GetAll();
        }
    
        public class DummyHotelData : IHotelDataService
        {
            IEnumerable<Hotel> hotels;
            public DummyHotelData()
            {
                hotels = new List<Hotel>
                {
                    new Hotel { Id = 1, DisplayName = "Sofitel" },
                    new Hotel { Id = 2, DisplayName = "Westin" },
                    new Hotel { Id = 3, DisplayName = "Novotel" }
                };
            }
    
            public IEnumerable<Hotel> GetAll()
            {
                return this.hotels;
            }
        }
    }
    
  4. By using an interface, we can separate the source of data from the delivery of that data.
  5. Next, we need to tell the application that this is a service to be injected into a page. And we need to tell it that the data that we want for now is the Dummy Hotel Data. That’s done back in the Startup class, in the ConfigureServices method, as follows:
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();
        services.AddSingleton<IMyCustomConfiguration, MyCustomConfiguration>();
        services.AddSingleton(implementationFactory => Configuration);
    
        services.AddScoped<IHotelDataService, DummyHotelData>();
    }
    

    Note the AddScoped method. This tells the application that each http request should get its own instance of that service, whatever the IHotelDataService supplies, which in this case is DummyHotelData.

  6. Back in the controller, the hotel data service can now by injected via the constructor. Remember, that’s the pattern with dependency injection now:
    namespace MyFirstAspNetCoreApp.Controllers
    {
        public class HomeController : Controller
        {
            private IHotelDataService hotelData;
    
            public HomeController(IHotelDataService hotelData)
            {
                this.hotelData = hotelData;
            }
            public IActionResult Index()
            {
                var model = hotelData.GetAll();
                return View(model);
            }
        }
    }
    

    See the addition of the HomeController constructor, with the hotel data service interface? The application understands that it needs to look up the list of services to obtain an instance from IHotelDataService. It is stored in a local variable that can then be used on the page.
    The model is then set to retrieve the data from the data source on line 13.

  7. Finally, I update the Index.cshtml page to strongly type the IEnumerable and also to display the entire list of hotels:
    @model IEnumerable<MyFirstAspNetCoreApp.Models.Hotel>
    <html>
    <head>
        <title>Home</title>
    </head>
    <body>
        <h1>Welcome!</h1>
        <ul>
            @foreach (var hotel in Model)
            {
            <li>
                @hotel.DisplayName
            </li>
            }
        </ul>
    </body>
    </html>
    

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: