Skip to content

Commit

Permalink
Removing IUnitOfWork.Repository<TEntity> for zero dependency/concern …
Browse files Browse the repository at this point in the history
…on IServiceProvider.
  • Loading branch information
lelong37 committed Feb 14, 2018
1 parent f61950c commit 92ffb3d
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 55 deletions.
2 changes: 1 addition & 1 deletion URF.Core.Abstractions/IUnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Urf.Core.Abstractions
{
public interface IUnitOfWork
{
IRepository<TEntity> Repository<TEntity>() where TEntity : class;
//IRepository<TEntity> Repository<TEntity>() where TEntity : class;
Task<int> SaveChangesAsync(CancellationToken cancellationToken = default);
Task<int> ExecuteSqlCommandAsync(string sql, IEnumerable<object> parameters, CancellationToken cancellationToken = default );
}
Expand Down
4 changes: 2 additions & 2 deletions URF.Core.EF.Tests/ServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ namespace URF.Core.EF.Tests
[Collection(nameof(NorthwindDbContext))]
public class ServiceTest
{
private List<Product> _products;
private List<Category> _categories;
private readonly List<Order> _orders;
private readonly List<Product> _products;
private readonly List<Category> _categories;
private readonly List<Customer> _customers;
private readonly List<OrderDetail> _ordersDetails;

Expand Down
11 changes: 6 additions & 5 deletions URF.Core.EF.Tests/TrackableRepositoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using TrackableEntities.Common.Core;
using URF.Core.Abstractions.Trackable;
using URF.Core.EF.Tests.Contexts;
using URF.Core.EF.Tests.Models;
using URF.Core.EF.Trackable;
Expand Down Expand Up @@ -63,7 +64,7 @@ public TrackableRepositoryTest(NorthwindDbContextFixture fixture)
public void Insert_Should_Set_Entity_State_to_Added()
{
// Arrange
var productsRepo = _unitOfWork.TrackableRepository<Product>();
ITrackableRepository<Product> productsRepo = new TrackableRepository<Product>(_fixture.Context);
var product = new Product {ProductId = 4, ProductName = "Product 4", UnitPrice = 40, CategoryId = 1};

// Act
Expand All @@ -77,7 +78,7 @@ public void Insert_Should_Set_Entity_State_to_Added()
public void Update_Should_Set_Entity_State_to_Modified()
{
// Arrange
var productsRepo = _unitOfWork.TrackableRepository<Product>();
ITrackableRepository<Product> productsRepo = new TrackableRepository<Product>(_fixture.Context);
var product = new Product { ProductId = 4, ProductName = "Product 4", UnitPrice = 40, CategoryId = 1 };

// Act
Expand All @@ -91,7 +92,7 @@ public void Update_Should_Set_Entity_State_to_Modified()
public void Delete_Should_Set_Entity_State_to_Deleted()
{
// Arrange
var productsRepo = _unitOfWork.TrackableRepository<Product>();
ITrackableRepository<Product> productsRepo = new TrackableRepository<Product>(_fixture.Context);
var product = new Product { ProductId = 4, ProductName = "Product 4", UnitPrice = 40, CategoryId = 1 };

// Act
Expand All @@ -105,7 +106,7 @@ public void Delete_Should_Set_Entity_State_to_Deleted()
public async Task DeleteAsync_Should_Set_Entity_State_to_Deleted()
{
// Arrange
var productsRepo = _unitOfWork.TrackableRepository<Product>();
ITrackableRepository<Product> productsRepo = new TrackableRepository<Product>(_fixture.Context);
var product = await productsRepo.FindAsync(1);

// Act
Expand All @@ -119,7 +120,7 @@ public async Task DeleteAsync_Should_Set_Entity_State_to_Deleted()
public async Task ApplyChanges_Should_Set_Graph_States()
{
// Arrange
var ordersRepo = _unitOfWork.TrackableRepository<Order>();
ITrackableRepository<Order> ordersRepo = new TrackableRepository<Order>(_fixture.Context);
var order = await ordersRepo.FindAsync(1);
ordersRepo.DetachEntities(order);

Expand Down
28 changes: 1 addition & 27 deletions URF.Core.EF.Tests/UnitOfWorkTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Urf.Core.Abstractions;
using URF.Core.EF.Tests.Contexts;
using URF.Core.EF.Tests.Models;
using Xunit;
Expand All @@ -17,33 +18,6 @@ public UnitOfWorkTest(NorthwindDbContextFixture fixture)
_fixture.Initialize();
}

[Fact]
public void Repository_Getter_Should_Create_Repository()
{
// Arrange
var unitOfWork = new UnitOfWork(_fixture.Context);

// Act
var repository = unitOfWork.Repository<Product>();

// Assert
Assert.NotNull(repository);
}

[Fact]
public void Repository_Getter_Should_Return_Created_Repository()
{
// Arrange
var unitOfWork = new UnitOfWork(_fixture.Context);
var repository = unitOfWork.Repository<Product>();

// Act
var repository1 = unitOfWork.Repository<Product>();

// Assert
Assert.Same(repository, repository1);
}

[Fact]
public async Task SaveChangesAsync_Should_Save_Changes()
{
Expand Down
9 changes: 0 additions & 9 deletions URF.Core.EF.Trackable/TrackableUnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,5 @@ public class TrackableUnitOfWork : UnitOfWork
public TrackableUnitOfWork(DbContext context) : base(context)
{
}

public virtual ITrackableRepository<TEntity> TrackableRepository<TEntity>() where TEntity : class, ITrackable
{
if (Repositories.TryGetValue(typeof(ITrackableRepository<TEntity>), out var repository) && repository is ITrackableRepository<TEntity>)
return (ITrackableRepository<TEntity>)repository;
var repository1 = (ITrackableRepository<TEntity>)Activator.CreateInstance(typeof(TrackableRepository<TEntity>), Context);
Repositories.TryAdd(typeof(ITrackableRepository<TEntity>), repository1);
return repository1;
}
}
}
11 changes: 0 additions & 11 deletions URF.Core.EF/UnitOfWork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,10 @@ namespace URF.Core.EF
public class UnitOfWork : IUnitOfWork
{
protected DbContext Context { get; }
protected ConcurrentDictionary<Type, object> Repositories { get; }

public UnitOfWork(DbContext context)
{
Context = context;
Repositories = new ConcurrentDictionary<Type, object>();
}

public virtual IRepository<TEntity> Repository<TEntity>() where TEntity : class
{
if (Repositories.TryGetValue(typeof(IRepository<TEntity>), out var repository) && repository is IRepository<TEntity>)
return (IRepository<TEntity>) repository;
var repository1 = (IRepository<TEntity>)Activator.CreateInstance(typeof(Repository<TEntity>), Context);
Repositories.TryAdd(typeof(IRepository<TEntity>), repository1);
return repository1;
}

public virtual async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
Expand Down

0 comments on commit 92ffb3d

Please sign in to comment.