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

Added support to swap to another database #68

Open
wants to merge 2 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ publish/

# NuGet Packages
*.nupkg
*.nuspec
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
Expand Down
12 changes: 6 additions & 6 deletions DemoApplication/App.config
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
<parameter value="v11.0"/>
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
</configuration>
5 changes: 4 additions & 1 deletion DemoApplication/DatabaseContext/UserManagementDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ public class UserManagementDbContext : DbContext
// Map our 'User' model by convention
public DbSet<User> Users { get; set; }

public UserManagementDbContext() : base("Server=localhost;Database=DbContextScopeDemo;Trusted_Connection=true;")
public UserManagementDbContext() : base("Server=(local);Database=DbContextScopeDemo;Trusted_Connection=true;")
{}

public UserManagementDbContext(string nameOrConnectionString): base(nameOrConnectionString)
{ }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
Expand Down
21 changes: 21 additions & 0 deletions DemoApplication/DbContextFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Mehdime.Entity;
using Numero3.EntityFramework.Demo.DatabaseContext;
using System.Data.Entity;

namespace Numero3.EntityFramework.Demo
{
public sealed class DbContextFactory : IDbContextFactory
{
private readonly string nameOrConnectionStringDefault;

public DbContextFactory(string nameOrConnectionStringDefault)
{
this.nameOrConnectionStringDefault = nameOrConnectionStringDefault;
}

public TDbContext CreateDbContext<TDbContext>(string nameOrConnectionString) where TDbContext : DbContext
{
return new UserManagementDbContext(nameOrConnectionString ?? nameOrConnectionStringDefault) as TDbContext;
}
}
}
6 changes: 4 additions & 2 deletions DemoApplication/Demo Application.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand All @@ -9,10 +9,11 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Numero3.EntityFramework.Demo</RootNamespace>
<AssemblyName>Numero3.EntityFramework.Demo</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand Down Expand Up @@ -57,6 +58,7 @@
<Compile Include="BusinessLogicServices\UserEmailService.cs" />
<Compile Include="CommandModel\UserCreationSpec.cs" />
<Compile Include="DatabaseContext\UserFluentMap.cs" />
<Compile Include="DbContextFactory.cs" />
<Compile Include="DomainModel\User.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
3 changes: 2 additions & 1 deletion DemoApplication/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Program
static void Main(string[] args)
{
//-- Poor-man DI - build our dependencies by hand for this demo
var dbContextScopeFactory = new DbContextScopeFactory();
var contextFactory = new DbContextFactory("Server=(local);Database=DbContextScopeDemo;Trusted_Connection=true;");
var dbContextScopeFactory = new DbContextScopeFactory(contextFactory);
var ambientDbContextLocator = new AmbientDbContextLocator();
var userRepository = new UserRepository(ambientDbContextLocator);

Expand Down
10 changes: 5 additions & 5 deletions Mehdime.Entity/App.config
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
</configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/></startup></configuration>
24 changes: 20 additions & 4 deletions Mehdime.Entity/Implementations/DbContextCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public class DbContextCollection : IDbContextCollection
private bool _disposed;
private bool _completed;
private bool _readOnly;
private string _nameOrConnectionString;

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

public DbContextCollection(bool readOnly = false, IsolationLevel? isolationLevel = null, IDbContextFactory dbContextFactory = null)
public DbContextCollection(bool readOnly = false, IsolationLevel? isolationLevel = null, IDbContextFactory dbContextFactory = null,
string nameOrConnectionString = null)
{
_disposed = false;
_completed = false;
Expand All @@ -50,6 +52,7 @@ public DbContextCollection(bool readOnly = false, IsolationLevel? isolationLevel
_readOnly = readOnly;
_isolationLevel = isolationLevel;
_dbContextFactory = dbContextFactory;
_nameOrConnectionString = nameOrConnectionString;
}

public TDbContext Get<TDbContext>() where TDbContext : DbContext
Expand All @@ -63,9 +66,7 @@ public TDbContext Get<TDbContext>() where TDbContext : DbContext
{
// First time we've been asked for this particular DbContext type.
// Create one, cache it and start its database transaction if needed.
var dbContext = _dbContextFactory != null
? _dbContextFactory.CreateDbContext<TDbContext>()
: Activator.CreateInstance<TDbContext>();
var dbContext = Create<TDbContext>();

_initializedDbContexts.Add(requestedType, dbContext);

Expand Down Expand Up @@ -281,5 +282,20 @@ private static TValue GetValueOrDefault<TKey, TValue>(IDictionary<TKey, TValue>
TValue value;
return dictionary.TryGetValue(key, out value) ? value : default(TValue);
}

private TDbContext Create<TDbContext>() where TDbContext : DbContext
{
TDbContext dbContext;

if (_dbContextFactory != null)
{
dbContext = _dbContextFactory.CreateDbContext<TDbContext>(_nameOrConnectionString);
} else
{
dbContext = Activator.CreateInstance<TDbContext>();
}

return dbContext;
}
}
}
15 changes: 8 additions & 7 deletions Mehdime.Entity/Implementations/DbContextReadOnlyScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ public class DbContextReadOnlyScope : IDbContextReadOnlyScope

public IDbContextCollection DbContexts { get { return _internalScope.DbContexts; } }

public DbContextReadOnlyScope(IDbContextFactory dbContextFactory = null)
: this(joiningOption: DbContextScopeOption.JoinExisting, isolationLevel: null, dbContextFactory: dbContextFactory)
{}
public DbContextReadOnlyScope(IDbContextFactory dbContextFactory = null, string nameOrConnectionString = null)
: this(joiningOption: DbContextScopeOption.JoinExisting, isolationLevel: null, dbContextFactory: dbContextFactory, nameOrConnectionString)
{ }

public DbContextReadOnlyScope(IsolationLevel isolationLevel, IDbContextFactory dbContextFactory = null)
: this(joiningOption: DbContextScopeOption.ForceCreateNew, isolationLevel: isolationLevel, dbContextFactory: dbContextFactory)
public DbContextReadOnlyScope(IsolationLevel isolationLevel, IDbContextFactory dbContextFactory = null, string nameOrConnectionString = null)
: this(joiningOption: DbContextScopeOption.ForceCreateNew, isolationLevel: isolationLevel, dbContextFactory: dbContextFactory, nameOrConnectionString)
{ }

public DbContextReadOnlyScope(DbContextScopeOption joiningOption, IsolationLevel? isolationLevel, IDbContextFactory dbContextFactory = null)
public DbContextReadOnlyScope(DbContextScopeOption joiningOption, IsolationLevel? isolationLevel, IDbContextFactory dbContextFactory = null,
string nameOrConnectionString = null)
{
_internalScope = new DbContextScope(joiningOption: joiningOption, readOnly: true, isolationLevel: isolationLevel, dbContextFactory: dbContextFactory);
_internalScope = new DbContextScope(joiningOption: joiningOption, readOnly: true, isolationLevel: isolationLevel, dbContextFactory: dbContextFactory, nameOrConnectionString);
}

public void Dispose()
Expand Down
5 changes: 3 additions & 2 deletions Mehdime.Entity/Implementations/DbContextScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public DbContextScope(bool readOnly, IDbContextFactory dbContextFactory = null)
: this(joiningOption: DbContextScopeOption.JoinExisting, readOnly: readOnly, isolationLevel: null, dbContextFactory: dbContextFactory)
{}

public DbContextScope(DbContextScopeOption joiningOption, bool readOnly, IsolationLevel? isolationLevel, IDbContextFactory dbContextFactory = null)
public DbContextScope(DbContextScopeOption joiningOption, bool readOnly, IsolationLevel? isolationLevel, IDbContextFactory dbContextFactory = null,
string nameOrConnectionString = null)
{
if (isolationLevel.HasValue && joiningOption == DbContextScopeOption.JoinExisting)
throw new ArgumentException("Cannot join an ambient DbContextScope when an explicit database transaction is required. When requiring explicit database transactions to be used (i.e. when the 'isolationLevel' parameter is set), you must not also ask to join the ambient context (i.e. the 'joinAmbient' parameter must be set to false).");
Expand All @@ -61,7 +62,7 @@ public DbContextScope(DbContextScopeOption joiningOption, bool readOnly, Isolati
else
{
_nested = false;
_dbContexts = new DbContextCollection(readOnly, isolationLevel, dbContextFactory);
_dbContexts = new DbContextCollection(readOnly, isolationLevel, dbContextFactory, nameOrConnectionString);
}

SetAmbientScope(this);
Expand Down
20 changes: 12 additions & 8 deletions Mehdime.Entity/Implementations/DbContextScopeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,42 @@ public DbContextScopeFactory(IDbContextFactory dbContextFactory = null)
_dbContextFactory = dbContextFactory;
}

public IDbContextScope Create(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
public IDbContextScope Create(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting, string nameOrConnectionString = null)
{
return new DbContextScope(
joiningOption: joiningOption,
readOnly: false,
isolationLevel: null,
dbContextFactory: _dbContextFactory);
dbContextFactory: _dbContextFactory,
nameOrConnectionString);
}

public IDbContextReadOnlyScope CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting)
public IDbContextReadOnlyScope CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting, string nameOrConnectionString = null)
{
return new DbContextReadOnlyScope(
joiningOption: joiningOption,
isolationLevel: null,
dbContextFactory: _dbContextFactory);
dbContextFactory: _dbContextFactory,
nameOrConnectionString);
}

