Commit b6d7ad62 authored by Mihkel Tiganik's avatar Mihkel Tiganik

UOW implemented (on AnimalTypesController).

RepositoryProvider & GetEntityRepository not implemented
parent 4d1bcd32
......@@ -2,8 +2,10 @@
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using DAL.App.EF.Repositories;
using DAL.App.Interfaces;
using DAL.App.Interfaces.Repositories;
using DAL.EF.Repositories;
using DAL.Interfaces;
using DAL.Interfaces.Helpers;
using DAL.Interfaces.Repositories;
......@@ -14,37 +16,43 @@ namespace DAL.App.EF
public class AppUnitOfWork : IAppUnitOfWork
{
private readonly ApplicationDbContext _dbContext;
private readonly IRepositoryProvider _repositoryProvider;
//private readonly IRepositoryProvider _repositoryProvider;
public AppUnitOfWork(ApplicationDbContext dbContext, IRepositoryProvider repositoryProvider)
public AppUnitOfWork(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
_repositoryProvider = repositoryProvider;
//_repositoryProvider = repositoryProvider;
}
public IAnimalTypeRepository AnimalTypes =>
GetCustomRepository<IAnimalTypeRepository>();
private IAnimalTypeRepository _animalTypeRepository;
public IAnimalTypeRepository AnimalTypes => _animalTypeRepository = _animalTypeRepository ?? new AnimalTypeRepository(_dbContext);
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);
public TRepositoryInterface GetCustomRepository<TRepositoryInterface>() where TRepositoryInterface : class
{
return _repositoryProvider.
ProvideCustomRepository<TRepositoryInterface>();
}
private IRepository<Customer> _customerRepository;
public IRepository<Customer> Customers => _customerRepository = _customerRepository ?? new CustomerRepository(_dbContext);
public IRepository<TEntity> GetEntityRepository<TEntity>() where TEntity : class
{
return _repositoryProvider.
ProvideEntityRepository<TEntity>();
}
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 IRepository<TEntity> GetEntityRepository<TEntity>() where TEntity : class
//{
// return _repositoryProvider.
// ProvideEntityRepository<TEntity>();
//}
public void SaveChanges()
{
......
......@@ -6,6 +6,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
......
......@@ -8,11 +8,11 @@ using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.Internal;
using System;
namespace WebApp.Migrations
namespace DAL.App.EF.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20180224103537_InitialDbCreation")]
partial class InitialDbCreation
[Migration("20180225144309_initialDbCreation")]
partial class initialDbCreation
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
......
......@@ -3,9 +3,9 @@ using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;
namespace WebApp.Migrations
namespace DAL.App.EF.Migrations
{
public partial class InitialDbCreation : Migration
public partial class initialDbCreation : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
......
......@@ -8,7 +8,7 @@ using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.Internal;
using System;
namespace WebApp.Migrations
namespace DAL.App.EF.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
......
......@@ -32,5 +32,10 @@ namespace DAL.App.EF.Repositories
.Include(c => c.PreviousLocation)
.ToListAsync();
}
public static implicit operator CatRepository(AnimalTypeRepository v)
{
throw new NotImplementedException();
}
}
}
......@@ -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;
}
}
......@@ -9,24 +9,26 @@ using Domain;
using DAL.App.EF.Repositories;
using DAL.App.Interfaces.Repositories;
using DAL.App.EF;
using DAL.App.Interfaces;
namespace WebApp.Controllers
{
public class AnimalTypesController : Controller
{
private readonly ApplicationDbContext _context;
private readonly IAnimalTypeRepository _animalTypeRepository;
public AnimalTypesController(ApplicationDbContext context)
private readonly IAppUnitOfWork _uow;
public AnimalTypesController(IAppUnitOfWork uow)
{
_animalTypeRepository = new AnimalTypeRepository(context);
_context = context;
_uow = uow;
}
// GET: AnimalTypes
public async Task<IActionResult> Index()
{
return View(await _animalTypeRepository.AllAsync());
return View(await _uow.AnimalTypes.AllAsync());
}
// GET: AnimalTypes/Details/5
......@@ -42,7 +44,7 @@ namespace WebApp.Controllers
// .SingleOrDefaultAsync(m => m.AnimalTypeId == id);
var animalType = await _animalTypeRepository.FindAsync();
var animalType = await _uow.AnimalTypes.FindAsync();
if (animalType == null)
{
......@@ -68,8 +70,8 @@ namespace WebApp.Controllers
if (ModelState.IsValid)
{
//_context.Add(animalType);
await _animalTypeRepository.AddAsync(animalType);
await _context.SaveChangesAsync(); // SaveChanges still has to be called from context, since we don't have it on our repo
await _uow.AnimalTypes.AddAsync(animalType);
await _uow.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(animalType);
......@@ -83,8 +85,7 @@ namespace WebApp.Controllers
return NotFound();
}
var animalType = await _animalTypeRepository.FindAsync(id);
//_context.AnimalTypes.SingleOrDefaultAsync(m => m.AnimalTypeId == id);
var animalType = await _uow.AnimalTypes.FindAsync(id);
if (animalType == null)
{
return NotFound();
......@@ -108,12 +109,12 @@ namespace WebApp.Controllers
{
try
{
_animalTypeRepository.Update(animalType);
await _context.SaveChangesAsync();
_uow.AnimalTypes.Update(animalType);
await _uow.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!(await _animalTypeRepository.ExistsByPrimaryKeyAsync(animalType.AnimalTypeId)) )
if (!(await _uow.AnimalTypes.ExistsByPrimaryKeyAsync(animalType.AnimalTypeId)) )
{
return NotFound();
}
......@@ -135,13 +136,8 @@ namespace WebApp.Controllers
return NotFound();
}
//var animalType = await _context.AnimalTypes
// .SingleOrDefaultAsync(m => m.AnimalTypeId == id);
//if (animalType == null)
//{
// return NotFound();
//}
var animalType = await _animalTypeRepository.FindAsync(id);
var animalType = await _uow.AnimalTypes.FindAsync(id);
return View(animalType);
}
......@@ -151,15 +147,11 @@ namespace WebApp.Controllers
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
_animalTypeRepository.Remove(id);
await _context.SaveChangesAsync();
_uow.AnimalTypes.Remove(id);
await _uow.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
//private bool AnimalTypeExists(int id)
//{
// // it checks against primary key
// return _context.AnimalTypes.Any(e => e.AnimalTypeId == id);
//}
}
}
......@@ -11,6 +11,7 @@ using Microsoft.Extensions.DependencyInjection;
using WebApp.Models;
using WebApp.Services;
using DAL.App.EF;
using DAL.App.Interfaces;
namespace WebApp
{
......@@ -36,6 +37,9 @@ namespace WebApp
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddScoped<IAppUnitOfWork, AppUnitOfWork>();
//AddScoped<IAppUnitOfWork, AppUnitOfWork>();
services.AddMvc();
}
......
@using System.Collections.Generic
@using System.Linq
@using Domain
@using Microsoft.AspNetCore.Http
@using Microsoft.AspNetCore.Http.Authentication
@model LoginViewModel
......
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