Commit 64826b79 authored by Scott DePouw's avatar Scott DePouw Committed by Steve Smith

Make IRepository Generic at Method Level (#24)

* Generic paramter on repository lowered to methods so 1 repo instance per class is all that's required.

* Fixed code to use new repository method signatures.
parent 92eb03a0
...@@ -6,26 +6,27 @@ namespace CleanArchitecture.Core ...@@ -6,26 +6,27 @@ namespace CleanArchitecture.Core
{ {
public static class DatabasePopulator public static class DatabasePopulator
{ {
public static int PopulateDatabase(IRepository<ToDoItem> todoRepository) public static int PopulateDatabase(IRepository todoRepository)
{ {
if (todoRepository.List().Any()) return 0; if (todoRepository.List<ToDoItem>().Any()) return 0;
todoRepository.Add(new ToDoItem() todoRepository.Add(new ToDoItem
{ {
Title = "Get Sample Working", Title = "Get Sample Working",
Description = "Try to get the sample to build." Description = "Try to get the sample to build."
}); });
todoRepository.Add(new ToDoItem() todoRepository.Add(new ToDoItem
{ {
Title = "Review Solution", Title = "Review Solution",
Description = "Review the different projects in the solution and how they relate to one another." Description = "Review the different projects in the solution and how they relate to one another."
}); });
todoRepository.Add(new ToDoItem() todoRepository.Add(new ToDoItem
{ {
Title = "Run and Review Tests", Title = "Run and Review Tests",
Description = "Make sure all the tests run and review what they are doing." Description = "Make sure all the tests run and review what they are doing."
}); });
return todoRepository.List().Count;
return todoRepository.List<ToDoItem>().Count;
} }
} }
} }
using System.Collections.Generic; using CleanArchitecture.Core.SharedKernel;
using CleanArchitecture.Core.SharedKernel; using System.Collections.Generic;
namespace CleanArchitecture.Core.Interfaces namespace CleanArchitecture.Core.Interfaces
{ {
public interface IRepository<T> where T : BaseEntity public interface IRepository
{ {
T GetById(int id); T GetById<T>(int id) where T : BaseEntity;
List<T> List(); List<T> List<T>() where T : BaseEntity;
T Add(T entity); T Add<T>(T entity) where T : BaseEntity;
void Update(T entity); void Update<T>(T entity) where T : BaseEntity;
void Delete(T entity); void Delete<T>(T entity) where T : BaseEntity;
} }
} }
using System.Collections.Generic; using CleanArchitecture.Core.Interfaces;
using System.Linq;
using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Core.SharedKernel; using CleanArchitecture.Core.SharedKernel;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
namespace CleanArchitecture.Infrastructure.Data namespace CleanArchitecture.Infrastructure.Data
{ {
public class EfRepository<T> : IRepository<T> where T : BaseEntity public class EfRepository : IRepository
{ {
private readonly AppDbContext _dbContext; private readonly AppDbContext _dbContext;
...@@ -15,17 +15,17 @@ namespace CleanArchitecture.Infrastructure.Data ...@@ -15,17 +15,17 @@ namespace CleanArchitecture.Infrastructure.Data
_dbContext = dbContext; _dbContext = dbContext;
} }
public T GetById(int id) public T GetById<T>(int id) where T : BaseEntity
{ {
return _dbContext.Set<T>().SingleOrDefault(e => e.Id == id); return _dbContext.Set<T>().SingleOrDefault(e => e.Id == id);
} }
public List<T> List() public List<T> List<T>() where T : BaseEntity
{ {
return _dbContext.Set<T>().ToList(); return _dbContext.Set<T>().ToList();
} }
public T Add(T entity) public T Add<T>(T entity) where T : BaseEntity
{ {
_dbContext.Set<T>().Add(entity); _dbContext.Set<T>().Add(entity);
_dbContext.SaveChanges(); _dbContext.SaveChanges();
...@@ -33,13 +33,13 @@ namespace CleanArchitecture.Infrastructure.Data ...@@ -33,13 +33,13 @@ namespace CleanArchitecture.Infrastructure.Data
return entity; return entity;
} }
public void Delete(T entity) public void Delete<T>(T entity) where T : BaseEntity
{ {
_dbContext.Set<T>().Remove(entity); _dbContext.Set<T>().Remove(entity);
_dbContext.SaveChanges(); _dbContext.SaveChanges();
} }
public void Update(T entity) public void Update<T>(T entity) where T : BaseEntity
{ {
_dbContext.Entry(entity).State = EntityState.Modified; _dbContext.Entry(entity).State = EntityState.Modified;
_dbContext.SaveChanges(); _dbContext.SaveChanges();
......
using System; using CleanArchitecture.Core.Entities;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Interfaces; using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Web.ApiModels; using CleanArchitecture.Web.ApiModels;
using CleanArchitecture.Web.Filters; using CleanArchitecture.Web.Filters;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using System.Linq;
namespace CleanArchitecture.Web.Api namespace CleanArchitecture.Web.Api
{ {
...@@ -14,19 +11,19 @@ namespace CleanArchitecture.Web.Api ...@@ -14,19 +11,19 @@ namespace CleanArchitecture.Web.Api
[ValidateModel] [ValidateModel]
public class ToDoItemsController : Controller public class ToDoItemsController : Controller
{ {
private readonly IRepository<ToDoItem> _todoRepository; private readonly IRepository _repository;
public ToDoItemsController(IRepository<ToDoItem> todoRepository) public ToDoItemsController(IRepository repository)
{ {
_todoRepository = todoRepository; _repository = repository;
} }
// GET: api/ToDoItems // GET: api/ToDoItems
[HttpGet] [HttpGet]
public IActionResult List() public IActionResult List()
{ {
var items = _todoRepository.List() var items = _repository.List<ToDoItem>()
.Select(item => ToDoItemDTO.FromToDoItem(item)); .Select(ToDoItemDTO.FromToDoItem);
return Ok(items); return Ok(items);
} }
...@@ -34,7 +31,7 @@ namespace CleanArchitecture.Web.Api ...@@ -34,7 +31,7 @@ namespace CleanArchitecture.Web.Api
[HttpGet("{id:int}")] [HttpGet("{id:int}")]
public IActionResult GetById(int id) public IActionResult GetById(int id)
{ {
var item = ToDoItemDTO.FromToDoItem(_todoRepository.GetById(id)); var item = ToDoItemDTO.FromToDoItem(_repository.GetById<ToDoItem>(id));
return Ok(item); return Ok(item);
} }
...@@ -47,16 +44,16 @@ namespace CleanArchitecture.Web.Api ...@@ -47,16 +44,16 @@ namespace CleanArchitecture.Web.Api
Title = item.Title, Title = item.Title,
Description = item.Description Description = item.Description
}; };
_todoRepository.Add(todoItem); _repository.Add(todoItem);
return Ok(ToDoItemDTO.FromToDoItem(todoItem)); return Ok(ToDoItemDTO.FromToDoItem(todoItem));
} }
[HttpPatch("{id:int}/complete")] [HttpPatch("{id:int}/complete")]
public IActionResult Complete(int id) public IActionResult Complete(int id)
{ {
var toDoItem = _todoRepository.GetById(id); var toDoItem = _repository.GetById<ToDoItem>(id);
toDoItem.MarkComplete(); toDoItem.MarkComplete();
_todoRepository.Update(toDoItem); _repository.Update(toDoItem);
return Ok(ToDoItemDTO.FromToDoItem(toDoItem)); return Ok(ToDoItemDTO.FromToDoItem(toDoItem));
} }
......
...@@ -7,22 +7,22 @@ namespace CleanArchitecture.Web.Controllers ...@@ -7,22 +7,22 @@ namespace CleanArchitecture.Web.Controllers
{ {
public class ToDoController : Controller public class ToDoController : Controller
{ {
private readonly IRepository<ToDoItem> _todoRepository; private readonly IRepository _repository;
public ToDoController(IRepository<ToDoItem> todoRepository) public ToDoController(IRepository repository)
{ {
_todoRepository = todoRepository; _repository = repository;
} }
public IActionResult Index() public IActionResult Index()
{ {
var items = _todoRepository.List(); var items = _repository.List<ToDoItem>();
return View(items); return View(items);
} }
public IActionResult Populate() public IActionResult Populate()
{ {
int recordsAdded = DatabasePopulator.PopulateDatabase(_todoRepository); int recordsAdded = DatabasePopulator.PopulateDatabase(_repository);
return Ok(recordsAdded); return Ok(recordsAdded);
} }
} }
......
...@@ -7,18 +7,18 @@ namespace CleanArchitecture.Web.Pages.ToDoRazorPage ...@@ -7,18 +7,18 @@ namespace CleanArchitecture.Web.Pages.ToDoRazorPage
{ {
public class IndexModel : PageModel public class IndexModel : PageModel
{ {
private readonly IRepository<ToDoItem> _todoRepository; private readonly IRepository _repository;
public List<ToDoItem> ToDoItems { get; set; } public List<ToDoItem> ToDoItems { get; set; }
public IndexModel(IRepository<ToDoItem> todoRepository) public IndexModel(IRepository repository)
{ {
_todoRepository = todoRepository; _repository = repository;
} }
public void OnGet() public void OnGet()
{ {
ToDoItems = _todoRepository.List(); ToDoItems = _repository.List<ToDoItem>();
} }
} }
} }
using CleanArchitecture.Core; using CleanArchitecture.Core;
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Interfaces; using CleanArchitecture.Core.Interfaces;
using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.RazorPages;
...@@ -7,18 +6,18 @@ namespace CleanArchitecture.Web.Pages.ToDoRazorPage ...@@ -7,18 +6,18 @@ namespace CleanArchitecture.Web.Pages.ToDoRazorPage
{ {
public class PopulateModel : PageModel public class PopulateModel : PageModel
{ {
private readonly IRepository<ToDoItem> _todoRepository; private readonly IRepository _repository;
public PopulateModel(IRepository<ToDoItem> todoRepository) public PopulateModel(IRepository repository)
{ {
_todoRepository = todoRepository; _repository = repository;
} }
public int RecordsAdded { get; set; } public int RecordsAdded { get; set; }
public void OnGet() public void OnGet()
{ {
RecordsAdded = DatabasePopulator.PopulateDatabase(_todoRepository); RecordsAdded = DatabasePopulator.PopulateDatabase(_repository);
} }
} }
} }
...@@ -63,7 +63,7 @@ namespace CleanArchitecture.Web ...@@ -63,7 +63,7 @@ namespace CleanArchitecture.Web
// TODO: Add Registry Classes to eliminate reference to Infrastructure // TODO: Add Registry Classes to eliminate reference to Infrastructure
// TODO: Move to Infrastucture Registry // TODO: Move to Infrastucture Registry
config.For(typeof(IRepository<>)).Add(typeof(EfRepository<>)); config.For<IRepository>().Add<EfRepository>();
//Populate the container using the service collection //Populate the container using the service collection
config.Populate(services); config.Populate(services);
......
...@@ -39,7 +39,7 @@ namespace CleanArchitecture.Tests.Integration.Data ...@@ -39,7 +39,7 @@ namespace CleanArchitecture.Tests.Integration.Data
repository.Add(item); repository.Add(item);
var newItem = repository.List().FirstOrDefault(); var newItem = repository.List<ToDoItem>().FirstOrDefault();
Assert.Equal(item, newItem); Assert.Equal(item, newItem);
Assert.True(newItem?.Id > 0); Assert.True(newItem?.Id > 0);
...@@ -59,7 +59,7 @@ namespace CleanArchitecture.Tests.Integration.Data ...@@ -59,7 +59,7 @@ namespace CleanArchitecture.Tests.Integration.Data
_dbContext.Entry(item).State = EntityState.Detached; _dbContext.Entry(item).State = EntityState.Detached;
// fetch the item and update its title // fetch the item and update its title
var newItem = repository.List() var newItem = repository.List<ToDoItem>()
.FirstOrDefault(i => i.Title == initialTitle); .FirstOrDefault(i => i.Title == initialTitle);
Assert.NotNull(newItem); Assert.NotNull(newItem);
Assert.NotSame(item, newItem); Assert.NotSame(item, newItem);
...@@ -68,7 +68,7 @@ namespace CleanArchitecture.Tests.Integration.Data ...@@ -68,7 +68,7 @@ namespace CleanArchitecture.Tests.Integration.Data
// Update the item // Update the item
repository.Update(newItem); repository.Update(newItem);
var updatedItem = repository.List() var updatedItem = repository.List<ToDoItem>()
.FirstOrDefault(i => i.Title == newTitle); .FirstOrDefault(i => i.Title == newTitle);
Assert.NotNull(updatedItem); Assert.NotNull(updatedItem);
...@@ -89,17 +89,17 @@ namespace CleanArchitecture.Tests.Integration.Data ...@@ -89,17 +89,17 @@ namespace CleanArchitecture.Tests.Integration.Data
repository.Delete(item); repository.Delete(item);
// verify it's no longer there // verify it's no longer there
Assert.DoesNotContain(repository.List(), Assert.DoesNotContain(repository.List<ToDoItem>(),
i => i.Title == initialTitle); i => i.Title == initialTitle);
} }
private EfRepository<ToDoItem> GetRepository() private EfRepository GetRepository()
{ {
var options = CreateNewContextOptions(); var options = CreateNewContextOptions();
var mockDispatcher = new Mock<IDomainEventDispatcher>(); var mockDispatcher = new Mock<IDomainEventDispatcher>();
_dbContext = new AppDbContext(options, mockDispatcher.Object); _dbContext = new AppDbContext(options, mockDispatcher.Object);
return new EfRepository<ToDoItem>(_dbContext); return new EfRepository(_dbContext);
} }
} }
} }
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