Skip to content

Commit

Permalink
Switch to using declarations in product code
Browse files Browse the repository at this point in the history
  • Loading branch information
roji committed Nov 14, 2019
1 parent 3782e7d commit 5862d23
Show file tree
Hide file tree
Showing 29 changed files with 1,161 additions and 1,337 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,9 @@ private static void IncludeCollection<TIncludingEntity, TIncludedEntity>(
entry.SetIsLoaded(navigation);
if (relatedEntities != null)
{
using (var enumerator = relatedEntities.GetEnumerator())
using var enumerator = relatedEntities.GetEnumerator();
while (enumerator.MoveNext())
{
while (enumerator.MoveNext())
{
}
}
}
else
Expand Down
88 changes: 37 additions & 51 deletions src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,15 @@ public virtual async Task<bool> DeleteDatabaseOnceAsync(
object __,
CancellationToken cancellationToken = default)
{
using (var response = await Client.GetDatabase(_databaseId).DeleteStreamAsync(cancellationToken: cancellationToken)
.ConfigureAwait(false))
using var response = await Client.GetDatabase(_databaseId).DeleteStreamAsync(cancellationToken: cancellationToken)
.ConfigureAwait(false);
if (response.StatusCode == HttpStatusCode.NotFound)
{
if (response.StatusCode == HttpStatusCode.NotFound)
{
return false;
}

response.EnsureSuccessStatusCode();
return response.StatusCode == HttpStatusCode.NoContent;
return false;
}

response.EnsureSuccessStatusCode();
return response.StatusCode == HttpStatusCode.NoContent;
}

/// <summary>
Expand Down Expand Up @@ -229,22 +227,20 @@ private async Task<bool> CreateContainerIfNotExistsOnceAsync(
(string ContainerId, string PartitionKey) parameters,
CancellationToken cancellationToken = default)
{
using (var response = await Client.GetDatabase(_databaseId).CreateContainerStreamAsync(
using var response = await Client.GetDatabase(_databaseId).CreateContainerStreamAsync(
new ContainerProperties(parameters.ContainerId, "/" + parameters.PartitionKey)
{
PartitionKeyDefinitionVersion = PartitionKeyDefinitionVersion.V2
},
cancellationToken: cancellationToken)
.ConfigureAwait(false))
.ConfigureAwait(false);
if (response.StatusCode == HttpStatusCode.Conflict)
{
if (response.StatusCode == HttpStatusCode.Conflict)
{
return false;
}

response.EnsureSuccessStatusCode();
return response.StatusCode == HttpStatusCode.Created;
return false;
}

response.EnsureSuccessStatusCode();
return response.StatusCode == HttpStatusCode.Created;
}

/// <summary>
Expand Down Expand Up @@ -284,21 +280,17 @@ private async Task<bool> CreateItemOnceAsync(
(string ContainerId, JToken Document, string PartitionKey) parameters,
CancellationToken cancellationToken = default)
{
await using (var stream = new MemoryStream())
await using (var writer = new StreamWriter(stream, new UTF8Encoding(), bufferSize: 1024, leaveOpen: false))
using (var jsonWriter = new JsonTextWriter(writer))
{
JsonSerializer.Create().Serialize(jsonWriter, parameters.Document);
await jsonWriter.FlushAsync(cancellationToken);
await using var stream = new MemoryStream();
await using var writer = new StreamWriter(stream, new UTF8Encoding(), bufferSize: 1024, leaveOpen: false);
using var jsonWriter = new JsonTextWriter(writer);
JsonSerializer.Create().Serialize(jsonWriter, parameters.Document);
await jsonWriter.FlushAsync(cancellationToken);

var container = Client.GetDatabase(_databaseId).GetContainer(parameters.ContainerId);
var partitionKey = CreatePartitionKey(parameters.PartitionKey);
using (var response = await container.CreateItemStreamAsync(stream, partitionKey, null, cancellationToken))
{
response.EnsureSuccessStatusCode();
return response.StatusCode == HttpStatusCode.Created;
}
}
var container = Client.GetDatabase(_databaseId).GetContainer(parameters.ContainerId);
var partitionKey = CreatePartitionKey(parameters.PartitionKey);
using var response = await container.CreateItemStreamAsync(stream, partitionKey, null, cancellationToken);
response.EnsureSuccessStatusCode();
return response.StatusCode == HttpStatusCode.Created;
}

/// <summary>
Expand Down Expand Up @@ -342,20 +334,16 @@ private async Task<bool> ReplaceItemOnceAsync(
{
using var stream = new MemoryStream();
using var writer = new StreamWriter(stream, new UTF8Encoding(), bufferSize: 1024, leaveOpen: false);
using (var jsonWriter = new JsonTextWriter(writer))
{
JsonSerializer.Create().Serialize(jsonWriter, parameters.Document);
await jsonWriter.FlushAsync(cancellationToken);
using var jsonWriter = new JsonTextWriter(writer);
JsonSerializer.Create().Serialize(jsonWriter, parameters.Document);
await jsonWriter.FlushAsync(cancellationToken);

var container = Client.GetDatabase(_databaseId).GetContainer(parameters.ContainerId);
var partitionKey = CreatePartitionKey(parameters.PartitionKey);
using (var response = await container.ReplaceItemStreamAsync(
stream, parameters.ItemId, partitionKey, null, cancellationToken))
{
response.EnsureSuccessStatusCode();
return response.StatusCode == HttpStatusCode.OK;
}
}
var container = Client.GetDatabase(_databaseId).GetContainer(parameters.ContainerId);
var partitionKey = CreatePartitionKey(parameters.PartitionKey);
using var response = await container.ReplaceItemStreamAsync(
stream, parameters.ItemId, partitionKey, null, cancellationToken);
response.EnsureSuccessStatusCode();
return response.StatusCode == HttpStatusCode.OK;
}

/// <summary>
Expand Down Expand Up @@ -409,12 +397,10 @@ public virtual async Task<bool> DeleteItemOnceAsync(
{
var items = Client.GetDatabase(_databaseId).GetContainer(parameters.ContainerId);
var partitionKey = CreatePartitionKey(parameters.PartitionKey);
using (var response = await items.DeleteItemStreamAsync(
parameters.DocumentId, partitionKey, cancellationToken: cancellationToken))
{
response.EnsureSuccessStatusCode();
return response.StatusCode == HttpStatusCode.NoContent;
}
using var response = await items.DeleteItemStreamAsync(
parameters.DocumentId, partitionKey, cancellationToken: cancellationToken);
response.EnsureSuccessStatusCode();
return response.StatusCode == HttpStatusCode.NoContent;
}

private PartitionKey CreatePartitionKey(string partitionKey)
Expand Down
48 changes: 21 additions & 27 deletions src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,16 @@ protected DbContextOperations(
/// </summary>
public virtual void DropDatabase([CanBeNull] string contextType)
{
using (var context = CreateContext(contextType))
using var context = CreateContext(contextType);
var connection = context.Database.GetDbConnection();
_reporter.WriteInformation(DesignStrings.DroppingDatabase(connection.Database));
if (context.Database.EnsureDeleted())
{
var connection = context.Database.GetDbConnection();
_reporter.WriteInformation(DesignStrings.DroppingDatabase(connection.Database));
if (context.Database.EnsureDeleted())
{
_reporter.WriteInformation(DesignStrings.DatabaseDropped(connection.Database));
}
else
{
_reporter.WriteInformation(DesignStrings.NotExistDatabase(connection.Database));
}
_reporter.WriteInformation(DesignStrings.DatabaseDropped(connection.Database));
}
else
{
_reporter.WriteInformation(DesignStrings.NotExistDatabase(connection.Database));
}
}

Expand All @@ -103,10 +101,8 @@ public virtual void DropDatabase([CanBeNull] string contextType)
/// </summary>
public virtual string ScriptDbContext([CanBeNull] string contextType)
{
using (var context = CreateContext(contextType))
{
return context.Database.GenerateCreateScript();
}
using var context = CreateContext(contextType);
return context.Database.GenerateCreateScript();
}

/// <summary>
Expand Down Expand Up @@ -228,22 +224,20 @@ where i.GetTypeInfo().IsGenericType
/// </summary>
public virtual ContextInfo GetContextInfo([CanBeNull] string contextType)
{
using (var context = CreateContext(contextType))
{
var info = new ContextInfo();
using var context = CreateContext(contextType);
var info = new ContextInfo();

var provider = context.GetService<IDatabaseProvider>();
info.ProviderName = provider.Name;
var provider = context.GetService<IDatabaseProvider>();
info.ProviderName = provider.Name;

var connection = context.Database.GetDbConnection();
info.DataSource = connection.DataSource;
info.DatabaseName = connection.Database;
var connection = context.Database.GetDbConnection();
info.DataSource = connection.DataSource;
info.DatabaseName = connection.Database;

var options = context.GetService<IDbContextOptions>();
info.Options = options.BuildOptionsFragment().Trim();
var options = context.GetService<IDbContextOptions>();
info.Options = options.BuildOptionsFragment().Trim();

return info;
}
return info;
}

private Func<DbContext> FindContextFactory(Type contextType)
Expand Down
64 changes: 28 additions & 36 deletions src/EFCore.Design/Design/Internal/MigrationsOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,16 @@ public virtual MigrationFiles AddMigration(

var subNamespace = SubnamespaceFromOutputPath(outputDir);

using (var context = _contextOperations.CreateContext(contextType))
{
var services = _servicesBuilder.Build(context);
EnsureServices(services);
EnsureMigrationsAssembly(services);
using var context = _contextOperations.CreateContext(contextType);
var services = _servicesBuilder.Build(context);
EnsureServices(services);
EnsureMigrationsAssembly(services);

var scaffolder = services.GetRequiredService<IMigrationsScaffolder>();
var migration = scaffolder.ScaffoldMigration(name, _rootNamespace, subNamespace, _language);
var files = scaffolder.Save(_projectDir, migration, outputDir);
var scaffolder = services.GetRequiredService<IMigrationsScaffolder>();
var migration = scaffolder.ScaffoldMigration(name, _rootNamespace, subNamespace, _language);
var files = scaffolder.Save(_projectDir, migration, outputDir);

return files;
}
return files;
}

// if outputDir is a subfolder of projectDir, then use each subfolder as a subnamespace
Expand Down Expand Up @@ -133,17 +131,15 @@ private string SubnamespaceFromOutputPath(string outputDir)
public virtual IEnumerable<MigrationInfo> GetMigrations(
[CanBeNull] string contextType)
{
using (var context = _contextOperations.CreateContext(contextType))
{
var services = _servicesBuilder.Build(context);
EnsureServices(services);
using var context = _contextOperations.CreateContext(contextType);
var services = _servicesBuilder.Build(context);
EnsureServices(services);

var migrationsAssembly = services.GetRequiredService<IMigrationsAssembly>();
var idGenerator = services.GetRequiredService<IMigrationsIdGenerator>();
var migrationsAssembly = services.GetRequiredService<IMigrationsAssembly>();
var idGenerator = services.GetRequiredService<IMigrationsIdGenerator>();

return from id in migrationsAssembly.Migrations.Keys
select new MigrationInfo { Id = id, Name = idGenerator.GetName(id) };
}
return from id in migrationsAssembly.Migrations.Keys
select new MigrationInfo { Id = id, Name = idGenerator.GetName(id) };
}

/// <summary>
Expand All @@ -158,15 +154,13 @@ public virtual string ScriptMigration(
bool idempotent,
[CanBeNull] string contextType)
{
using (var context = _contextOperations.CreateContext(contextType))
{
var services = _servicesBuilder.Build(context);
EnsureServices(services);
using var context = _contextOperations.CreateContext(contextType);
var services = _servicesBuilder.Build(context);
EnsureServices(services);

var migrator = services.GetRequiredService<IMigrator>();
var migrator = services.GetRequiredService<IMigrator>();

return migrator.GenerateScript(fromMigration, toMigration, idempotent);
}
return migrator.GenerateScript(fromMigration, toMigration, idempotent);
}

/// <summary>
Expand Down Expand Up @@ -201,20 +195,18 @@ public virtual void UpdateDatabase(
public virtual MigrationFiles RemoveMigration(
[CanBeNull] string contextType, bool force)
{
using (var context = _contextOperations.CreateContext(contextType))
{
var services = _servicesBuilder.Build(context);
EnsureServices(services);
EnsureMigrationsAssembly(services);
using var context = _contextOperations.CreateContext(contextType);
var services = _servicesBuilder.Build(context);
EnsureServices(services);
EnsureMigrationsAssembly(services);

var scaffolder = services.GetRequiredService<IMigrationsScaffolder>();
var scaffolder = services.GetRequiredService<IMigrationsScaffolder>();

var files = scaffolder.RemoveMigration(_projectDir, _rootNamespace, force, _language);
var files = scaffolder.RemoveMigration(_projectDir, _rootNamespace, force, _language);

_reporter.WriteInformation(DesignStrings.Done);
_reporter.WriteInformation(DesignStrings.Done);

return files;
}
return files;
}

private static void EnsureServices(IServiceProvider services)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,9 @@ public static ConventionSet Build()
.UseInternalServiceProvider(p))
.BuildServiceProvider();

using (var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope())
{
using (var context = serviceScope.ServiceProvider.GetService<DbContext>())
{
return ConventionSet.CreateConventionSet(context);
}
}
using var serviceScope = serviceProvider.GetRequiredService<IServiceScopeFactory>().CreateScope();
using var context = serviceScope.ServiceProvider.GetService<DbContext>();
return ConventionSet.CreateConventionSet(context);
}
}
}
10 changes: 4 additions & 6 deletions src/EFCore.Proxies/Proxies/Internal/ProxiesOptionsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,11 @@ public virtual void Validate(IDbContextOptions options)
var internalServiceProvider = options.FindExtension<CoreOptionsExtension>()?.InternalServiceProvider;
if (internalServiceProvider != null)
{
using (var scope = internalServiceProvider.CreateScope())
using var scope = internalServiceProvider.CreateScope();
var conventionPlugins = scope.ServiceProvider.GetService<IEnumerable<IConventionSetPlugin>>();
if (conventionPlugins?.Any(s => s is ProxiesConventionSetPlugin) == false)
{
var conventionPlugins = scope.ServiceProvider.GetService<IEnumerable<IConventionSetPlugin>>();
if (conventionPlugins?.Any(s => s is ProxiesConventionSetPlugin) == false)
{
throw new InvalidOperationException(ProxiesStrings.ProxyServicesMissing);
}
throw new InvalidOperationException(ProxiesStrings.ProxyServicesMissing);
}
}
}
Expand Down
Loading

0 comments on commit 5862d23

Please sign in to comment.