Commit 4d1bcd32 authored by Mihkel Tiganik's avatar Mihkel Tiganik

UnitOfWork classes and interfaces added

parent 4c1a6895
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using DAL.App.Interfaces;
using DAL.App.Interfaces.Repositories;
using DAL.Interfaces;
using DAL.Interfaces.Helpers;
using DAL.Interfaces.Repositories;
using Domain;
namespace DAL.App.EF
{
public class AppUnitOfWork : IAppUnitOfWork
{
private readonly ApplicationDbContext _dbContext;
private readonly IRepositoryProvider _repositoryProvider;
public AppUnitOfWork(ApplicationDbContext dbContext, IRepositoryProvider repositoryProvider)
{
_dbContext = dbContext;
_repositoryProvider = repositoryProvider;
}
public IAnimalTypeRepository AnimalTypes =>
GetCustomRepository<IAnimalTypeRepository>();
public IRepository<Cat> Cats =>
GetEntityRepository<Cat>();
public IRepository<Customer> Customers =>
GetEntityRepository<Customer>();
public IRepository<PreviousLocation> PreviousLocations =>
GetEntityRepository<PreviousLocation>();
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()
{
_dbContext.SaveChanges();
}
public async Task SaveChangesAsync()
{
await _dbContext.SaveChangesAsync();
}
}
}
......@@ -7,7 +7,7 @@ using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using WebApp.Models;
namespace WebApp.Data
namespace DAL.App.EF
{
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
......
......@@ -4,6 +4,10 @@
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="2.0.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DAL.App.Interfaces\DAL.App.Interfaces.csproj" />
<ProjectReference Include="..\DAL.EF\DAL.EF.csproj" />
......
using System;
using System.Collections.Generic;
using System.Text;
using DAL.App.EF.Repositories;
using DAL.App.Interfaces.Repositories;
using DAL.EF.Repositories;
using DAL.Interfaces.Helpers;
using DAL.Interfaces.Repositories;
namespace DAL.App.EF.Helpers
{
public class EFRepositoryProvider : IRepositoryProvider
{
private readonly Dictionary<Type, object> _repositoryCache = new Dictionary<Type, object>();
private readonly ApplicationDbContext _context;
public EFRepositoryProvider(ApplicationDbContext context)
{
_context = context;
}
public TRepositoryInterface ProvideCustomRepository<TRepositoryInterface>() where TRepositoryInterface : class
{
_repositoryCache.TryGetValue(typeof(TRepositoryInterface), out var repo);
if(repo == null)
{
switch (typeof(TRepositoryInterface).Name)
{
case nameof(IAnimalTypeRepository):
repo = new AnimalTypeRepository(_context);
break;
case nameof(ICatRepository):
repo = new CatRepository(_context);
break;
case nameof(ICustomerRepository):
repo = new CustomerRepository(_context);
break;
case nameof(IPreviousLocationRepository):
repo = new PreviousLocationRepository(_context);
break;
default:
throw new NotImplementedException(
$"No repository defined for type: {typeof(TRepositoryInterface).Name}");
}
_repositoryCache.Add(typeof(TRepositoryInterface), repo);
}
return (TRepositoryInterface)repo;
}
public IRepository<TEntity> ProvideEntityRepository<TEntity>() where TEntity : class
{
_repositoryCache.TryGetValue(typeof(IRepository<TEntity>), out var repo);
if(repo == null)
{
repo = new EFRepository<TEntity>(_context);
_repositoryCache.Add(typeof(IRepository<TEntity>), repo);
}
return (IRepository<TEntity>)repo;
}
}
}
......@@ -26,8 +26,7 @@ namespace DAL.App.EF.Repositories
{
return await RepositoryDbSet.AnyAsync(e => e.AnimalTypeId == keyValue);
}
// Some random method which can be deleted later
public void foo() { }
}
}
......@@ -16,15 +16,6 @@ namespace DAL.App.EF.Repositories
{
}
public bool ExistsByPrimaryKey(int keyValue)
{
return RepositoryDbSet.Any(e => e.CustomerId == keyValue);
}
public async Task<bool> ExistsByPrimaryKeyAsync(int keyValue)
{
return await RepositoryDbSet.AnyAsync(e => e.CustomerId == keyValue);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using DAL.App.Interfaces.Repositories;
using DAL.Interfaces;
using DAL.Interfaces.Repositories;
using Domain;
namespace Domain
namespace DAL.App.Interfaces
{
class IAppUnitOfWork
public interface IAppUnitOfWork : IUnitOfWork
{
IAnimalTypeRepository AnimalTypes { get; }
IRepository<Cat> Cats { get; }
IRepository<Customer> Customers { get; }
IRepository<PreviousLocation> PreviousLocations { get; }
}
}
......@@ -9,7 +9,6 @@ namespace DAL.App.Interfaces.Repositories
{
public interface ICustomerRepository : IRepository<Customer>
{
bool ExistsByPrimaryKey(int keyValue);
Task<bool> ExistsByPrimaryKeyAsync(int keyValue);
}
}
using System;
using System.Collections.Generic;
using System.Text;
using DAL.Interfaces.Repositories;
using System.Threading.Tasks;
namespace DAL.Interfaces.Helpers
{
public interface IRepositoryProvider
{
IRepository<TEntity> ProvideEntityRepository<TEntity>()
where TEntity : class;
TRepositoryInterface ProvideCustomRepository<TRepositoryInterface>()
where TRepositoryInterface : class;
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using DAL.Interfaces.Repositories;
namespace DAL.Interfaces
{
public interface IUnitOfWork
{
void SaveChanges();
Task SaveChangesAsync();
IRepository<TEntity> GetEntityRepository<TEntity>()
where TEntity : class;
TRepositoryInterface GetCustomRepository<TRepositoryInterface>()
where TRepositoryInterface : class;
}
}
......@@ -16,8 +16,4 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Folder Include="Helpers\" />
</ItemGroup>
</Project>
......@@ -6,9 +6,9 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Domain;
using WebApp.Data;
using DAL.App.EF.Repositories;
using DAL.App.Interfaces.Repositories;
using DAL.App.EF;
namespace WebApp.Controllers
{
......
......@@ -6,8 +6,8 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Domain;
using WebApp.Data;
using DAL.App.EF.Repositories;
using DAL.App.EF;
namespace WebApp.Controllers
{
......
......@@ -6,9 +6,9 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Domain;
using WebApp.Data;
using DAL.App.Interfaces.Repositories;
using DAL.App.EF.Repositories;
using DAL.App.EF;
namespace WebApp.Controllers
{
......@@ -113,7 +113,7 @@ namespace WebApp.Controllers
}
catch (DbUpdateConcurrencyException)
{
if (! await _customerRepository.ExistsByPrimaryKeyAsync(customer.CustomerId))
if (!CustomerExists(customer.CustomerId))
{
return NotFound();
}
......@@ -154,6 +154,10 @@ namespace WebApp.Controllers
return RedirectToAction(nameof(Index));
}
private bool CustomerExists(int id)
{
return _context.Customers.Any(e => e.CustomerId == id);
}
}
}
......@@ -6,9 +6,9 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Domain;
using WebApp.Data;
using DAL.App.Interfaces.Repositories;
using DAL.App.EF.Repositories;
using DAL.App.EF;
namespace WebApp.Controllers
{
......
// <auto-generated />
using DAL.App.EF;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
......@@ -6,7 +7,6 @@ using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.Internal;
using System;
using WebApp.Data;
namespace WebApp.Migrations
{
......
// <auto-generated />
using DAL.App.EF;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
......@@ -6,7 +7,6 @@ using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.Internal;
using System;
using WebApp.Data;
namespace WebApp.Migrations
{
......
......@@ -8,9 +8,9 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using WebApp.Data;
using WebApp.Models;
using WebApp.Services;
using DAL.App.EF;
namespace WebApp
{
......
......@@ -23,4 +23,8 @@
<ProjectReference Include="..\Domain\Domain.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Data\" />
</ItemGroup>
</Project>
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