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

Add IsCosmos extension method. #16091

Merged
merged 1 commit into from
Jun 14, 2019
Merged
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
20 changes: 20 additions & 0 deletions src/EFCore.Cosmos/Extensions/CosmosDatabaseFacadeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.Azure.Cosmos;
using Microsoft.EntityFrameworkCore.Cosmos.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
Expand Down Expand Up @@ -39,5 +41,23 @@ private static TService GetService<TService>(IInfrastructure<IServiceProvider> d

return service;
}

/// <summary>
/// <para>
/// Returns <c>true</c> if the database provider currently in use is the Cosmos provider.
/// </para>
/// <para>
/// This method can only be used after the <see cref="DbContext" /> has been configured because
/// it is only then that the provider is known. This means that this method cannot be used
/// in <see cref="DbContext.OnConfiguring" /> because this is where application code sets the
/// provider to use as part of configuring the context.
/// </para>
/// </summary>
/// <param name="database"> The facade from <see cref="DbContext.Database" />. </param>
/// <returns> <c>true</c> if the Cosmos provider is being used. </returns>
public static bool IsCosmos([NotNull] this DatabaseFacade database)
=> database.ProviderName.Equals(
typeof(CosmosOptionsExtension).GetTypeInfo().Assembly.GetName().Name,
StringComparison.Ordinal);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public static DbContextOptionsBuilder UseCosmos(
Check.NotEmpty(authKeyOrResourceToken, nameof(authKeyOrResourceToken));
Check.NotEmpty(databaseName, nameof(databaseName));

var extension = optionsBuilder.Options.FindExtension<CosmosDbOptionsExtension>()
?? new CosmosDbOptionsExtension();
var extension = optionsBuilder.Options.FindExtension<CosmosOptionsExtension>()
?? new CosmosOptionsExtension();

extension = extension
.WithServiceEndPoint(serviceEndPoint)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static IServiceCollection AddEntityFrameworkCosmos([NotNull] this IServic

var builder = new EntityFrameworkServicesBuilder(serviceCollection)
.TryAdd<LoggingDefinitions, CosmosLoggingDefinitions>()
.TryAdd<IDatabaseProvider, DatabaseProvider<CosmosDbOptionsExtension>>()
.TryAdd<IDatabaseProvider, DatabaseProvider<CosmosOptionsExtension>>()
.TryAdd<IDatabase, CosmosDatabaseWrapper>()
.TryAdd<IExecutionStrategyFactory, CosmosExecutionStrategyFactory>()
.TryAdd<IDbContextTransactionManager, CosmosTransactionManager>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public virtual CosmosDbContextOptionsBuilder Region(string region)
/// </summary>
/// <param name="setAction"> An action to set the option. </param>
/// <returns> The same builder instance so that multiple calls can be chained. </returns>
protected virtual CosmosDbContextOptionsBuilder WithOption([NotNull] Func<CosmosDbOptionsExtension, CosmosDbOptionsExtension> setAction)
protected virtual CosmosDbContextOptionsBuilder WithOption([NotNull] Func<CosmosOptionsExtension, CosmosOptionsExtension> setAction)
{
((IDbContextOptionsBuilderInfrastructure)OptionsBuilder).AddOrUpdateExtension(
setAction(OptionsBuilder.Options.FindExtension<CosmosDbOptionsExtension>() ?? new CosmosDbOptionsExtension()));
setAction(OptionsBuilder.Options.FindExtension<CosmosOptionsExtension>() ?? new CosmosOptionsExtension()));

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Microsoft.EntityFrameworkCore.Cosmos.Infrastructure.Internal
{
public class CosmosDbOptionsExtension : IDbContextOptionsExtension
public class CosmosOptionsExtension : IDbContextOptionsExtension
{
private string _serviceEndPoint;
private string _authKeyOrResourceToken;
Expand All @@ -23,11 +23,11 @@ public class CosmosDbOptionsExtension : IDbContextOptionsExtension
private string _logFragment;
private long? _serviceProviderHash;

public CosmosDbOptionsExtension()
public CosmosOptionsExtension()
{
}

protected CosmosDbOptionsExtension(CosmosDbOptionsExtension copyFrom)
protected CosmosOptionsExtension(CosmosOptionsExtension copyFrom)
{
_serviceEndPoint = copyFrom._serviceEndPoint;
_authKeyOrResourceToken = copyFrom._authKeyOrResourceToken;
Expand All @@ -38,7 +38,7 @@ protected CosmosDbOptionsExtension(CosmosDbOptionsExtension copyFrom)

public virtual string ServiceEndPoint => _serviceEndPoint;

public virtual CosmosDbOptionsExtension WithServiceEndPoint(string serviceEndPoint)
public virtual CosmosOptionsExtension WithServiceEndPoint(string serviceEndPoint)
{
var clone = Clone();

Expand All @@ -49,7 +49,7 @@ public virtual CosmosDbOptionsExtension WithServiceEndPoint(string serviceEndPoi

public virtual string AuthKeyOrResourceToken => _authKeyOrResourceToken;

public virtual CosmosDbOptionsExtension WithAuthKeyOrResourceToken(string authKeyOrResourceToken)
public virtual CosmosOptionsExtension WithAuthKeyOrResourceToken(string authKeyOrResourceToken)
{
var clone = Clone();

Expand All @@ -60,7 +60,7 @@ public virtual CosmosDbOptionsExtension WithAuthKeyOrResourceToken(string authKe

public virtual string DatabaseName => _databaseName;

public virtual CosmosDbOptionsExtension WithDatabaseName(string database)
public virtual CosmosOptionsExtension WithDatabaseName(string database)
{
var clone = Clone();

Expand All @@ -71,7 +71,7 @@ public virtual CosmosDbOptionsExtension WithDatabaseName(string database)

public virtual string Region => _region;

public virtual CosmosDbOptionsExtension WithRegion(string region)
public virtual CosmosOptionsExtension WithRegion(string region)
{
var clone = Clone();

Expand All @@ -92,7 +92,7 @@ public virtual CosmosDbOptionsExtension WithRegion(string region)
/// </summary>
/// <param name="executionStrategyFactory"> The option to change. </param>
/// <returns> A new instance with the option changed. </returns>
public virtual CosmosDbOptionsExtension WithExecutionStrategyFactory(
public virtual CosmosOptionsExtension WithExecutionStrategyFactory(
[CanBeNull] Func<ExecutionStrategyDependencies, IExecutionStrategy> executionStrategyFactory)
{
var clone = Clone();
Expand All @@ -102,7 +102,7 @@ public virtual CosmosDbOptionsExtension WithExecutionStrategyFactory(
return clone;
}

protected virtual CosmosDbOptionsExtension Clone() => new CosmosDbOptionsExtension(this);
protected virtual CosmosOptionsExtension Clone() => new CosmosOptionsExtension(this);

public bool ApplyServices(IServiceCollection services)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class CosmosSingletonOptions : ICosmosSingletonOptions
/// </summary>
public virtual void Initialize(IDbContextOptions options)
{
var cosmosOptions = options.FindExtension<CosmosDbOptionsExtension>();
var cosmosOptions = options.FindExtension<CosmosOptionsExtension>();
if (cosmosOptions != null)
{
ServiceEndPoint = cosmosOptions.ServiceEndPoint;
Expand All @@ -72,7 +72,7 @@ public virtual void Initialize(IDbContextOptions options)
/// </summary>
public virtual void Validate(IDbContextOptions options)
{
var inMemoryOptions = options.FindExtension<CosmosDbOptionsExtension>();
var inMemoryOptions = options.FindExtension<CosmosOptionsExtension>();

if (inMemoryOptions != null
&& (ServiceEndPoint != inMemoryOptions.ServiceEndPoint
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public CosmosClientWrapper(
[NotNull] IExecutionStrategyFactory executionStrategyFactory,
[NotNull] IDiagnosticsLogger<DbLoggerCategory.Database.Command> commandLogger)
{
var options = dbContextOptions.FindExtension<CosmosDbOptionsExtension>();
var options = dbContextOptions.FindExtension<CosmosOptionsExtension>();

_singletonWrapper = singletonWrapper;
_databaseId = options.DatabaseName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public CosmosExecutionStrategyFactory([NotNull] ExecutionStrategyDependencies de

Dependencies = dependencies;

_createExecutionStrategy = dependencies.Options?.FindExtension<CosmosDbOptionsExtension>()?.ExecutionStrategyFactory
_createExecutionStrategy = dependencies.Options?.FindExtension<CosmosOptionsExtension>()?.ExecutionStrategyFactory
?? CreateDefaultStrategy;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class InMemoryDatabaseFacadeExtensions
{
/// <summary>
/// <para>
/// Returns true if the database provider currently in use is the in-memory provider.
/// Returns <c>true</c> if the database provider currently in use is the in-memory provider.
/// </para>
/// <para>
/// This method can only be used after the <see cref="DbContext" /> has been configured because
Expand All @@ -27,7 +27,7 @@ public static class InMemoryDatabaseFacadeExtensions
/// </para>
/// </summary>
/// <param name="database"> The facade from <see cref="DbContext.Database" />. </param>
/// <returns> True if the in-memory database is being used; false otherwise. </returns>
/// <returns> <c>true</c> if the in-memory database is being used. </returns>
public static bool IsInMemory([NotNull] this DatabaseFacade database)
=> database.ProviderName.Equals(
typeof(InMemoryOptionsExtension).GetTypeInfo().Assembly.GetName().Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public void Cosmos_client_instance_is_shared_between_contexts()
{
client = context.Database.GetCosmosClient();
Assert.NotNull(client);
Assert.True(context.Database.IsCosmos());
}

using (var context = new CustomerContext(options))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ public void Can_create_options_with_specified_region()
o => { o.Region(regionName); });

var extension = options
.Options.FindExtension<CosmosDbOptionsExtension>();
.Options.FindExtension<CosmosOptionsExtension>();

Assert.Equal(regionName, extension.Region);
}

/// <summary>
/// The region will be checked by the cosmosdb sdk, because the region list is not constant
/// </summary>
[ConditionalFact]
public void Can_create_options_with_wrong_region()
{
Expand All @@ -37,8 +34,9 @@ public void Can_create_options_with_wrong_region()
o => { o.Region(regionName); });

var extension = options
.Options.FindExtension<CosmosDbOptionsExtension>();
.Options.FindExtension<CosmosOptionsExtension>();

// The region will be validated by the Cosmos SDK, because the region list is not constant
Assert.Equal(regionName, extension.Region);
}
}
Expand Down