Skip to content

Commit

Permalink
WIP - Added Delete Repository Functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
garywoodfine committed Jul 7, 2020
1 parent 1405453 commit 013d677
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 24 deletions.
26 changes: 5 additions & 21 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
## What type of PR is this? (check all applicable)
- [] Refactor
- [] Feature
- [] Bug Fix
- [] Optimization
- [] Documentation Update
- [ ] Refactor
- [ ] Feature
- [ ] Bug Fix
- [ ] Optimization
- [ ] Documentation Update

## Overview:
[A summary of what you did in no more than one paragraph]

## Work carried out:

[A list of work you have done, use markdown checklist format,
Don't bother to include list of files changed this will automatically be included]

example.
- [x] Updated packages in `package.json` to update references

## Practical usage example
[Sometimes the use of the code isn't immediately clear, if so, add a usable example here]

## Screenshot
[If the PR benefits from it then paste a screenshot of the update here.]

## Developer Notes:
[Sometimes, extra notes are needed to add clarity to a PR, add them here]
22 changes: 19 additions & 3 deletions src/DeleteRepository.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;

namespace Threenine.Data
{
public class DeleteRepository<T> : IDeleteRepository<T> where T : class
{
private readonly DbContext _dbContext;
private readonly DbSet<T> _dbSet;

public DeleteRepository(DbContext context)
{
_dbContext = context ?? throw new ArgumentException(nameof(context));
_dbSet = _dbContext.Set<T>();
var dbContext = context ?? throw new ArgumentException(nameof(context));
_dbSet = dbContext.Set<T>();
}

public void Delete(T entity)
{
_dbSet.Remove(entity);
}

public void Delete(params T[] entities)
{
_dbSet.RemoveRange(entities);
}


public void Delete(IEnumerable<T> entities)
{
_dbSet.RemoveRange(entities);
}
}
}
7 changes: 7 additions & 0 deletions src/IDeleteRepository.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
using System.Collections.Generic;

namespace Threenine.Data
{
public interface IDeleteRepository<T> where T : class
{
void Delete(T entity);

void Delete(params T[] entities);

void Delete(IEnumerable<T> entities);
}
}
1 change: 1 addition & 0 deletions src/IUnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public interface IUnitOfWork : IDisposable
IRepositoryAsync<TEntity> GetRepositoryAsync<TEntity>() where TEntity : class;
IRepositoryReadOnly<TEntity> GetReadOnlyRepository<TEntity>() where TEntity : class;
IRepositoryReadOnlyAsync<TEntity> GetReadOnlyRepositoryAsync<TEntity>() where TEntity : class;
IDeleteRepository<TEntity> DeleteRepository<TEntity>() where TEntity : class;


int Commit(bool autoHistory = false);
Expand Down
6 changes: 6 additions & 0 deletions src/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ public IRepositoryReadOnlyAsync<TEntity> GetReadOnlyRepositoryAsync<TEntity>() w
new RepositoryReadOnlyAsync<TEntity>(Context));
}

public IDeleteRepository<TEntity> DeleteRepository<TEntity>() where TEntity : class
{
return (IDeleteRepository<TEntity>) GetOrAddRepository(typeof(TEntity),
new DeleteRepository<TEntity>(Context));
}

public TContext Context { get; }

public int Commit(bool autoHistory = false)
Expand Down
40 changes: 40 additions & 0 deletions tests/Threenine.Data.Tests/DeleteRepositoryTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using TestDatabase;
using Threenine.Data.Tests.TestFixtures;
using Xunit;

namespace Threenine.Data.Tests
{
[Collection(GlobalTestStrings.ProductCollectionName)]
public class DeleteRepositoryTests : IDisposable
{
private readonly SqlLiteWith20ProductsTestFixture _fixture;
public DeleteRepositoryTests(SqlLiteWith20ProductsTestFixture fixture)
{
_fixture = fixture;
}

[Fact]
public void ShouldDeleteProduct()
{
using var uow = new UnitOfWork<TestDbContext>(_fixture.Context);

var getRepo = uow.GetRepository<TestProduct>();
var delRepo = uow.DeleteRepository<TestProduct>();

uow.Commit();

var prod = getRepo.SingleOrDefault(x => x.Id == 1);
delRepo.Delete(prod);
uow.Commit();

prod = getRepo.SingleOrDefault(x => x.Id == 1);
Assert.Null(prod);
}

public void Dispose()
{
_fixture?.Dispose();
}
}
}

0 comments on commit 013d677

Please sign in to comment.