Skip to content

Commit

Permalink
drop sql classic
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Aug 30, 2024
1 parent cacd279 commit 70fb82b
Show file tree
Hide file tree
Showing 20 changed files with 31 additions and 153 deletions.
4 changes: 2 additions & 2 deletions pages/directory-and-name-resolution.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public Task<SqlDatabase> Build(
string? databaseSuffix = null,
[CallerMemberName] string memberName = "")
```
<sup><a href='/src/LocalDb/SqlInstance.cs#L128-L150' title='Snippet source file'>snippet source</a> | <a href='#snippet-ConventionBuildSignature' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/LocalDb/SqlInstance.cs#L99-L121' title='Snippet source file'>snippet source</a> | <a href='#snippet-ConventionBuildSignature' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

With these parameters the database name is the derived as follows:
Expand Down Expand Up @@ -150,7 +150,7 @@ If full control over the database name is required, there is an overload that ta
/// </summary>
public async Task<SqlDatabase> Build(string dbName)
```
<sup><a href='/src/LocalDb/SqlInstance.cs#L165-L172' title='Snippet source file'>snippet source</a> | <a href='#snippet-ExplicitBuildSignature' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/LocalDb/SqlInstance.cs#L136-L143' title='Snippet source file'>snippet source</a> | <a href='#snippet-ExplicitBuildSignature' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Which can be used as follows:
Expand Down
1 change: 0 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
<Using Include="System.Security.Principal" />
<Using Include="System.Runtime.InteropServices" />
<Using Include="System.Linq.Expressions" />
<Using Include="System.Data.SqlClient.SqlConnection" Alias="DataSqlConnection" />
<Using Include="Microsoft.Extensions.DependencyInjection" Condition="$(TargetFramework) == 'net7.0' or $(TargetFramework) == 'net8.0'" />
</ItemGroup>
</Project>
6 changes: 4 additions & 2 deletions src/EfClassicLocalDb.Tests/Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[Collection("Sequential")]
using System.Data.SqlClient;

