Вынес файл Repository.cs в папку Repositories

parent 534ca999
using Microsoft.AspNetCore.Mvc;
using MyCities.Models;
using MyCities.Repositories;
namespace MyCities.Controllers
{
public class HomeController : Controller
{
private IRepository _repository;
private ICityRepository _cityRepository;
public HomeController(IRepository repository)
public HomeController(ICityRepository cityRepository)
{
_repository = repository;
_cityRepository = cityRepository;
}
public IActionResult Index()
{
return View(_repository.Cities);
return View(_cityRepository.Cities);
}
[HttpGet]
......@@ -26,7 +27,7 @@ namespace MyCities.Controllers
[HttpPost]
public IActionResult Create(City city)
{
_repository.Add(city);
_cityRepository.Add(city);
return RedirectToAction("Index");
}
}
......
using Microsoft.AspNetCore.Mvc;
namespace MyCities.Controllers
{
public class PersonController : Controller
{
// GET
public IActionResult Index()
{
return View();
}
}
}
namespace MyCities.Models
{
public class Person
{
public string FullName { get; set; }
public string NickName { get; set; }
public string AvatarImagePath { get; set; }
}
}
using System.Collections.Generic;
using MyCities.Models;
namespace MyCities.Repositories
{
public interface ICityRepository
{
List<City> Cities { get; }
void Add(City city);
}
}
using System.Collections.Generic;
using MyCities.Models;
namespace MyCities.Models
namespace MyCities.Repositories
{
public interface IRepository
{
List<City> Cities { get; }
void Add(City city);
}
public class InMemoryRepository : IRepository
public class InMemoryCityRepository : ICityRepository
{
private List<City> _cities = new List<City>
{
......
......@@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using MyCities.Models;
using MyCities.Repositories;
namespace MyCities
{
......@@ -32,7 +33,7 @@ namespace MyCities
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddSingleton<IRepository, InMemoryRepository>();
services.AddSingleton<ICityRepository, InMemoryCityRepository>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
......
@model IEnumerable<MyCities.Models.City>
@{ Layout = "_Layout";}
<table class="table small table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Counry</th>
<th>Population</th>
</tr>
</thead>
<tbody>
@foreach (var city in Model)
{
<tr>
<td>@city.Name</td>
<td>@city.Country</td>
<td>@city.Population.ToString("#,###")</td>
</tr>
}
</tbody>
</table>
<a href="/Home/Create" class="btn btn-primary">Create city</a>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment