-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make CosmosClient singleton and add a public way to access it.
Part of #12086
- Loading branch information
1 parent
90be0d5
commit bae4df4
Showing
15 changed files
with
490 additions
and
111 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/EFCore.Cosmos/Extensions/CosmosDatabaseFacadeExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using JetBrains.Annotations; | ||
using Microsoft.Azure.Cosmos; | ||
using Microsoft.EntityFrameworkCore.Cosmos.Internal; | ||
using Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Utilities; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.EntityFrameworkCore | ||
{ | ||
/// <summary> | ||
/// Extension methods for the <see cref="DatabaseFacade" /> returned from <see cref="DbContext.Database" /> | ||
/// that can be used only with the Cosmos provider. | ||
/// </summary> | ||
public static class CosmosDatabaseFacadeExtensions | ||
{ | ||
/// <summary> | ||
/// Gets the underlying <see cref="CosmosClient" /> for this <see cref="DbContext" />. | ||
/// </summary> | ||
/// <param name="databaseFacade"> The <see cref="DatabaseFacade" /> for the context. </param> | ||
/// <returns> The <see cref="CosmosClient" /> </returns> | ||
public static CosmosClient GetCosmosClient([NotNull] this DatabaseFacade databaseFacade) | ||
=> GetService<SingletonCosmosClientWrapper>(databaseFacade).Client; | ||
|
||
private static TService GetService<TService>(IInfrastructure<IServiceProvider> databaseFacade) | ||
{ | ||
Check.NotNull(databaseFacade, nameof(databaseFacade)); | ||
|
||
var service = databaseFacade.Instance.GetService<TService>(); | ||
if (service == null) | ||
{ | ||
throw new InvalidOperationException(CosmosStrings.CosmosNotInUse); | ||
} | ||
|
||
return service; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/EFCore.Cosmos/Infrastructure/Internal/CosmosSingletonOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using Microsoft.EntityFrameworkCore.Diagnostics; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Cosmos.Infrastructure.Internal | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </para> | ||
/// <para> | ||
/// The service lifetime is <see cref="ServiceLifetime.Singleton"/>. This means a single instance | ||
/// is used by many <see cref="DbContext"/> instances. The implementation must be thread-safe. | ||
/// This service cannot depend on services registered as <see cref="ServiceLifetime.Scoped"/>. | ||
/// </para> | ||
/// </summary> | ||
public class CosmosSingletonOptions : ICosmosSingletonOptions | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public string ServiceEndPoint { get; private set; } | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public string AuthKeyOrResourceToken { get; private set; } | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public string Region { get; private set; } | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public virtual void Initialize(IDbContextOptions options) | ||
{ | ||
var cosmosOptions = options.FindExtension<CosmosDbOptionsExtension>(); | ||
if (cosmosOptions != null) | ||
{ | ||
ServiceEndPoint = cosmosOptions.ServiceEndPoint; | ||
AuthKeyOrResourceToken = cosmosOptions.AuthKeyOrResourceToken; | ||
Region = cosmosOptions.Region; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
public virtual void Validate(IDbContextOptions options) | ||
{ | ||
var inMemoryOptions = options.FindExtension<CosmosDbOptionsExtension>(); | ||
|
||
if (inMemoryOptions != null | ||
&& (ServiceEndPoint != inMemoryOptions.ServiceEndPoint | ||
|| AuthKeyOrResourceToken != inMemoryOptions.AuthKeyOrResourceToken | ||
|| Region != inMemoryOptions.Region)) | ||
{ | ||
throw new InvalidOperationException( | ||
CoreStrings.SingletonOptionChanged( | ||
nameof(CosmosDbContextOptionsExtensions.UseCosmos), | ||
nameof(DbContextOptionsBuilder.UseInternalServiceProvider))); | ||
} | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
src/EFCore.Cosmos/Infrastructure/Internal/ICosmosSingletonOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.EntityFrameworkCore.Cosmos.Infrastructure.Internal | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </para> | ||
/// <para> | ||
/// The service lifetime is <see cref="ServiceLifetime.Singleton"/> and multiple registrations | ||
/// are allowed. This means a single instance of each service is used by many <see cref="DbContext"/> | ||
/// instances. The implementation must be thread-safe. | ||
/// This service cannot depend on services registered as <see cref="ServiceLifetime.Scoped"/>. | ||
/// </para> | ||
/// </summary> | ||
public interface ICosmosSingletonOptions : ISingletonOptions | ||
{ | ||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
string ServiceEndPoint { get; } | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
string AuthKeyOrResourceToken { get; } | ||
|
||
/// <summary> | ||
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to | ||
/// the same compatibility standards as public APIs. It may be changed or removed without notice in | ||
/// any release. You should only use it directly in your code with extreme caution and knowing that | ||
/// doing so can result in application failures when updating to a new Entity Framework Core release. | ||
/// </summary> | ||
string Region { get; } | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.