From ec946bf245ddd5a7cfa80d73ce43b6bf4e528749 Mon Sep 17 00:00:00 2001 From: Ben Ramey Date: Fri, 13 Nov 2015 13:00:24 -0600 Subject: [PATCH 1/3] Use IDbContext instead of DbContext directly to make for easier unit testing and mocking --- .../UserManagementDbContext.cs | 3 +- .../AmbientDbContextLocator.cs | 2 +- .../Implementations/DbContextCollection.cs | 12 ++++---- .../Interfaces/IAmbientDbContextLocator.cs | 28 ++++++++++++++----- .../Interfaces/IDbContextCollection.cs | 2 +- .../Interfaces/IDbContextFactory.cs | 2 +- 6 files changed, 32 insertions(+), 17 deletions(-) diff --git a/DemoApplication/DatabaseContext/UserManagementDbContext.cs b/DemoApplication/DatabaseContext/UserManagementDbContext.cs index ad64e86..0b1e540 100644 --- a/DemoApplication/DatabaseContext/UserManagementDbContext.cs +++ b/DemoApplication/DatabaseContext/UserManagementDbContext.cs @@ -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 Users { get; set; } diff --git a/Mehdime.Entity/Implementations/AmbientDbContextLocator.cs b/Mehdime.Entity/Implementations/AmbientDbContextLocator.cs index d182fc9..f16bb22 100644 --- a/Mehdime.Entity/Implementations/AmbientDbContextLocator.cs +++ b/Mehdime.Entity/Implementations/AmbientDbContextLocator.cs @@ -11,7 +11,7 @@ namespace Mehdime.Entity { public class AmbientDbContextLocator : IAmbientDbContextLocator { - public TDbContext Get() where TDbContext : DbContext + public TDbContext Get() where TDbContext : class, IDbContext { var ambientDbContextScope = DbContextScope.GetAmbientScope(); return ambientDbContextScope == null ? null : ambientDbContextScope.DbContexts.Get(); diff --git a/Mehdime.Entity/Implementations/DbContextCollection.cs b/Mehdime.Entity/Implementations/DbContextCollection.cs index 578790e..24b6909 100644 --- a/Mehdime.Entity/Implementations/DbContextCollection.cs +++ b/Mehdime.Entity/Implementations/DbContextCollection.cs @@ -29,30 +29,30 @@ namespace Mehdime.Entity /// public class DbContextCollection : IDbContextCollection { - private Dictionary _initializedDbContexts; - private Dictionary _transactions; + private Dictionary _initializedDbContexts; + private Dictionary _transactions; private IsolationLevel? _isolationLevel; private readonly IDbContextFactory _dbContextFactory; private bool _disposed; private bool _completed; private bool _readOnly; - internal Dictionary InitializedDbContexts { get { return _initializedDbContexts; } } + internal Dictionary InitializedDbContexts { get { return _initializedDbContexts; } } public DbContextCollection(bool readOnly = false, IsolationLevel? isolationLevel = null, IDbContextFactory dbContextFactory = null) { _disposed = false; _completed = false; - _initializedDbContexts = new Dictionary(); - _transactions = new Dictionary(); + _initializedDbContexts = new Dictionary(); + _transactions = new Dictionary(); _readOnly = readOnly; _isolationLevel = isolationLevel; _dbContextFactory = dbContextFactory; } - public TDbContext Get() where TDbContext : DbContext + public TDbContext Get() where TDbContext : class, IDbContext { if (_disposed) throw new ObjectDisposedException("DbContextCollection"); diff --git a/Mehdime.Entity/Interfaces/IAmbientDbContextLocator.cs b/Mehdime.Entity/Interfaces/IAmbientDbContextLocator.cs index b91cc1f..4aafbe5 100644 --- a/Mehdime.Entity/Interfaces/IAmbientDbContextLocator.cs +++ b/Mehdime.Entity/Interfaces/IAmbientDbContextLocator.cs @@ -5,21 +5,35 @@ * This software may be modified and distributed under the terms * of the MIT license. See the LICENSE file for details. */ + +using System; using System.Data.Entity; +using System.Data.Entity.Infrastructure; +using System.Threading; +using System.Threading.Tasks; namespace Mehdime.Entity { /// - /// Convenience methods to retrieve ambient DbContext instances. + /// Convenience methods to retrieve ambient DbContext instances. /// public interface IAmbientDbContextLocator { /// - /// 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. /// - TDbContext Get() where TDbContext : DbContext; + TDbContext Get() where TDbContext : class, IDbContext; + } + + public interface IDbContext : IDisposable + { + DbContextConfiguration Configuration { get; } + Database Database { get; } + + int SaveChanges(); + + Task SaveChangesAsync(CancellationToken cancelToken); } -} +} \ No newline at end of file diff --git a/Mehdime.Entity/Interfaces/IDbContextCollection.cs b/Mehdime.Entity/Interfaces/IDbContextCollection.cs index 5476c6c..adbc231 100644 --- a/Mehdime.Entity/Interfaces/IDbContextCollection.cs +++ b/Mehdime.Entity/Interfaces/IDbContextCollection.cs @@ -18,6 +18,6 @@ public interface IDbContextCollection : IDisposable /// /// Get or create a DbContext instance of the specified type. /// - TDbContext Get() where TDbContext : DbContext; + TDbContext Get() where TDbContext : class, IDbContext; } } \ No newline at end of file diff --git a/Mehdime.Entity/Interfaces/IDbContextFactory.cs b/Mehdime.Entity/Interfaces/IDbContextFactory.cs index f8291cd..5fcb402 100644 --- a/Mehdime.Entity/Interfaces/IDbContextFactory.cs +++ b/Mehdime.Entity/Interfaces/IDbContextFactory.cs @@ -33,6 +33,6 @@ namespace Mehdime.Entity /// public interface IDbContextFactory { - TDbContext CreateDbContext() where TDbContext : DbContext; + TDbContext CreateDbContext() where TDbContext : IDbContext; } } From 10ae1777178ff65c4f1c38a82e01ac31b172687c Mon Sep 17 00:00:00 2001 From: Ben Ramey Date: Fri, 13 Nov 2015 13:01:33 -0600 Subject: [PATCH 2/3] Move IDbContext to its own class --- .../Interfaces/IAmbientDbContextLocator.cs | 14 -------------- Mehdime.Entity/Interfaces/IDbContext.cs | 19 +++++++++++++++++++ Mehdime.Entity/Mehdime.Entity.csproj | 1 + 3 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 Mehdime.Entity/Interfaces/IDbContext.cs diff --git a/Mehdime.Entity/Interfaces/IAmbientDbContextLocator.cs b/Mehdime.Entity/Interfaces/IAmbientDbContextLocator.cs index 4aafbe5..3d52d05 100644 --- a/Mehdime.Entity/Interfaces/IAmbientDbContextLocator.cs +++ b/Mehdime.Entity/Interfaces/IAmbientDbContextLocator.cs @@ -7,10 +7,6 @@ */ using System; -using System.Data.Entity; -using System.Data.Entity.Infrastructure; -using System.Threading; -using System.Threading.Tasks; namespace Mehdime.Entity { @@ -26,14 +22,4 @@ public interface IAmbientDbContextLocator /// TDbContext Get() where TDbContext : class, IDbContext; } - - public interface IDbContext : IDisposable - { - DbContextConfiguration Configuration { get; } - Database Database { get; } - - int SaveChanges(); - - Task SaveChangesAsync(CancellationToken cancelToken); - } } \ No newline at end of file diff --git a/Mehdime.Entity/Interfaces/IDbContext.cs b/Mehdime.Entity/Interfaces/IDbContext.cs new file mode 100644 index 0000000..662710d --- /dev/null +++ b/Mehdime.Entity/Interfaces/IDbContext.cs @@ -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 SaveChangesAsync(CancellationToken cancelToken); + } +} \ No newline at end of file diff --git a/Mehdime.Entity/Mehdime.Entity.csproj b/Mehdime.Entity/Mehdime.Entity.csproj index 6064367..9c52974 100644 --- a/Mehdime.Entity/Mehdime.Entity.csproj +++ b/Mehdime.Entity/Mehdime.Entity.csproj @@ -56,6 +56,7 @@ + From 7721d1fc1bb33062d4948c962b4bff83ffb253b8 Mon Sep 17 00:00:00 2001 From: Ben Ramey Date: Fri, 13 Nov 2015 13:04:50 -0600 Subject: [PATCH 3/3] Add class constraint to be consistent on IDbContextFactory --- Mehdime.Entity/Interfaces/IDbContextFactory.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mehdime.Entity/Interfaces/IDbContextFactory.cs b/Mehdime.Entity/Interfaces/IDbContextFactory.cs index 5fcb402..74c6e8f 100644 --- a/Mehdime.Entity/Interfaces/IDbContextFactory.cs +++ b/Mehdime.Entity/Interfaces/IDbContextFactory.cs @@ -33,6 +33,6 @@ namespace Mehdime.Entity /// public interface IDbContextFactory { - TDbContext CreateDbContext() where TDbContext : IDbContext; + TDbContext CreateDbContext() where TDbContext : class, IDbContext; } }