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
{
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",
Description = "Try to get the sample to build."
});
todoRepository.Add(new ToDoItem()
todoRepository.Add(new ToDoItem
{
Title = "Review Solution",
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",
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
{
public interface IRepository<T> where T : BaseEntity
public interface IRepository
{
T GetById(int id);
List<T> List();
T Add(T entity);
void Update(T entity);
void Delete(T entity);
T GetById<T>(int id) where T : BaseEntity;
List<T> List<T>() where T : BaseEntity;
T Add<T>(T entity) where T : BaseEntity;
void Update<T>(T entity) where T : BaseEntity;
void Delete<T>(T entity) where T : BaseEntity;
}
}
\ No newline at end of file
}
using System.Collections.Generic;
using System.Linq;
using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Core.SharedKernel;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
namespace CleanArchitecture.Infrastructure.Data
{
public class EfRepository<T> : IRepository<T> where T : BaseEntity
public class EfRepository : IRepository
{
private readonly AppDbContext _dbContext;
......@@ -15,17 +15,17 @@ namespace CleanArchitecture.Infrastructure.Data
_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);
}
public List<T> List()
public List<T> List<T>() where T : BaseEntity
{
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.SaveChanges();
......@@ -33,16 +33,16 @@ namespace CleanArchitecture.Infrastructure.Data
return entity;
}
public void Delete(T entity)
public void Delete<T>(T entity) where T : BaseEntity
{
_dbContext.Set<T>().Remove(entity);
_dbContext.SaveChanges();
}
public void Update(T entity)
public void Update<T>(T entity) where T : BaseEntity
{
_dbContext.Entry(entity).State = EntityState.Modified;
_dbContext.SaveChanges();
}
}
}
\ No newline at end of file
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Interfaces;
using CleanArchitecture.Web.ApiModels;
using CleanArchitecture.Web.Filters;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
namespace CleanArchitecture.Web.Api
{
......@@ -14,19 +11,19 @@ namespace CleanArchitecture.Web.Api
[ValidateModel]
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
[HttpGet]
public IActionResult List()
{
var items = _todoRepository.List()
.Select(item => ToDoItemDTO.FromToDoItem(item));
var items = _repository.List<ToDoItem>()
.Select(ToDoItemDTO.FromToDoItem);
return Ok(items);
}
......@@ -34,7 +31,7 @@ namespace CleanArchitecture.Web.Api
[HttpGet("{id:int}")]
public IActionResult GetById(int id)
{
var item = ToDoItemDTO.FromToDoItem(_todoRepository.GetById(id));
var item = ToDoItemDTO.FromToDoItem(_repository.GetById<ToDoItem>(id));
return Ok(item);
}
......@@ -47,16 +44,16 @@ namespace CleanArchitecture.Web.Api
Title = item.Title,
Description = item.Description
};
_todoRepository.Add(todoItem);
_repository.Add(todoItem);
return Ok(ToDoItemDTO.FromToDoItem(todoItem));
}
[HttpPatch("{id:int}/complete")]
public IActionResult Complete(int id)
{
var toDoItem = _todoRepository.GetById(id);
var toDoItem = _repository.GetById<ToDoItem>(id);
toDoItem.MarkComplete();
_todoRepository.Update(toDoItem);
_repository.Update(toDoItem);
return Ok(ToDoItemDTO.FromToDoItem(toDoItem));
}
......
......@@ -7,22 +7,22 @@ namespace CleanArchitecture.Web.Controllers
{
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()
{
var items = _todoRepository.List();
var items = _repository.List<ToDoItem>();
return View(items);
}
public IActionResult Populate()
{
int recordsAdded = DatabasePopulator.PopulateDatabase(_todoRepository);
int recordsAdded = DatabasePopulator.PopulateDatabase(_repository);
return Ok(recordsAdded);
}
}
......
......@@ -7,18 +7,18 @@ namespace CleanArchitecture.Web.Pages.ToDoRazorPage
{
public class IndexModel : PageModel
{
private readonly IRepository<ToDoItem> _todoRepository;
private readonly IRepository _repository;
public List<ToDoItem> ToDoItems { get; set; }
public IndexModel(IRepository<ToDoItem> todoRepository)
public IndexModel(IRepository repository)
{
_todoRepository = todoRepository;
_repository = repository;
}
public void OnGet()
{
ToDoItems = _todoRepository.List();
ToDoItems = _repository.List<ToDoItem>();
}
}
}
using CleanArchitecture.Core;
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Interfaces;
using Microsoft.AspNetCore.Mvc.RazorPages;
......@@ -7,18 +6,18 @@ namespace CleanArchitecture.Web.Pages.ToDoRazorPage
{
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 void OnGet()
{
RecordsAdded = DatabasePopulator.PopulateDatabase(_todoRepository);
RecordsAdded = DatabasePopulator.PopulateDatabase(_repository);
}
}
}
......@@ -63,7 +63,7 @@ namespace CleanArchitecture.Web
// TODO: Add Registry Classes to eliminate reference to Infrastructure
// TODO: Move to Infrastucture Registry
config.For(typeof(IRepository<>)).Add(typeof(EfRepository<>));
config.For<IRepository>().Add<EfRepository>();
//Populate the container using the service collection
config.Populate(services);
......
......@@ -39,7 +39,7 @@ namespace CleanArchitecture.Tests.Integration.Data
repository.Add(item);
var newItem = repository.List().FirstOrDefault();
var newItem = repository.List<ToDoItem>().FirstOrDefault();
Assert.Equal(item, newItem);
Assert.True(newItem?.Id > 0);
......@@ -59,7 +59,7 @@ namespace CleanArchitecture.Tests.Integration.Data
_dbContext.Entry(item).State = EntityState.Detached;
// fetch the item and update its title
var newItem = repository.List()
var newItem = repository.List<ToDoItem>()
.FirstOrDefault(i => i.Title == initialTitle);
Assert.NotNull(newItem);
Assert.NotSame(item, newItem);
......@@ -68,7 +68,7 @@ namespace CleanArchitecture.Tests.Integration.Data
// Update the item
repository.Update(newItem);
var updatedItem = repository.List()
var updatedItem = repository.List<ToDoItem>()
.FirstOrDefault(i => i.Title == newTitle);
Assert.NotNull(updatedItem);
......@@ -89,17 +89,17 @@ namespace CleanArchitecture.Tests.Integration.Data
repository.Delete(item);
// verify it's no longer there
Assert.DoesNotContain(repository.List(),
Assert.DoesNotContain(repository.List<ToDoItem>(),
i => i.Title == initialTitle);
}
private EfRepository<ToDoItem> GetRepository()
private EfRepository GetRepository()
{
var options = CreateNewContextOptions();
var mockDispatcher = new Mock<IDomainEventDispatcher>();
_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