From 3a9e467b326eb1c181307beb212af51806d1dd67 Mon Sep 17 00:00:00 2001 From: Jake Sanchez Date: Mon, 6 Mar 2017 15:07:17 -0600 Subject: [PATCH 1/5] shared repo interface --- PokemonTracker/PokemonTracker.csproj | 1 + .../Interfaces/IPokemonRepository.cs | 18 ++------------ .../Repositories/Interfaces/IRepository.cs | 15 ++++++++++++ .../Interfaces/ITrainerRepository.cs | 15 +----------- .../Repositories/PokemonRepository.cs | 24 +++++++++---------- .../Repositories/TrainerRepository.cs | 23 +++++++++--------- PokemonTracker/Services/HomeService.cs | 4 ++-- 7 files changed, 45 insertions(+), 55 deletions(-) create mode 100644 PokemonTracker/Repositories/Interfaces/IRepository.cs diff --git a/PokemonTracker/PokemonTracker.csproj b/PokemonTracker/PokemonTracker.csproj index e896bf2..0730df3 100644 --- a/PokemonTracker/PokemonTracker.csproj +++ b/PokemonTracker/PokemonTracker.csproj @@ -148,6 +148,7 @@ + diff --git a/PokemonTracker/Repositories/Interfaces/IPokemonRepository.cs b/PokemonTracker/Repositories/Interfaces/IPokemonRepository.cs index 9a2b4fe..36572e8 100644 --- a/PokemonTracker/Repositories/Interfaces/IPokemonRepository.cs +++ b/PokemonTracker/Repositories/Interfaces/IPokemonRepository.cs @@ -1,21 +1,7 @@ using PokemonTracker.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace PokemonTracker.Repositories.Interfaces { - // don't take these return types to be the best way to do things - - // maybe you want to return bools or some status.. - // didn't include Update since this is a dummy - public interface IPokemonRepository - { - IEnumerable GetAll(); - IEnumerable SearchByKey(string key); - Pokemon GetById(int id); - void Insert(Pokemon pokemon); - void Delete(int id); - } + public interface IPokemonRepository : IRepository + { } } diff --git a/PokemonTracker/Repositories/Interfaces/IRepository.cs b/PokemonTracker/Repositories/Interfaces/IRepository.cs new file mode 100644 index 0000000..01bb5ad --- /dev/null +++ b/PokemonTracker/Repositories/Interfaces/IRepository.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; + +namespace PokemonTracker.Repositories.Interfaces +{ + public interface IRepository where T : class + { + IEnumerable GetAll(); + IEnumerable Where(Expression> where); + T Find(int id); + void Insert(T obj); + void Delete(T obj); + } +} diff --git a/PokemonTracker/Repositories/Interfaces/ITrainerRepository.cs b/PokemonTracker/Repositories/Interfaces/ITrainerRepository.cs index ada5b98..1f1d8f0 100644 --- a/PokemonTracker/Repositories/Interfaces/ITrainerRepository.cs +++ b/PokemonTracker/Repositories/Interfaces/ITrainerRepository.cs @@ -1,21 +1,8 @@ using PokemonTracker.Models; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace PokemonTracker.Repositories.Interfaces { - // don't take these return types to be the best way to do things - - // maybe you want to return bools or some status.. - // didn't include Update since this is a dummy - public interface ITrainerRepository + public interface ITrainerRepository : IRepository { - IEnumerable GetAll(); - IEnumerable SearchByKey(string key); - Trainer GetById(int id); - void Insert(Trainer trainer); - void Delete(int id); } } diff --git a/PokemonTracker/Repositories/PokemonRepository.cs b/PokemonTracker/Repositories/PokemonRepository.cs index 81949cb..1be4d36 100644 --- a/PokemonTracker/Repositories/PokemonRepository.cs +++ b/PokemonTracker/Repositories/PokemonRepository.cs @@ -2,8 +2,8 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Web; using PokemonTracker.Models; +using System.Linq.Expressions; namespace PokemonTracker.Repositories { @@ -16,32 +16,32 @@ public PokemonRepository() _context = new PokemonTrackerContext(); } - public void Delete(int id) + public void Delete(Pokemon obj) { - var pokemon = GetById(id); + var pokemon = Find(obj.Id); _context.Pokemons.Remove(pokemon); _context.SaveChanges(); } - public IEnumerable GetAll() + public Pokemon Find(int id) { - return _context.Pokemons.AsEnumerable(); + return _context.Pokemons.Find(id); } - public IEnumerable SearchByKey(string key) + public IEnumerable GetAll() { - return _context.Pokemons.Where(t => t.Name.Contains(key)); + return _context.Pokemons.AsEnumerable(); } - public Pokemon GetById(int id) + public void Insert(Pokemon obj) { - return _context.Pokemons.AsQueryable().Where(p => p.Id == id).FirstOrDefault(); + _context.Pokemons.Add(obj); + _context.SaveChanges(); } - public void Insert(Pokemon pokemon) + public IEnumerable Where(Expression> where) { - _context.Pokemons.Add(pokemon); - _context.SaveChanges(); + return GetAll().AsQueryable().Where(where); } } } \ No newline at end of file diff --git a/PokemonTracker/Repositories/TrainerRepository.cs b/PokemonTracker/Repositories/TrainerRepository.cs index 20f654a..af9ab16 100644 --- a/PokemonTracker/Repositories/TrainerRepository.cs +++ b/PokemonTracker/Repositories/TrainerRepository.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using System.Web; namespace PokemonTracker.Repositories @@ -16,32 +17,32 @@ public TrainerRepository() _context = new PokemonTrackerContext(); } - public void Delete(int index) + public void Delete(Trainer obj) { - var trainer = GetById(index); + var trainer = Find(obj.Id); _context.Trainers.Remove(trainer); _context.SaveChanges(); } - public IEnumerable GetAll() + public Trainer Find(int id) { - return _context.Trainers.AsEnumerable(); + return _context.Trainers.Find(id); } - public IEnumerable SearchByKey(string key) + public IEnumerable GetAll() { - return _context.Trainers.Where(t => t.Name.Contains(key)); + return _context.Trainers.AsEnumerable(); } - public Trainer GetById(int id) + public void Insert(Trainer obj) { - return _context.Trainers.AsQueryable().Where(t => t.Id == id).FirstOrDefault(); + _context.Trainers.Add(obj); + _context.SaveChanges(); } - public void Insert(Trainer trainer) + public IEnumerable Where(Expression> where) { - _context.Trainers.Add(trainer); - _context.SaveChanges(); + return GetAll().AsQueryable().Where(where); } } } \ No newline at end of file diff --git a/PokemonTracker/Services/HomeService.cs b/PokemonTracker/Services/HomeService.cs index 30c9619..a7e482c 100644 --- a/PokemonTracker/Services/HomeService.cs +++ b/PokemonTracker/Services/HomeService.cs @@ -34,8 +34,8 @@ public SearchViewModel GetSearchViewModel(string param) return new SearchViewModel() { SearchParam = param, - Pokemons = _pokemonRepo.SearchByKey(param), - Trainers = _trainerRepo.SearchByKey(param) + Pokemons = _pokemonRepo.Where(p => p.Name.Contains(param)), + Trainers = _trainerRepo.Where(t => t.Name.Contains(param)) }; } } From 5539bb44b5d6054dea6d9be2ecb0453f0be02d18 Mon Sep 17 00:00:00 2001 From: Jake Sanchez Date: Mon, 6 Mar 2017 15:19:27 -0600 Subject: [PATCH 2/5] Closes #5 - move view models to presentation layer --- PokemonTracker/Controllers/HomeController.cs | 31 +++++++++----- PokemonTracker/PokemonTracker.csproj | 6 ++- PokemonTracker/Services/HomeService.cs | 42 ------------------- .../Services/Interfaces/IHomeService.cs | 15 ------- .../Services/Interfaces/IPokemonService.cs | 11 +++++ .../Services/Interfaces/ITrainerService.cs | 11 +++++ PokemonTracker/Services/PokemonService.cs | 27 ++++++++++++ PokemonTracker/Services/TrainerService.cs | 28 +++++++++++++ 8 files changed, 102 insertions(+), 69 deletions(-) delete mode 100644 PokemonTracker/Services/HomeService.cs delete mode 100644 PokemonTracker/Services/Interfaces/IHomeService.cs create mode 100644 PokemonTracker/Services/Interfaces/IPokemonService.cs create mode 100644 PokemonTracker/Services/Interfaces/ITrainerService.cs create mode 100644 PokemonTracker/Services/PokemonService.cs create mode 100644 PokemonTracker/Services/TrainerService.cs diff --git a/PokemonTracker/Controllers/HomeController.cs b/PokemonTracker/Controllers/HomeController.cs index 4923ca3..8194c58 100644 --- a/PokemonTracker/Controllers/HomeController.cs +++ b/PokemonTracker/Controllers/HomeController.cs @@ -1,20 +1,20 @@ -using PokemonTracker.Services; +using PokemonTracker.Models.ViewModels; +using PokemonTracker.Services; using PokemonTracker.Services.Interfaces; using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; using System.Web.Mvc; namespace PokemonTracker.Controllers { public class HomeController : Controller { - private IHomeService _homeService; + private IPokemonService _pokemonService; + private ITrainerService _trainerService; public HomeController() { - _homeService = new HomeService(); + _pokemonService = new PokemonService(); + _trainerService = new TrainerService(); } // Http GET by default @@ -22,8 +22,13 @@ public HomeController() // Note how there is a Views/Home/Index.cshtml file - this is what View() references public ActionResult Index() { - var model = _homeService.GetHomeViewModel(); - return View(model); + var viewModel = new HomeViewModel() + { + Pokemons = _pokemonService.GetAllPokemon(), + Trainers = _trainerService.GetAllTrainers() + }; + + return View(viewModel); } // again, notice the corresponding Views/Home/Search file @@ -34,8 +39,14 @@ public ActionResult Search(string param) return RedirectToAction("Index"); } - var model = _homeService.GetSearchViewModel(param); - return View(model); + var viewModel = new SearchViewModel() + { + Pokemons = _pokemonService.SearchPokemonForKey(param), + Trainers = _trainerService.SearchTrainersForKey(param), + SearchParam = param + }; + + return View(viewModel); } } } \ No newline at end of file diff --git a/PokemonTracker/PokemonTracker.csproj b/PokemonTracker/PokemonTracker.csproj index 0730df3..7d7b057 100644 --- a/PokemonTracker/PokemonTracker.csproj +++ b/PokemonTracker/PokemonTracker.csproj @@ -152,8 +152,10 @@ - - + + + + diff --git a/PokemonTracker/Services/HomeService.cs b/PokemonTracker/Services/HomeService.cs deleted file mode 100644 index a7e482c..0000000 --- a/PokemonTracker/Services/HomeService.cs +++ /dev/null @@ -1,42 +0,0 @@ -using PokemonTracker.Services.Interfaces; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Web; -using PokemonTracker.Models.ViewModels; -using PokemonTracker.Repositories.Interfaces; -using PokemonTracker.Repositories; - -namespace PokemonTracker.Services -{ - public class HomeService : IHomeService - { - private IPokemonRepository _pokemonRepo; - private ITrainerRepository _trainerRepo; - - public HomeService() - { - _pokemonRepo = new PokemonRepository(); - _trainerRepo = new TrainerRepository(); - } - - public HomeViewModel GetHomeViewModel() - { - return new HomeViewModel() - { - Pokemons = _pokemonRepo.GetAll(), - Trainers = _trainerRepo.GetAll() - }; - } - - public SearchViewModel GetSearchViewModel(string param) - { - return new SearchViewModel() - { - SearchParam = param, - Pokemons = _pokemonRepo.Where(p => p.Name.Contains(param)), - Trainers = _trainerRepo.Where(t => t.Name.Contains(param)) - }; - } - } -} \ No newline at end of file diff --git a/PokemonTracker/Services/Interfaces/IHomeService.cs b/PokemonTracker/Services/Interfaces/IHomeService.cs deleted file mode 100644 index d5900ab..0000000 --- a/PokemonTracker/Services/Interfaces/IHomeService.cs +++ /dev/null @@ -1,15 +0,0 @@ -using PokemonTracker.Models.ViewModels; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace PokemonTracker.Services.Interfaces -{ - public interface IHomeService - { - HomeViewModel GetHomeViewModel(); - SearchViewModel GetSearchViewModel(string param); - } -} diff --git a/PokemonTracker/Services/Interfaces/IPokemonService.cs b/PokemonTracker/Services/Interfaces/IPokemonService.cs new file mode 100644 index 0000000..8f776fc --- /dev/null +++ b/PokemonTracker/Services/Interfaces/IPokemonService.cs @@ -0,0 +1,11 @@ +using PokemonTracker.Models; +using System.Collections.Generic; + +namespace PokemonTracker.Services.Interfaces +{ + public interface IPokemonService + { + IEnumerable GetAllPokemon(); + IEnumerable SearchPokemonForKey(string key); + } +} diff --git a/PokemonTracker/Services/Interfaces/ITrainerService.cs b/PokemonTracker/Services/Interfaces/ITrainerService.cs new file mode 100644 index 0000000..de53b7a --- /dev/null +++ b/PokemonTracker/Services/Interfaces/ITrainerService.cs @@ -0,0 +1,11 @@ +using PokemonTracker.Models; +using System.Collections.Generic; + +namespace PokemonTracker.Services.Interfaces +{ + public interface ITrainerService + { + IEnumerable GetAllTrainers(); + IEnumerable SearchTrainersForKey(string key); + } +} diff --git a/PokemonTracker/Services/PokemonService.cs b/PokemonTracker/Services/PokemonService.cs new file mode 100644 index 0000000..180ff08 --- /dev/null +++ b/PokemonTracker/Services/PokemonService.cs @@ -0,0 +1,27 @@ +using System.Collections.Generic; +using PokemonTracker.Models; +using PokemonTracker.Repositories.Interfaces; +using PokemonTracker.Repositories; + +namespace PokemonTracker.Services.Interfaces +{ + public class PokemonService : IPokemonService + { + private IPokemonRepository _pokemonRepo; + + public PokemonService() + { + _pokemonRepo = new PokemonRepository(); + } + + public IEnumerable GetAllPokemon() + { + return _pokemonRepo.GetAll(); + } + + public IEnumerable SearchPokemonForKey(string key) + { + return _pokemonRepo.Where(p => p.Name.Contains(key)); + } + } +} \ No newline at end of file diff --git a/PokemonTracker/Services/TrainerService.cs b/PokemonTracker/Services/TrainerService.cs new file mode 100644 index 0000000..eef3293 --- /dev/null +++ b/PokemonTracker/Services/TrainerService.cs @@ -0,0 +1,28 @@ +using PokemonTracker.Models; +using PokemonTracker.Repositories; +using PokemonTracker.Repositories.Interfaces; +using PokemonTracker.Services.Interfaces; +using System.Collections.Generic; + +namespace PokemonTracker.Services +{ + public class TrainerService : ITrainerService + { + private ITrainerRepository _trainerRepo; + + public TrainerService() + { + _trainerRepo = new TrainerRepository(); + } + + public IEnumerable GetAllTrainers() + { + return _trainerRepo.GetAll(); + } + + public IEnumerable SearchTrainersForKey(string key) + { + return _trainerRepo.Where(t => t.Name.Contains(key)); + } + } +} \ No newline at end of file From 79d1c96e29065e2e2a646e02cc1cc213b480060e Mon Sep 17 00:00:00 2001 From: Jake Sanchez Date: Mon, 6 Mar 2017 15:44:37 -0600 Subject: [PATCH 3/5] Closes #3 - installed unity and added dependency injection --- .../PokemonTracker.Tests.csproj | 3 ++ PokemonTracker.Tests/app.config | 11 +++++ PokemonTracker/App_Start/Bootstrapper.cs | 32 ++++++++++++++ PokemonTracker/App_Start/RouteConfig.cs | 6 +-- PokemonTracker/App_Start/UnityConfig.cs | 42 +++++++++++++++++++ PokemonTracker/App_Start/UnityMvcActivator.cs | 34 +++++++++++++++ PokemonTracker/Controllers/HomeController.cs | 7 ++-- PokemonTracker/Global.asax.cs | 7 +--- PokemonTracker/PokemonTracker.csproj | 27 ++++++++++++ PokemonTracker/Services/PokemonService.cs | 5 +-- PokemonTracker/Services/TrainerService.cs | 5 +-- PokemonTracker/Web.config | 2 +- PokemonTracker/packages.config | 4 ++ 13 files changed, 164 insertions(+), 21 deletions(-) create mode 100644 PokemonTracker.Tests/app.config create mode 100644 PokemonTracker/App_Start/Bootstrapper.cs create mode 100644 PokemonTracker/App_Start/UnityConfig.cs create mode 100644 PokemonTracker/App_Start/UnityMvcActivator.cs diff --git a/PokemonTracker.Tests/PokemonTracker.Tests.csproj b/PokemonTracker.Tests/PokemonTracker.Tests.csproj index abb2572..5dedde5 100644 --- a/PokemonTracker.Tests/PokemonTracker.Tests.csproj +++ b/PokemonTracker.Tests/PokemonTracker.Tests.csproj @@ -57,6 +57,9 @@ PokemonTracker + + +