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

Interfaces for CosmosClientWrapper and SingletonCosmosClientWrapper #23508

Merged
merged 1 commit into from
Nov 27, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class CosmosDatabaseFacadeExtensions
/// <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;
=> GetService<ISingletonCosmosClientWrapper>(databaseFacade).Client;

private static TService GetService<TService>(IInfrastructure<IServiceProvider> databaseFacade)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ public static IServiceCollection AddEntityFrameworkCosmos([NotNull] this IServic
.TryAddProviderSpecificServices(
b => b
.TryAddSingleton<ICosmosSingletonOptions, CosmosSingletonOptions>()
.TryAddSingleton<SingletonCosmosClientWrapper, SingletonCosmosClientWrapper>()
.TryAddSingleton<ISingletonCosmosClientWrapper, SingletonCosmosClientWrapper>()
.TryAddSingleton<ISqlExpressionFactory, SqlExpressionFactory>()
.TryAddSingleton<IQuerySqlGeneratorFactory, QuerySqlGeneratorFactory>()
.TryAddSingleton<IMethodCallTranslatorProvider, CosmosMethodCallTranslatorProvider>()
.TryAddSingleton<IMemberTranslatorProvider, CosmosMemberTranslatorProvider>()
.TryAddScoped<CosmosClientWrapper, CosmosClientWrapper>()
.TryAddScoped<ICosmosClientWrapper, CosmosClientWrapper>()
);

builder.TryAddCoreServices();
Expand Down
5 changes: 2 additions & 3 deletions src/EFCore.Cosmos/Query/Internal/CosmosQueryContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal;
using Microsoft.EntityFrameworkCore.Query;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.EntityFrameworkCore.ValueGeneration;

#nullable enable

Expand All @@ -27,7 +26,7 @@ public class CosmosQueryContext : QueryContext
/// </summary>
public CosmosQueryContext(
[NotNull] QueryContextDependencies dependencies,
[NotNull] CosmosClientWrapper cosmosClient)
[NotNull] ICosmosClientWrapper cosmosClient)
: base(dependencies)
{
Check.NotNull(cosmosClient, nameof(cosmosClient));
Expand All @@ -41,6 +40,6 @@ public CosmosQueryContext(
/// 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 CosmosClientWrapper CosmosClient { get; }
public virtual ICosmosClientWrapper CosmosClient { get; }
}
}
4 changes: 2 additions & 2 deletions src/EFCore.Cosmos/Query/Internal/CosmosQueryContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Query.Internal
public class CosmosQueryContextFactory : IQueryContextFactory
{
private readonly QueryContextDependencies _dependencies;
private readonly CosmosClientWrapper _cosmosClient;
private readonly ICosmosClientWrapper _cosmosClient;

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -29,7 +29,7 @@ public class CosmosQueryContextFactory : IQueryContextFactory
/// </summary>
public CosmosQueryContextFactory(
[NotNull] QueryContextDependencies dependencies,
[NotNull] CosmosClientWrapper cosmosClient)
[NotNull] ICosmosClientWrapper cosmosClient)
{
Check.NotNull(dependencies, nameof(dependencies));
Check.NotNull(cosmosClient, nameof(cosmosClient));
Expand Down
100 changes: 53 additions & 47 deletions src/EFCore.Cosmos/Storage/Internal/CosmosClientWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal
/// The implementation does not need to be thread-safe.
/// </para>
/// </summary>
public class CosmosClientWrapper
public class CosmosClientWrapper : ICosmosClientWrapper
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -60,7 +60,7 @@ public class CosmosClientWrapper
/// </summary>
public static readonly string DefaultPartitionKey = "__partitionKey";

private readonly SingletonCosmosClientWrapper _singletonWrapper;
private readonly ISingletonCosmosClientWrapper _singletonWrapper;
private readonly string _databaseId;
private readonly IExecutionStrategyFactory _executionStrategyFactory;
private readonly IDiagnosticsLogger<DbLoggerCategory.Database.Command> _commandLogger;
Expand All @@ -79,7 +79,7 @@ static CosmosClientWrapper()
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public CosmosClientWrapper(
[NotNull] SingletonCosmosClientWrapper singletonWrapper,
[NotNull] ISingletonCosmosClientWrapper singletonWrapper,
[NotNull] IDbContextOptions dbContextOptions,
[NotNull] IExecutionStrategyFactory executionStrategyFactory,
[NotNull] IDiagnosticsLogger<DbLoggerCategory.Database.Command> commandLogger)
Expand Down Expand Up @@ -205,8 +205,8 @@ public virtual async Task<bool> DeleteDatabaseOnceAsync(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual bool CreateContainerIfNotExists(
[NotNull] string containerId,
[NotNull] string partitionKey)
string containerId,
string partitionKey)
=> _executionStrategyFactory.Create().Execute(
(containerId, partitionKey), CreateContainerIfNotExistsOnce, null);

Expand All @@ -222,8 +222,8 @@ private bool CreateContainerIfNotExistsOnce(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Task<bool> CreateContainerIfNotExistsAsync(
[NotNull] string containerId,
[NotNull] string partitionKey,
string containerId,
string partitionKey,
CancellationToken cancellationToken = default)
=> _executionStrategyFactory.Create().ExecuteAsync(
(containerId, partitionKey), CreateContainerIfNotExistsOnceAsync, null, cancellationToken);
Expand Down Expand Up @@ -256,9 +256,9 @@ private async Task<bool> CreateContainerIfNotExistsOnceAsync(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual bool CreateItem(
[NotNull] string containerId,
[NotNull] JToken document,
[NotNull] IUpdateEntry entry)
string containerId,
JToken document,
IUpdateEntry entry)
=> _executionStrategyFactory.Create().Execute(
(containerId, document, entry), CreateItemOnce, null);

Expand All @@ -274,9 +274,9 @@ private bool CreateItemOnce(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Task<bool> CreateItemAsync(
[NotNull] string containerId,
[NotNull] JToken document,
[NotNull] IUpdateEntry updateEntry,
string containerId,
JToken document,
IUpdateEntry updateEntry,
CancellationToken cancellationToken = default)
=> _executionStrategyFactory.Create().ExecuteAsync(
(containerId, document, updateEntry), CreateItemOnceAsync, null, cancellationToken);
Expand Down Expand Up @@ -311,10 +311,10 @@ private async Task<bool> CreateItemOnceAsync(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual bool ReplaceItem(
[NotNull] string collectionId,
[NotNull] string documentId,
[NotNull] JObject document,
[NotNull] IUpdateEntry entry)
string collectionId,
string documentId,
JObject document,
IUpdateEntry entry)
=> _executionStrategyFactory.Create().Execute(
(collectionId, documentId, document, entry),
ReplaceItemOnce,
Expand All @@ -332,10 +332,10 @@ private bool ReplaceItemOnce(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Task<bool> ReplaceItemAsync(
[NotNull] string collectionId,
[NotNull] string documentId,
[NotNull] JObject document,
[NotNull] IUpdateEntry updateEntry,
string collectionId,
string documentId,
JObject document,
IUpdateEntry updateEntry,
CancellationToken cancellationToken = default)
=> _executionStrategyFactory.Create().ExecuteAsync(
(collectionId, documentId, document, updateEntry),
Expand Down Expand Up @@ -374,9 +374,9 @@ private async Task<bool> ReplaceItemOnceAsync(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual bool DeleteItem(
[NotNull] string containerId,
[NotNull] string documentId,
[NotNull] IUpdateEntry entry)
string containerId,
string documentId,
IUpdateEntry entry)
=> _executionStrategyFactory.Create().Execute(
(containerId, documentId, entry), DeleteItemOnce, null);

Expand All @@ -398,9 +398,9 @@ public virtual bool DeleteItemOnce(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual Task<bool> DeleteItemAsync(
[NotNull] string containerId,
[NotNull] string documentId,
[NotNull] IUpdateEntry entry,
string containerId,
string documentId,
IUpdateEntry entry,
CancellationToken cancellationToken = default)
=> _executionStrategyFactory.Create().ExecuteAsync(
(containerId, documentId, entry), DeleteItemOnceAsync, null, cancellationToken);
Expand Down Expand Up @@ -500,9 +500,9 @@ private static void ProcessResponse(ResponseMessage response, IUpdateEntry entry
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual IEnumerable<JObject> ExecuteSqlQuery(
[NotNull] string containerId,
[CanBeNull] string partitionKey,
[NotNull] CosmosSqlQuery query)
string containerId,
string partitionKey,
CosmosSqlQuery query)
{
_commandLogger.ExecutingSqlQuery(query);

Expand All @@ -516,9 +516,9 @@ public virtual IEnumerable<JObject> ExecuteSqlQuery(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual IAsyncEnumerable<JObject> ExecuteSqlQueryAsync(
[NotNull] string containerId,
[CanBeNull] string partitionKey,
[NotNull] CosmosSqlQuery query)
string containerId,
string partitionKey,
CosmosSqlQuery query)
{
_commandLogger.ExecutingSqlQuery(query);

Expand All @@ -532,9 +532,9 @@ public virtual IAsyncEnumerable<JObject> ExecuteSqlQueryAsync(
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual JObject ExecuteReadItem(
[NotNull] string containerId,
[CanBeNull] string partitionKey,
[NotNull] string resourceId)
string containerId,
string partitionKey,
string resourceId)
{
_commandLogger.ExecutingReadItem(partitionKey, resourceId);

Expand All @@ -550,10 +550,10 @@ public virtual JObject ExecuteReadItem(
/// 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>
internal virtual async Task<JObject> ExecuteReadItemAsync(
[NotNull] string containerId,
[CanBeNull] string partitionKey,
[NotNull] string resourceId,
public virtual async Task<JObject> ExecuteReadItemAsync(
string containerId,
string partitionKey,
string resourceId,
CancellationToken cancellationToken = default)
{
_commandLogger.ExecutingReadItem(partitionKey, resourceId);
Expand All @@ -578,7 +578,13 @@ private static JObject JObjectFromReadItemResponseMessage(ResponseMessage respon
return new JObject(new JProperty("c", jObject));
}

private FeedIterator CreateQuery(
/// <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 FeedIterator CreateQuery(
string containerId,
string partitionKey,
CosmosSqlQuery query)
Expand Down Expand Up @@ -656,13 +662,13 @@ private static bool TryReadJObject(JsonTextReader jsonReader, out JObject jObjec

private sealed class DocumentEnumerable : IEnumerable<JObject>
{
private readonly CosmosClientWrapper _cosmosClient;
private readonly ICosmosClientWrapper _cosmosClient;
private readonly string _containerId;
private readonly string _partitionKey;
private readonly CosmosSqlQuery _cosmosSqlQuery;

public DocumentEnumerable(
CosmosClientWrapper cosmosClient,
ICosmosClientWrapper cosmosClient,
string containerId,
string partitionKey,
CosmosSqlQuery cosmosSqlQuery)
Expand All @@ -681,7 +687,7 @@ IEnumerator IEnumerable.GetEnumerator()

private sealed class Enumerator : IEnumerator<JObject>
{
private readonly CosmosClientWrapper _cosmosClientWrapper;
private readonly ICosmosClientWrapper _cosmosClientWrapper;
private readonly string _containerId;
private readonly string _partitionKey;
private readonly CosmosSqlQuery _cosmosSqlQuery;
Expand Down Expand Up @@ -763,13 +769,13 @@ public void Reset()

private sealed class DocumentAsyncEnumerable : IAsyncEnumerable<JObject>
{
private readonly CosmosClientWrapper _cosmosClient;
private readonly ICosmosClientWrapper _cosmosClient;
private readonly string _containerId;
private readonly string _partitionKey;
private readonly CosmosSqlQuery _cosmosSqlQuery;

public DocumentAsyncEnumerable(
CosmosClientWrapper cosmosClient,
ICosmosClientWrapper cosmosClient,
string containerId,
string partitionKey,
CosmosSqlQuery cosmosSqlQuery)
Expand All @@ -785,7 +791,7 @@ public IAsyncEnumerator<JObject> GetAsyncEnumerator(CancellationToken cancellati

private sealed class AsyncEnumerator : IAsyncEnumerator<JObject>
{
private readonly CosmosClientWrapper _cosmosClientWrapper;
private readonly ICosmosClientWrapper _cosmosClientWrapper;
private readonly string _containerId;
private readonly string _partitionKey;
private readonly CosmosSqlQuery _cosmosSqlQuery;
Expand Down
4 changes: 2 additions & 2 deletions src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal
/// </summary>
public class CosmosDatabaseCreator : IDatabaseCreator
{
private readonly CosmosClientWrapper _cosmosClient;
private readonly ICosmosClientWrapper _cosmosClient;
private readonly IModel _model;
private readonly IUpdateAdapterFactory _updateAdapterFactory;
private readonly IDatabase _database;
Expand All @@ -31,7 +31,7 @@ public class CosmosDatabaseCreator : IDatabaseCreator
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public CosmosDatabaseCreator(
[NotNull] CosmosClientWrapper cosmosClient,
[NotNull] ICosmosClientWrapper cosmosClient,
[NotNull] IModel model,
[NotNull] IUpdateAdapterFactory updateAdapterFactory,
[NotNull] IDatabase database)
Expand Down
7 changes: 3 additions & 4 deletions src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal
/// </summary>
public class CosmosDatabaseWrapper : EntityFrameworkCore.Storage.Database
{
private readonly Dictionary<IEntityType, DocumentSource> _documentCollections
= new Dictionary<IEntityType, DocumentSource>();
private readonly Dictionary<IEntityType, DocumentSource> _documentCollections = new();

private readonly CosmosClientWrapper _cosmosClient;
private readonly ICosmosClientWrapper _cosmosClient;
private readonly bool _sensitiveLoggingEnabled;

/// <summary>
Expand All @@ -53,7 +52,7 @@ private readonly Dictionary<IEntityType, DocumentSource> _documentCollections
/// </summary>
public CosmosDatabaseWrapper(
[NotNull] DatabaseDependencies dependencies,
[NotNull] CosmosClientWrapper cosmosClient,
[NotNull] ICosmosClientWrapper cosmosClient,
[NotNull] ILoggingOptions loggingOptions)
: base(dependencies)
{
Expand Down
Loading