public IDbContextScope CreateWithTransaction(IsolationLevel isolationLevel)
public IDbContextScope CreateWithTransaction(IsolationLevel isolationLevel, string nameOrConnectionString = null)
{
return new DbContextScope(
joiningOption: DbContextScopeOption.ForceCreateNew,
readOnly: false,
isolationLevel: isolationLevel,
dbContextFactory: _dbContextFactory);
dbContextFactory: _dbContextFactory,
nameOrConnectionString);
}

public IDbContextReadOnlyScope CreateReadOnlyWithTransaction(IsolationLevel isolationLevel)
public IDbContextReadOnlyScope CreateReadOnlyWithTransaction(IsolationLevel isolationLevel, string nameOrConnectionString = null)
{
return new DbContextReadOnlyScope(
joiningOption: DbContextScopeOption.ForceCreateNew,
isolationLevel: isolationLevel,
dbContextFactory: _dbContextFactory);
dbContextFactory: _dbContextFactory,
nameOrConnectionString);
}

public IDisposable SuppressAmbientContext()
Expand Down
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>(string nameOrConnectionString) where TDbContext : DbContext;
}
}
8 changes: 4 additions & 4 deletions Mehdime.Entity/Interfaces/IDbContextScopeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public interface IDbContextScopeFactory
/// is an advanced feature that should be used with great care and only if you fully understand the
/// implications of doing this.
/// </summary>
IDbContextScope Create(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting);
IDbContextScope Create(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting, string nameOrConnectionString = null);

