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

Convert CosmosModelCustomizer to a convention. #16169

Merged
merged 1 commit into from
Jun 20, 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
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public static IServiceCollection AddEntityFrameworkCosmos([NotNull] this IServic
.TryAdd<IDatabase, CosmosDatabaseWrapper>()
.TryAdd<IExecutionStrategyFactory, CosmosExecutionStrategyFactory>()
.TryAdd<IDbContextTransactionManager, CosmosTransactionManager>()
.TryAdd<IModelCustomizer, CosmosModelCustomizer>()
.TryAdd<IModelValidator, CosmosModelValidator>()
.TryAdd<IProviderConventionSetBuilder, CosmosConventionSetBuilder>()
.TryAdd<IDatabaseCreator, CosmosDatabaseCreator>()
Expand Down
41 changes: 0 additions & 41 deletions src/EFCore.Cosmos/Infrastructure/CosmosModelCustomizer.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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 JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;

namespace Microsoft.EntityFrameworkCore.Metadata.Conventions
{
/// <summary>
/// A convention that configures the default container name as the context type name.
/// </summary>
public class ContextContainerNameConvention : IModelInitializedConvention
{
/// <summary>
/// Creates a new instance of <see cref="ContextContainerNameConvention" />.
/// </summary>
/// <param name="dependencies"> Parameter object containing dependencies for this convention. </param>
public ContextContainerNameConvention([NotNull] ProviderConventionSetBuilderDependencies dependencies)
{
Dependencies = dependencies;
}

/// <summary>
/// Parameter object containing service dependencies.
/// </summary>
protected virtual ProviderConventionSetBuilderDependencies Dependencies { get; }

/// <summary>
/// Called after a model is initialized.
/// </summary>
/// <param name="modelBuilder"> The builder for the model. </param>
/// <param name="context"> Additional information associated with convention execution. </param>
public void ProcessModelInitialized(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
{
modelBuilder.ForCosmosHasDefaultContainerName(Dependencies.ContextType.Name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public override ConventionSet CreateConventionSet()
{
var conventionSet = base.CreateConventionSet();

conventionSet.ModelInitializedConventions.Add(new ContextContainerNameConvention(Dependencies));

var discriminatorConvention = new CosmosDiscriminatorConvention(Dependencies);
var storeKeyConvention = new StoreKeyConvention(Dependencies);
conventionSet.EntityTypeAddedConventions.Add(storeKeyConvention);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Linq;
using System.Net.Http;
using Microsoft.EntityFrameworkCore.TestUtilities.Xunit;

namespace Microsoft.EntityFrameworkCore.Cosmos.TestUtilities
Expand Down Expand Up @@ -36,9 +38,36 @@ private static bool TryConnect()

return true;
}
catch (Exception)
catch (AggregateException aggregate)
{
return false;
if (aggregate.Flatten().InnerExceptions.Any(e => IsNotConfigured(e)))
{
return false;
}

throw;
}
catch (Exception e)
{
if (IsNotConfigured(e))
{
return false;
}

throw;
}
}

private static bool IsNotConfigured(Exception firstException)
{
switch (firstException)
{
case HttpRequestException re:
return true;
case Exception e:
return e.Message.StartsWith("The input authorization token can't serve the request. Please check that the expected payload is built as per the protocol, and check the key being used.");
default:
return false;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ static TestEnvironment()

public static string DefaultConnection => Config["DefaultConnection"] ?? "https://localhost:8081";

public static string AuthToken => Config["AuthToken"];
public static string AuthToken => Config["AuthToken"] ?? "";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ public void Can_get_and_set_collection_name()
var entityType = modelBuilder
.Entity<Customer>();

Assert.Equal(nameof(Customer), entityType.Metadata.GetCosmosContainerName());
Assert.Equal(nameof(DbContext), entityType.Metadata.GetCosmosContainerName());

entityType.ForCosmosToContainer("Customizer");
Assert.Equal("Customizer", entityType.Metadata.GetCosmosContainerName());

entityType.ForCosmosToContainer(null);
Assert.Equal(nameof(Customer), entityType.Metadata.GetCosmosContainerName());
Assert.Equal(nameof(DbContext), entityType.Metadata.GetCosmosContainerName());

modelBuilder.ForCosmosHasDefaultContainerName("Unicorn");
Assert.Equal("Unicorn", entityType.Metadata.GetCosmosContainerName());
Expand Down Expand Up @@ -63,8 +63,9 @@ public void Default_container_name_is_used_if_not_set()
.Entity<Customer>();

var entityType = modelBuilder.Model.FindEntityType(typeof(Customer));
modelBuilder.ForCosmosHasDefaultContainerName(null);

Assert.Equal("Customer", entityType.GetCosmosContainerName());
Assert.Equal(nameof(Customer), entityType.GetCosmosContainerName());
Assert.Null(modelBuilder.Model.GetCosmosDefaultContainerName());

modelBuilder.ForCosmosHasDefaultContainerName("db0");
Expand Down