-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP - Added Delete Repository Functionality
- Loading branch information
1 parent
1405453
commit 013d677
Showing
6 changed files
with
78 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |