Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constrain on IDbContext instead of DbContext for easier unit testing and mocking #30

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DemoApplication/DatabaseContext/UserManagementDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Data.Entity;
using System.Reflection;
using Mehdime.Entity;
using Numero3.EntityFramework.Demo.DomainModel;

namespace Numero3.EntityFramework.Demo.DatabaseContext
{
public class UserManagementDbContext : DbContext
public class UserManagementDbContext : DbContext, IDbContext
{
// Map our 'User' model by convention
public DbSet<User> Users { get; set; }
Expand Down
2 changes: 1 addition & 1 deletion Mehdime.Entity/Implementations/AmbientDbContextLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Mehdime.Entity
{
public class AmbientDbContextLocator : IAmbientDbContextLocator
{
public TDbContext Get<TDbContext>() where TDbContext : DbContext
public TDbContext Get<TDbContext>() where TDbContext : class, IDbContext
{
var ambientDbContextScope = DbContextScope.GetAmbientScope();
return ambientDbContextScope == null ? null : ambientDbContextScope.DbContexts.Get<TDbContext>();
Expand Down
12 changes: 6 additions & 6 deletions Mehdime.Entity/Implementations/DbContextCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,30 @@ namespace Mehdime.Entity
/// </summary>
public class DbContextCollection : IDbContextCollection
{
private Dictionary<Type, DbContext> _initializedDbContexts;
private Dictionary<DbContext, DbContextTransaction> _transactions;
private Dictionary<Type, IDbContext> _initializedDbContexts;
private Dictionary<IDbContext, DbContextTransaction> _transactions;
private IsolationLevel? _isolationLevel;
private readonly IDbContextFactory _dbContextFactory;
private bool _disposed;
private bool _completed;
private bool _readOnly;

internal Dictionary<Type, DbContext> InitializedDbContexts { get { return _initializedDbContexts; } }
internal Dictionary<Type, IDbContext> InitializedDbContexts { get { return _initializedDbContexts; } }

public DbContextCollection(bool readOnly = false, IsolationLevel? isolationLevel = null, IDbContextFactory dbContextFactory = null)
{
_disposed = false;
_completed = false;

_initializedDbContexts = new Dictionary<Type, DbContext>();
_transactions = new Dictionary<DbContext, DbContextTransaction>();
_initializedDbContexts = new Dictionary<Type, IDbContext>();
_transactions = new Dictionary<IDbContext, DbContextTransaction>();

_readOnly = readOnly;
_isolationLevel = isolationLevel;
_dbContextFactory = dbContextFactory;
}

public TDbContext Get<TDbContext>() where TDbContext : DbContext
public TDbContext Get<TDbContext>() where TDbContext : class, IDbContext
{
if (_disposed)
throw new ObjectDisposedException("DbContextCollection");
Expand Down
16 changes: 8 additions & 8 deletions Mehdime.Entity/Interfaces/IAmbientDbContextLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
using System.Data.Entity;

using System;

namespace Mehdime.Entity
{
/// <summary>
/// Convenience methods to retrieve ambient DbContext instances.
/// Convenience methods to retrieve ambient DbContext instances.
/// </summary>
public interface IAmbientDbContextLocator
{
/// <summary>
/// If called within the scope of a DbContextScope, gets or creates
/// the ambient DbContext instance for the provided DbContext type.
///
/// Otherwise returns null.
/// If called within the scope of a DbContextScope, gets or creates
/// the ambient DbContext instance for the provided DbContext type.
/// Otherwise returns null.
/// </summary>
TDbContext Get<TDbContext>() where TDbContext : DbContext;
TDbContext Get<TDbContext>() where TDbContext : class, IDbContext;
}
}
}
19 changes: 19 additions & 0 deletions Mehdime.Entity/Interfaces/IDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Mehdime.Entity
{
public interface IDbContext : IDisposable
{
DbContextConfiguration Configuration { get; }
Database Database { get; }

int SaveChanges();

Task<int> SaveChangesAsync(CancellationToken cancelToken);
}
}
2 changes: 1 addition & 1 deletion Mehdime.Entity/Interfaces/IDbContextCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ public interface IDbContextCollection : IDisposable
/// <summary>
/// Get or create a DbContext instance of the specified type.
/// </summary>
TDbContext Get<TDbContext>() where TDbContext : DbContext;
TDbContext Get<TDbContext>() where TDbContext : class, IDbContext;
}
}
2 changes: 1 addition & 1 deletion Mehdime.Entity/Interfaces/IDbContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ namespace Mehdime.Entity
/// </remarks>
public interface IDbContextFactory
{
TDbContext CreateDbContext<TDbContext>() where TDbContext : DbContext;
TDbContext CreateDbContext<TDbContext>() where TDbContext : class, IDbContext;
}
}
1 change: 1 addition & 0 deletions Mehdime.Entity/Mehdime.Entity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="Implementations\DbContextScopeFactory.cs" />
<Compile Include="Enums\DbContextScopeOption.cs" />
<Compile Include="Interfaces\IAmbientDbContextLocator.cs" />
<Compile Include="Interfaces\IDbContext.cs" />
<Compile Include="Interfaces\IDbContextCollection.cs" />
<Compile Include="Interfaces\IDbContextFactory.cs" />
<Compile Include="Interfaces\IDbContextReadOnlyScope.cs" />
Expand Down