[Collection("Sequential")]
public class Tests
{
static SqlInstance<TestDbContext> instance;
Expand All @@ -21,7 +23,7 @@ public async Task ServiceScope()
{
static void Add(List<object?> objects, IServiceProvider provider)
{
objects.Add(provider.GetService<DataSqlConnection>());
objects.Add(provider.GetService<SqlConnection>());
objects.Add(provider.GetService<TestDbContext>());
}

Expand Down
3 changes: 2 additions & 1 deletion src/EfClassicLocalDb/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
global using System.Data.Entity;
global using System.ComponentModel;
global using System.ComponentModel;
global using System.Data.SqlClient;
2 changes: 1 addition & 1 deletion src/EfClassicLocalDb/QuietConfig/ManifestTokenResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ManifestTokenResolver :

public string ResolveManifestToken(DbConnection connection)
{
if (connection is DataSqlConnection)
if (connection is SqlConnection)
{
return "2012";
}
Expand Down
14 changes: 4 additions & 10 deletions src/EfClassicLocalDb/ServiceScope.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
#if(NET7_0_OR_GREATER)
class ServiceScope :
class ServiceScope(DbContext context, SqlConnection connection) :
IServiceScope,
IServiceProvider
{
DbContext context;
DataSqlConnection connection;

public ServiceScope(DbContext context, DataSqlConnection connection)
{
this.context = context;
this.connection = connection;
}
DbContext context = context;
SqlConnection connection = connection;

public void Dispose()
{
Expand All @@ -22,7 +16,7 @@ public void Dispose()

public object? GetService(Type type)
{
if (type == typeof(DataSqlConnection))
if (type == typeof(SqlConnection))
{
return connection;
}
Expand Down
8 changes: 4 additions & 4 deletions src/EfClassicLocalDb/SqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ internal SqlDatabase(
}

public string Name { get; }
public DataSqlConnection Connection { get; }
public SqlConnection Connection { get; }
public string ConnectionString { get; }

public async Task<DataSqlConnection> OpenNewConnection()
public async Task<SqlConnection> OpenNewConnection()
{
var connection = new DataSqlConnection(ConnectionString);
var connection = new SqlConnection(ConnectionString);
await connection.OpenAsync();
return connection;
}

public static implicit operator TDbContext(SqlDatabase<TDbContext> instance) => instance.Context;

public static implicit operator DataSqlConnection(SqlDatabase<TDbContext> instance) => instance.Connection;
public static implicit operator SqlConnection(SqlDatabase<TDbContext> instance) => instance.Connection;

public async Task Start()
{
Expand Down
4 changes: 2 additions & 2 deletions src/EfClassicLocalDb/SqlDatabase_Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public partial class SqlDatabase<TDbContext> :
{
public object? GetService(Type type)
{
if (type == typeof(DataSqlConnection))
if (type == typeof(SqlConnection))
{
return Connection;
}
Expand All @@ -31,7 +31,7 @@ public partial class SqlDatabase<TDbContext> :
#if(NET7_0_OR_GREATER)
public IServiceScope CreateScope()
{
var connection = new DataSqlConnection(ConnectionString);
var connection = new SqlConnection(ConnectionString);
connection.Open();
return new ServiceScope(NewDbContext(), connection);
}
Expand Down
2 changes: 1 addition & 1 deletion src/EfClassicLocalDb/SqlInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public SqlInstance(
}

Wrapper = new(
_ => new DataSqlConnection(_),
_ => new SqlConnection(_),
storageValue.Name,
storageValue.Directory,
templateSize,
Expand Down
1 change: 0 additions & 1 deletion src/EfLocalDb.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public async Task ServiceScope()
{
static void Add(List<object?> objects, IServiceProvider provider)
{
objects.Add(provider.GetService<DataSqlConnection>());
objects.Add(provider.GetService<SqlConnection>());
objects.Add(provider.GetService<TestDbContext>());
}
Expand Down
1 change: 0 additions & 1 deletion src/EfLocalDb/EfLocalDb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
<PackageReference Include="Fody" PrivateAssets="all" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
<PackageReference Include="Microsoft.Data.SqlClient" />
<PackageReference Include="System.Data.SqlClient" />
<PackageReference Include="ProjectDefaults" PrivateAssets="all" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<PackageReference Include="System.IO.FileSystem.AccessControl" />
Expand Down
9 changes: 1 addition & 8 deletions src/EfLocalDb/ServiceScope.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,24 @@
class ServiceScope(DbContext context, DataSqlConnection dataConnection, SqlConnection connection) :
class ServiceScope(DbContext context, SqlConnection connection) :
IServiceScope,
IServiceProvider,
IAsyncDisposable
{
public void Dispose()
{
connection.Dispose();
dataConnection.Dispose();
context.Dispose();
}

public async ValueTask DisposeAsync()
{
await connection.DisposeAsync();
await dataConnection.DisposeAsync();
await context.DisposeAsync();
}

public IServiceProvider ServiceProvider => this;

public object? GetService(Type type)
{
if (type == typeof(DataSqlConnection))
{
return dataConnection;
}

if (type == typeof(SqlConnection))
{
return connection;
Expand Down
22 changes: 0 additions & 22 deletions src/EfLocalDb/SqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,10 @@ internal SqlDatabase(
this.sqlOptionsBuilder = sqlOptionsBuilder;
ConnectionString = connectionString;
Connection = new(connectionString);
dataConnection = new(() =>
{
var connection = new DataSqlConnection(connectionString);
connection.Open();
return connection;
});
}

public string Name { get; }
public SqlConnection Connection { get; }
Lazy<DataSqlConnection> dataConnection;
public DataSqlConnection DataConnection => dataConnection.Value;
public string ConnectionString { get; }

public async Task<SqlConnection> OpenNewConnection()
Expand All @@ -49,21 +41,12 @@ public async Task<SqlConnection> OpenNewConnection()
return connection;
}

public async Task<DataSqlConnection> OpenNewDataConnection()
{
var connection = new DataSqlConnection(ConnectionString);
await connection.OpenAsync();
return connection;
}

public static implicit operator TDbContext(SqlDatabase<TDbContext> instance) => instance.Context;

public static implicit operator SqlConnection(SqlDatabase<TDbContext> instance) => instance.Connection;

public static implicit operator DbConnection(SqlDatabase<TDbContext> instance) => instance.Connection;

public static implicit operator DataSqlConnection(SqlDatabase<TDbContext> instance) => instance.DataConnection;

public async Task Start()
{
await Connection.OpenAsync();
Expand Down Expand Up @@ -115,11 +98,6 @@ public async ValueTask DisposeAsync()
// ReSharper restore ConditionIsAlwaysTrueOrFalse

await Connection.DisposeAsync();

if (dataConnection.IsValueCreated)
{
await dataConnection.Value.DisposeAsync();
}
}

public async Task Delete()
Expand Down
9 changes: 1 addition & 8 deletions src/EfLocalDb/SqlDatabase_Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ public partial class SqlDatabase<TDbContext> :
return Connection;
}

if (type == typeof(DataSqlConnection))
{
return DataConnection;
}

if (type == typeof(TDbContext))
{
return Context;
Expand All @@ -38,9 +33,7 @@ public IServiceScope CreateScope()
{
var connection = new SqlConnection(ConnectionString);
connection.Open();
var dataConnection = new DataSqlConnection(ConnectionString);
dataConnection.Open();
return new ServiceScope(NewDbContext(), dataConnection, connection);
return new ServiceScope(NewDbContext(), connection);
}

public AsyncServiceScope CreateAsyncScope() =>
Expand Down
5 changes: 1 addition & 4 deletions src/LocalDb.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ public async Task Simple()
[Fact]
public async Task ServiceScope()
{
static void Add(List<object?> objects, IServiceProvider provider)
{
objects.Add(provider.GetService<DataSqlConnection>());
static void Add(List<object?> objects, IServiceProvider provider) =>
objects.Add(provider.GetService<SqlConnection>());
}

var instance = new SqlInstance("ServiceScope", TestDbBuilder.CreateTable);

Expand Down
1 change: 0 additions & 1 deletion src/LocalDb/LocalDb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
<PackageReference Include="MethodTimer.Fody" PrivateAssets="All" />
<PackageReference Include="Microsoft.Win32.Registry" />
<PackageReference Include="Microsoft.Data.SqlClient" />
<PackageReference Include="System.Data.SqlClient" />
<PackageReference Include="Polyfill" PrivateAssets="all" />
<PackageReference Include="ProjectDefaults" PrivateAssets="all" />
<PackageReference Include="System.IO.FileSystem.AccessControl" />
Expand Down
19 changes: 4 additions & 15 deletions src/LocalDb/ServiceScope.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class ServiceScope(DataSqlConnection dataConnection, SqlConnection connection) :
class ServiceScope(SqlConnection connection) :
#if(NET5_0_OR_GREATER)
IAsyncDisposable,
#endif
Expand All @@ -7,29 +7,18 @@ class ServiceScope(DataSqlConnection dataConnection, SqlConnection connection) :
#endif
IServiceProvider
{
public void Dispose()
{
public void Dispose() =>
connection.Dispose();
dataConnection.Dispose();
}

#if(NET5_0_OR_GREATER)
public async ValueTask DisposeAsync()
{
await connection.DisposeAsync();
await dataConnection.DisposeAsync();
}
public ValueTask DisposeAsync() =>
connection.DisposeAsync();
#endif

public IServiceProvider ServiceProvider => this;

public object? GetService(Type type)
{
if (type == typeof(DataSqlConnection))
{
return dataConnection;
}

if (type == typeof(SqlConnection))
{
return connection;
Expand Down
35 changes: 3 additions & 32 deletions src/LocalDb/SqlDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,32 @@ internal SqlDatabase(string connectionString, string name, Func<Task> delete)
ConnectionString = connectionString;
Name = name;
Connection = new(connectionString);
dataConnection = new(() =>
{
var connection = new DataSqlConnection(connectionString);
connection.Open();
return connection;
});
}

public string ConnectionString { get; }
public string Name { get; }

public SqlConnection Connection { get; }
Lazy<DataSqlConnection> dataConnection;
public DataSqlConnection DataConnection => dataConnection.Value;

public static implicit operator SqlConnection(SqlDatabase instance) => instance.Connection;

public static implicit operator DbConnection(SqlDatabase instance) => instance.Connection;

public static implicit operator DataSqlConnection(SqlDatabase instance) => instance.DataConnection;

public async Task<SqlConnection> OpenNewConnection()
{
var connection = new SqlConnection(ConnectionString);
await connection.OpenAsync();
return connection;
}

public async Task<DataSqlConnection> OpenNewDataConnection()
{
var connection = new DataSqlConnection(ConnectionString);
await connection.OpenAsync();
return connection;
}

public Task Start() => Connection.OpenAsync();

public void Dispose()
{
public void Dispose() =>
Connection.Dispose();
if (dataConnection.IsValueCreated)
{
dataConnection.Value.Dispose();
}
}

#if(!NET48)
public async ValueTask DisposeAsync()
{
await Connection.DisposeAsync();
if (dataConnection.IsValueCreated)
{
await dataConnection.Value.DisposeAsync();
}
}
public ValueTask DisposeAsync() =>
Connection.DisposeAsync();
#endif

// ReSharper disable once ReplaceAsyncWithTaskReturn
Expand Down
Loading

0 comments on commit 70fb82b

Please sign in to comment.