Skip to content

Commit

Permalink
refactor: implemented unit of work
Browse files Browse the repository at this point in the history
  • Loading branch information
hiresm committed Jan 2, 2024
1 parent 236a63f commit b5eafb7
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 94 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Security.Claims;
using AutomatedTestingApp.Areas.Identity.Repositories;
using AutomatedTestingApp.Areas.Identity.Models;
using AutomatedTestingApp.Repositories;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -8,11 +9,12 @@ namespace AutomatedTestingApp.Areas.Identity.Controllers;

public class AccountController : Controller
{
private readonly IUserRepository _userRepository;
private readonly IUnitOfWork _unitOfWork;
private IRepository<IdentityUser> UserRepository => _unitOfWork.GetRepository<IdentityUser>();

public AccountController(IUserRepository userRepository)
public AccountController(IUnitOfWork unitOfWork)
{
_userRepository = userRepository;
_unitOfWork = unitOfWork;
}

public IActionResult Login(string? returnUrl = null)
Expand All @@ -24,7 +26,8 @@ public IActionResult Login(string? returnUrl = null)
[HttpPost]
public async Task<IActionResult> Login(string userName, string password, string? returnUrl = null)
{
var user = await _userRepository.GetUserByUsernameAsync(userName);
var user = UserRepository.Get(x => x.Username == userName).FirstOrDefault();

if (user == null || user.Password != password)
{
ViewData["ReturnUrl"] = returnUrl;
Expand Down

This file was deleted.

This file was deleted.

4 changes: 2 additions & 2 deletions AutomatedTestingApp/AutomatedTestingApp/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using AutomatedTestingApp.Areas.Identity.Repositories;
using AutomatedTestingApp.Helpers;
using AutomatedTestingApp.Repositories;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.EntityFrameworkCore;

Expand All @@ -12,7 +12,7 @@
throw new InvalidOperationException("Connection string 'DefaultConnection' not found.");
builder.Services.AddDbContext<DataContext>(options =>
options.UseSqlite($"DataSource={connectionString};Cache=Shared"));
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IUnitOfWork, UnitOfWork>();
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(x =>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Linq.Expressions;

namespace AutomatedTestingApp.Repositories;

public interface IRepository<TEntity>
{
IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>>? filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null);

TEntity GetById(object id);

void Insert(TEntity entity);

void Update(TEntity entity);

void Delete(object id);

void Delete(TEntity entityToDelete);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace AutomatedTestingApp.Repositories;

public interface IUnitOfWork
{
void Commit();
void Rollback();
IRepository<TEntity> GetRepository<TEntity>() where TEntity : class;
}
62 changes: 62 additions & 0 deletions AutomatedTestingApp/AutomatedTestingApp/Repositories/Repository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Linq.Expressions;
using AutomatedTestingApp.Helpers;
using Microsoft.EntityFrameworkCore;

namespace AutomatedTestingApp.Repositories;

public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
{
private readonly DbSet<TEntity> _dbSet;

public Repository(DataContext context)
{
_dbSet = context.Set<TEntity>();
}

public IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>>? filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>>? orderBy = null)
{
IQueryable<TEntity> query = _dbSet;

if (filter != null)
query = query.Where(filter);

if (orderBy != null)
return orderBy(query).ToList();

return _dbSet.ToList();
}

public TEntity GetById(object id)
{
return _dbSet.Find(id);

Check warning on line 32 in AutomatedTestingApp/AutomatedTestingApp/Repositories/Repository.cs

View workflow job for this annotation

GitHub Actions / Build

Possible null reference return.

Check warning on line 32 in AutomatedTestingApp/AutomatedTestingApp/Repositories/Repository.cs

View workflow job for this annotation

GitHub Actions / Build

Possible null reference return.

Check warning on line 32 in AutomatedTestingApp/AutomatedTestingApp/Repositories/Repository.cs

View workflow job for this annotation

GitHub Actions / Build

Possible null reference return.

Check warning on line 32 in AutomatedTestingApp/AutomatedTestingApp/Repositories/Repository.cs

View workflow job for this annotation

GitHub Actions / Build

Possible null reference return.
}

public void Insert(TEntity entity)
{
_dbSet.Add(entity);
}

public void Update(TEntity entity)
{
_dbSet.Attach(entity);
}

public void Delete(object id)
{
TEntity entityToDelete = _dbSet.Find(id);

Check warning on line 47 in AutomatedTestingApp/AutomatedTestingApp/Repositories/Repository.cs

View workflow job for this annotation

GitHub Actions / Build

Converting null literal or possible null value to non-nullable type.

Check warning on line 47 in AutomatedTestingApp/AutomatedTestingApp/Repositories/Repository.cs

View workflow job for this annotation

GitHub Actions / Build

Converting null literal or possible null value to non-nullable type.

Check warning on line 47 in AutomatedTestingApp/AutomatedTestingApp/Repositories/Repository.cs

View workflow job for this annotation

GitHub Actions / Build

Converting null literal or possible null value to non-nullable type.

Check warning on line 47 in AutomatedTestingApp/AutomatedTestingApp/Repositories/Repository.cs

View workflow job for this annotation

GitHub Actions / Build

Converting null literal or possible null value to non-nullable type.

if (entityToDelete == null)
return;

Delete(entityToDelete);
}

public void Delete(TEntity entityToDelete)
{
if (_dbSet.Local.Contains(entityToDelete))
_dbSet.Remove(entityToDelete);
else
_dbSet.Attach(entityToDelete);
}
}
39 changes: 39 additions & 0 deletions AutomatedTestingApp/AutomatedTestingApp/Repositories/UnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using AutomatedTestingApp.Helpers;

namespace AutomatedTestingApp.Repositories;

public class UnitOfWork : IUnitOfWork, IDisposable
{
private readonly DataContext _context;
private Dictionary<Type, Object> _repositories;

public UnitOfWork(DataContext context)
{
_context = context;
_repositories = new Dictionary<Type, object>();
}

public void Commit()
{
_context.SaveChanges();
}

public void Rollback()
{
// TODO rollback changes here
}

public IRepository<TEntity> GetRepository<TEntity>() where TEntity : class
{
if (_repositories.Keys.Contains(typeof(TEntity)))
return (IRepository<TEntity>)_repositories[typeof(TEntity)];
var repository = new Repository<TEntity>(_context);
_repositories.Add(typeof(TEntity), repository);
return repository;
}

public void Dispose()
{
_context.Dispose();
}
}

0 comments on commit b5eafb7

Please sign in to comment.