Commit e44b89c8 authored by Mihkel Tiganik's avatar Mihkel Tiganik

Presentation version

parent b6d7ad62
......@@ -16,43 +16,35 @@ namespace DAL.App.EF
public class AppUnitOfWork : IAppUnitOfWork
{
private readonly ApplicationDbContext _dbContext;
//private readonly IRepositoryProvider _repositoryProvider;
private readonly IRepositoryProvider _repositoryProvider;
public AppUnitOfWork(ApplicationDbContext dbContext)
public AppUnitOfWork(ApplicationDbContext dbContext, IRepositoryProvider repositoryProvider)
{
_dbContext = dbContext;
//_repositoryProvider = repositoryProvider;
_repositoryProvider = repositoryProvider;
}
private IAnimalTypeRepository _animalTypeRepository;
public IAnimalTypeRepository AnimalTypes => _animalTypeRepository = _animalTypeRepository ?? new AnimalTypeRepository(_dbContext);
public IAnimalTypeRepository AnimalTypes => GetCustomRepository<IAnimalTypeRepository>();
public IRepository<Cat> Cats => GetEntityRepository<Cat>();
public IRepository<Customer> Customers => GetEntityRepository<Customer>();
public IRepository<PreviousLocation> PreviousLocations => GetEntityRepository<PreviousLocation>();
//Could be done like this:
//public IRepository<Cat> Cats => new CatRepository(_dbContext);
private IRepository<Cat> _catRepository;
public IRepository<Cat> Cats => _catRepository = _catRepository ?? new CatRepository(_dbContext);
private IRepository<Customer> _customerRepository;
public IRepository<Customer> Customers => _customerRepository = _customerRepository ?? new CustomerRepository(_dbContext);
private IRepository<PreviousLocation> _previousLocationRepository;
public IRepository<PreviousLocation> PreviousLocations => _previousLocationRepository = _previousLocationRepository ?? new PreviousLocationRepository(_dbContext);
//public TRepositoryInterface GetCustomRepository<TRepositoryInterface>() where TRepositoryInterface : class
//{
// return _repositoryProvider.
// ProvideCustomRepository<TRepositoryInterface>();
//}
public TRepositoryInterface GetCustomRepository<TRepositoryInterface>() where TRepositoryInterface : class
{
return _repositoryProvider.
ProvideCustomRepository<TRepositoryInterface>();
}
//public IRepository<TEntity> GetEntityRepository<TEntity>() where TEntity : class
//{
// return _repositoryProvider.
// ProvideEntityRepository<TEntity>();
//}
public IRepository<TEntity> GetEntityRepository<TEntity>() where TEntity : class
{
return _repositoryProvider.
ProvideEntityRepository<TEntity>();
}
public void SaveChanges()
{
......
......@@ -12,7 +12,6 @@ namespace DAL.App.EF.Repositories
{
public class AnimalTypeRepository : EFRepository<AnimalType>, IAnimalTypeRepository
{
// TODO: write constructor
public AnimalTypeRepository(DbContext dbContext) : base(dbContext)
{
}
......
......@@ -11,10 +11,10 @@ namespace DAL.Interfaces
void SaveChanges();
Task SaveChangesAsync();
//IRepository<TEntity> GetEntityRepository<TEntity>()
// where TEntity : class;
//TRepositoryInterface GetCustomRepository<TRepositoryInterface>()
// where TRepositoryInterface : class;
IRepository<TEntity> GetEntityRepository<TEntity>()
where TEntity : class;
TRepositoryInterface GetCustomRepository<TRepositoryInterface>()
where TRepositoryInterface : class;
}
}
......@@ -8,6 +8,7 @@ using Microsoft.EntityFrameworkCore;
using Domain;
using DAL.App.EF.Repositories;
using DAL.App.EF;
using DAL.App.Interfaces;
namespace WebApp.Controllers
{
......@@ -15,6 +16,7 @@ namespace WebApp.Controllers
{
private readonly ApplicationDbContext _context;
private readonly CatRepository _catRepository;
private readonly IAppUnitOfWork _uow;
public CatsController(ApplicationDbContext context)
{
......
......@@ -9,6 +9,7 @@ using Domain;
using DAL.App.Interfaces.Repositories;
using DAL.App.EF.Repositories;
using DAL.App.EF;
using DAL.App.Interfaces;
namespace WebApp.Controllers
{
......@@ -16,17 +17,17 @@ namespace WebApp.Controllers
{
private readonly ApplicationDbContext _context;
private readonly ICustomerRepository _customerRepository;
private readonly IAppUnitOfWork _uow;
public CustomersController(ApplicationDbContext context)
public CustomersController(IAppUnitOfWork uow)
{
_customerRepository = new CustomerRepository(context);
_context = context;
_uow = uow;
}
// GET: Customers
public async Task<IActionResult> Index(string search)
{
var query = await _customerRepository.AllAsync();
var query = await _uow.Customers.AllAsync();
//_context.Customers.AsQueryable();
if (!string.IsNullOrEmpty(search))
{
......@@ -44,7 +45,7 @@ namespace WebApp.Controllers
return NotFound();
}
var customer = await _customerRepository.FindAsync(id);
var customer = await _uow.Customers.FindAsync(id);
if (customer == null)
{
return NotFound();
......@@ -69,8 +70,8 @@ namespace WebApp.Controllers
if (ModelState.IsValid)
{
await _customerRepository.AddAsync(customer);
await _context.SaveChangesAsync();
await _uow.Customers.AddAsync(customer);
await _uow.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(customer);
......@@ -84,7 +85,7 @@ namespace WebApp.Controllers
return NotFound();
}
var customer = await _customerRepository.FindAsync(id);
var customer = await _uow.Customers.FindAsync(id);
if (customer == null)
{
return NotFound();
......@@ -108,8 +109,8 @@ namespace WebApp.Controllers
{
try
{
_customerRepository.Update(customer);
await _context.SaveChangesAsync();
_uow.Customers.Update(customer);
await _uow.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
......@@ -135,7 +136,7 @@ namespace WebApp.Controllers
return NotFound();
}
var customer = await _customerRepository.FindAsync(id);
var customer = await _uow.Customers.FindAsync(id);
if (customer == null)
{
return NotFound();
......
......@@ -12,6 +12,8 @@ using WebApp.Models;
using WebApp.Services;
using DAL.App.EF;
using DAL.App.Interfaces;
using DAL.Interfaces.Helpers;
using DAL.App.EF.Helpers;
namespace WebApp
{
......@@ -37,8 +39,13 @@ namespace WebApp
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
// transient - object is created new on every request
// singleton - object is alive for lifetime of application
// scoped - object is alive for duration of web request
services.AddScoped<IRepositoryProvider, EFRepositoryProvider>();
services.AddScoped<IAppUnitOfWork, AppUnitOfWork>();
//AddScoped<IAppUnitOfWork, AppUnitOfWork>();
//AddScoped<IAppUnitOfWork, AppUnitOfWork>();
services.AddMvc();
}
......
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