/// <summary>
/// Creates a new DbContextScope for read-only queries.
Expand All @@ -42,7 +42,7 @@ public interface IDbContextScopeFactory
/// is an advanced feature that should be used with great care and only if you fully understand the
/// implications of doing this.
/// </summary>
IDbContextReadOnlyScope CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting);
IDbContextReadOnlyScope CreateReadOnly(DbContextScopeOption joiningOption = DbContextScopeOption.JoinExisting, string nameOrConnectionString = null);

/// <summary>
/// Forces the creation of a new ambient DbContextScope (i.e. does not
Expand All @@ -58,7 +58,7 @@ public interface IDbContextScopeFactory
/// This is an advanced feature that you should use very carefully
/// and only if you fully understand the implications of doing this.
/// </summary>
IDbContextScope CreateWithTransaction(IsolationLevel isolationLevel);
IDbContextScope CreateWithTransaction(IsolationLevel isolationLevel, string nameOrConnectionString = null);

/// <summary>
/// Forces the creation of a new ambient read-only DbContextScope (i.e. does not
Expand All @@ -74,7 +74,7 @@ public interface IDbContextScopeFactory
/// This is an advanced feature that you should use very carefully
/// and only if you fully understand the implications of doing this.
/// </summary>
IDbContextReadOnlyScope CreateReadOnlyWithTransaction(IsolationLevel isolationLevel);
IDbContextReadOnlyScope CreateReadOnlyWithTransaction(IsolationLevel isolationLevel, string nameOrConnectionString = null);

/// <summary>
/// Temporarily suppresses the ambient DbContextScope.
Expand Down
3 changes: 2 additions & 1 deletion Mehdime.Entity/Mehdime.Entity.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Mehdime.Entity</RootNamespace>
<AssemblyName>Mehdime.Entity</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
4 changes: 2 additions & 2 deletions Mehdime.Entity/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.2.0")]
[assembly: AssemblyFileVersion("1.1.2.0")]