Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Architecture Refactor #6

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions PokemonTracker.Tests/Mocks/MockPokemonRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using PokemonTracker.Models;
using PokemonTracker.Repositories.Interfaces;
using System.Linq;

namespace PokemonTracker.Tests.Mocks
{
public class MockPokemonRepository : IPokemonRepository
{
private List<Pokemon> _allPokemon;
public MockPokemonRepository(List<Pokemon> allPokemon)
{
_allPokemon = allPokemon;
}

public void Delete(Pokemon obj)
{
var pokemon = Find(obj.Id);
_allPokemon.Remove(pokemon);
}

public Pokemon Find(int id)
{
return _allPokemon.Find(p => p.Id == id);
}

public IEnumerable<Pokemon> GetAll()
{
return _allPokemon;
}

public void Insert(Pokemon obj)
{
_allPokemon.Add(obj);
}

public IEnumerable<Pokemon> Where(Expression<Func<Pokemon, bool>> where)
{
return _allPokemon.AsQueryable().Where(where).AsEnumerable();
}
}
}
72 changes: 72 additions & 0 deletions PokemonTracker.Tests/PokemonServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using PokemonTracker.Services.Interfaces;
using PokemonTracker.Tests.Mocks;
using PokemonTracker.Models;
using System.Collections.Generic;
using System.Linq;

namespace PokemonTracker.Tests
{
[TestClass]
public class PokemonServiceTests
{
public static List<Pokemon> pokemonList = new List<Pokemon>
{
new Pokemon { Id = 1, Name = "Bulbasaur", Type = PokemonType.GRASS },
new Pokemon { Id = 2, Name = "Charmander", Type = PokemonType.FIRE },
new Pokemon { Id = 3, Name = "Squirtle", Type = PokemonType.WATER },
new Pokemon { Id = 4, Name = "Pikachu", Type = PokemonType.ELECTRIC },
new Pokemon { Id = 5, Name = "Jigglypuff", Type = PokemonType.FAIRY },
new Pokemon { Id = 6, Name = "Growlithe", Type = PokemonType.FIRE },
new Pokemon { Id = 7, Name = "Machamp", Type = PokemonType.FIGHTING },
new Pokemon { Id = 8, Name = "Magikarp", Type = PokemonType.WATER },
new Pokemon { Id = 9, Name = "Gyarados", Type = PokemonType.WATER },
new Pokemon { Id = 10, Name = "Dragonite", Type = PokemonType.DRAGON }
};

public static List<Pokemon> pokemonWithT = new List<Pokemon>
{
new Pokemon { Id = 3, Name = "Squirtle", Type = PokemonType.WATER },
new Pokemon { Id = 6, Name = "Growlithe", Type = PokemonType.FIRE },
new Pokemon { Id = 10, Name = "Dragonite", Type = PokemonType.DRAGON }
};

private PokemonService GetService()
{
var mockRepo = new MockPokemonRepository(pokemonList);
return new PokemonService(mockRepo);
}

[TestMethod]
public void TestGetAllPokemon()
{
var service = GetService();
var allPokemon = service.GetAllPokemon();
CompareLists(allPokemon.ToList(), pokemonList);
}

[TestMethod]
public void TestSearchPokemon()
{
var service = GetService();
var pokemonSearchedByT = service.SearchPokemonForKey("t");
CompareLists(pokemonSearchedByT.ToList(), pokemonWithT);
}

private void CompareLists(List<Pokemon> actual, List<Pokemon> expected)
{
Assert.AreEqual(actual.Count, expected.Count);

for (var i = 0; i < actual.Count; i++)
{
var a = actual.ElementAt(i);
var e = expected.ElementAt(i);

Assert.AreEqual(a.Id, e.Id);
Assert.AreEqual(a.Name, e.Name);
Assert.AreEqual(a.Type, e.Type);
}
}
}
}
6 changes: 5 additions & 1 deletion PokemonTracker.Tests/PokemonTracker.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
<Reference Include="System.Xml.Linq" />
</ItemGroup>
<ItemGroup>
<Compile Include="UnitTest1.cs" />
<Compile Include="Mocks\MockPokemonRepository.cs" />
<Compile Include="PokemonServiceTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -57,6 +58,9 @@
<Name>PokemonTracker</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
14 changes: 0 additions & 14 deletions PokemonTracker.Tests/UnitTest1.cs

This file was deleted.

11 changes: 11 additions & 0 deletions PokemonTracker.Tests/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
32 changes: 32 additions & 0 deletions PokemonTracker/App_Start/Bootstrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Mvc;
using PokemonTracker.Repositories;
using PokemonTracker.Repositories.Interfaces;
using PokemonTracker.Services;
using PokemonTracker.Services.Interfaces;
using System.Web.Mvc;

namespace PokemonTracker
{
public class Bootstrapper
{
public static void Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}

private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();

container.RegisterType<IPokemonRepository, PokemonRepository>();
container.RegisterType<ITrainerRepository, TrainerRepository>();

container.RegisterType<IPokemonService, PokemonService>();
container.RegisterType<ITrainerService, TrainerService>();

return container;
}
}
}
6 changes: 1 addition & 5 deletions PokemonTracker/App_Start/RouteConfig.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc;
using System.Web.Routing;

namespace PokemonTracker
Expand Down
42 changes: 42 additions & 0 deletions PokemonTracker/App_Start/UnityConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace PokemonTracker.App_Start
{
/// <summary>
/// Specifies the Unity configuration for the main container.
/// </summary>
public class UnityConfig
{
#region Unity Container
private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
});

/// <summary>
/// Gets the configured Unity container.
/// </summary>
public static IUnityContainer GetConfiguredContainer()
{
return container.Value;
}
#endregion

/// <summary>Registers the type mappings with the Unity container.</summary>
/// <param name="container">The unity container to configure.</param>
/// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to
/// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
public static void RegisterTypes(IUnityContainer container)
{
// NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
// container.LoadConfiguration();

// TODO: Register your types here
// container.RegisterType<IProductRepository, ProductRepository>();
}
}
}
34 changes: 34 additions & 0 deletions PokemonTracker/App_Start/UnityMvcActivator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Linq;
using System.Web.Mvc;
using Microsoft.Practices.Unity.Mvc;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(PokemonTracker.App_Start.UnityWebActivator), "Start")]
[assembly: WebActivatorEx.ApplicationShutdownMethod(typeof(PokemonTracker.App_Start.UnityWebActivator), "Shutdown")]

namespace PokemonTracker.App_Start
{
/// <summary>Provides the bootstrapping for integrating Unity with ASP.NET MVC.</summary>
public static class UnityWebActivator
{
/// <summary>Integrates Unity when the application starts.</summary>
public static void Start()
{
var container = UnityConfig.GetConfiguredContainer();

FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));

DependencyResolver.SetResolver(new UnityDependencyResolver(container));

// TODO: Uncomment if you want to use PerRequestLifetimeManager
// Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
}

/// <summary>Disposes the Unity container when the application is shut down.</summary>
public static void Shutdown()
{
var container = UnityConfig.GetConfiguredContainer();
container.Dispose();
}
}
}
32 changes: 21 additions & 11 deletions PokemonTracker/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
using PokemonTracker.Services;
using PokemonTracker.Models.ViewModels;
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()
public HomeController(IPokemonService pokemonService, ITrainerService trainerService)
{
_homeService = new HomeService();
_pokemonService = pokemonService;
_trainerService = trainerService;
}

// Http GET by default
// '/' will map to 'Home/Index' : check App_Start/RouteConfig
// 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
Expand All @@ -34,8 +38,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);
}
}
}
7 changes: 2 additions & 5 deletions PokemonTracker/Global.asax.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc;
using System.Web.Routing;

namespace PokemonTracker
Expand All @@ -13,6 +9,7 @@ protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
Bootstrapper.Initialise();
}
}
}
Loading