Skip to content

Commit

Permalink
Make conventions public
Browse files Browse the repository at this point in the history
Use a parameter object for all convention constructors and IModelSource.GetModel()
Move ValidationConvention to ProviderConventionSetBuilder

Part of #214
Part of #15662
  • Loading branch information
AndriySvyryd committed Jun 1, 2019
1 parent b260ecc commit ba7c54a
Show file tree
Hide file tree
Showing 138 changed files with 1,203 additions and 1,246 deletions.
11 changes: 3 additions & 8 deletions src/EFCore.Cosmos/Infrastructure/CosmosModelCustomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
namespace Microsoft.EntityFrameworkCore.Cosmos.Infrastructure
{
/// <summary>
/// <para>
/// Builds the model for a given context. This default implementation builds the model by calling
/// <see cref="DbContext.OnConfiguring(DbContextOptionsBuilder)" /> on the context.
/// </para>
/// <para>
/// This type is typically used by database providers (and other extensions). It is generally
/// not used in application code.
/// </para>
/// Builds the model for a given context. This implementation builds the model by calling
/// <see cref="DbContext.OnConfiguring(DbContextOptionsBuilder)" /> on the context and
/// using the context type name as the default container name.
/// </summary>
public class CosmosModelCustomizer : ModelCustomizer
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;

namespace Microsoft.EntityFrameworkCore.Cosmos.Metadata.Conventions.Internal
namespace Microsoft.EntityFrameworkCore.Cosmos.Metadata.Conventions
{
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
Expand All @@ -25,8 +24,8 @@ public class CosmosDiscriminatorConvention : DiscriminatorConvention, IEntityTyp
/// 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 CosmosDiscriminatorConvention([NotNull] IDiagnosticsLogger<DbLoggerCategory.Model> logger)
: base(logger)
public CosmosDiscriminatorConvention([NotNull] ProviderConventionSetBuilderDependencies dependencies)
: base(dependencies)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;

namespace Microsoft.EntityFrameworkCore.Cosmos.Metadata.Conventions.Internal
{
Expand All @@ -20,8 +19,8 @@ public override ConventionSet CreateConventionSet()
{
var conventionSet = base.CreateConventionSet();

var discriminatorConvention = new CosmosDiscriminatorConvention(Dependencies.Logger);
var storeKeyConvention = new StoreKeyConvention();
var discriminatorConvention = new CosmosDiscriminatorConvention(Dependencies);
var storeKeyConvention = new StoreKeyConvention(Dependencies);
conventionSet.EntityTypeAddedConventions.Add(storeKeyConvention);
conventionSet.EntityTypeAddedConventions.Add(discriminatorConvention);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// 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.Cosmos.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.ValueGeneration.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Newtonsoft.Json.Linq;

namespace Microsoft.EntityFrameworkCore.Cosmos.Metadata.Conventions.Internal
namespace Microsoft.EntityFrameworkCore.Cosmos.Metadata.Conventions
{
public class StoreKeyConvention :
IEntityTypeAddedConvention,
Expand All @@ -19,6 +21,16 @@ public class StoreKeyConvention :
public static readonly string IdPropertyName = "id";
public static readonly string JObjectPropertyName = "__jObject";

public StoreKeyConvention([NotNull] ProviderConventionSetBuilderDependencies dependencies)
{
Dependencies = dependencies;
}

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

private static void Process(IConventionEntityTypeBuilder entityTypeBuilder)
{
var entityType = entityTypeBuilder.Metadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata;
using Newtonsoft.Json.Linq;
using Remotion.Linq.Parsing.ExpressionVisitors;
Expand Down
3 changes: 1 addition & 2 deletions src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Update.Internal;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Update;
using Newtonsoft.Json.Linq;
Expand Down
2 changes: 1 addition & 1 deletion src/EFCore.Cosmos/Update/Internal/DocumentSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Collections;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Cosmos.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Microsoft.EntityFrameworkCore.Utilities;
Expand Down Expand Up @@ -761,9 +761,9 @@ protected virtual void GenerateEntityTypeAnnotations(

IgnoreAnnotations(
annotations,
RelationshipDiscoveryConvention.NavigationCandidatesAnnotationName,
RelationshipDiscoveryConvention.AmbiguousNavigationsAnnotationName,
InversePropertyAttributeConvention.InverseNavigationsAnnotationName,
CoreAnnotationNames.NavigationCandidates,
CoreAnnotationNames.AmbiguousNavigations,
CoreAnnotationNames.InverseNavigations,
CoreAnnotationNames.NavigationAccessMode,
CoreAnnotationNames.PropertyAccessMode,
CoreAnnotationNames.ChangeTrackingStrategy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Migrations.Operations;
using Microsoft.EntityFrameworkCore.Utilities;
Expand Down Expand Up @@ -219,9 +219,9 @@ private static IEnumerable<string> GetAnnotationNamespaces(IEnumerable<IAnnotata
{
var ignoredAnnotations = new List<string>
{
RelationshipDiscoveryConvention.NavigationCandidatesAnnotationName,
RelationshipDiscoveryConvention.AmbiguousNavigationsAnnotationName,
InversePropertyAttributeConvention.InverseNavigationsAnnotationName,
CoreAnnotationNames.NavigationCandidates,
CoreAnnotationNames.AmbiguousNavigations,
CoreAnnotationNames.InverseNavigations,
ChangeDetector.SkipDetectChangesAnnotation,
CoreAnnotationNames.OwnedTypes,
CoreAnnotationNames.ChangeTrackingStrategy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;
Expand Down Expand Up @@ -67,7 +68,13 @@ public CSharpDbContextGenerator(
/// 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 string WriteCode(IModel model, string @namespace, string contextName, string connectionString, bool useDataAnnotations, bool suppressConnectionStringWarning)
public virtual string WriteCode(
IModel model,
string @namespace,
string contextName,
string connectionString,
bool useDataAnnotations,
bool suppressConnectionStringWarning)
{
Check.NotNull(model, nameof(model));

Expand Down Expand Up @@ -683,16 +690,10 @@ private void GenerateProperty(IProperty property, bool useDataAnnotations)
$"({_code.Literal(property.GetComputedColumnSql())})");
}

var dummyLogger = new DiagnosticsLogger<DbLoggerCategory.Model>(
new ScopedLoggerFactory(new LoggerFactory(), dispose: true),
new LoggingOptions(),
new DiagnosticListener(""),
_loggingDefinitions);

var valueGenerated = property.ValueGenerated;
var isRowVersion = false;
if (((IConventionProperty)property).GetValueGeneratedConfigurationSource().HasValue
&& new RelationalValueGeneratorConvention(dummyLogger).GetValueGenerated((IConventionProperty)property) != valueGenerated)
&& RelationalValueGeneratorConvention.GetValueGenerated(property) != valueGenerated)
{
string methodName;
switch (valueGenerated)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using JetBrains.Annotations;
Expand All @@ -20,7 +19,6 @@
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;
using Microsoft.Extensions.Logging;

namespace Microsoft.EntityFrameworkCore.Scaffolding.Internal
{
Expand Down Expand Up @@ -516,13 +514,7 @@ protected virtual KeyBuilder VisitPrimaryKey([NotNull] EntityTypeBuilder builder
var property = builder.Metadata.FindProperty(GetPropertyName(primaryKey.Columns[0]))?.AsProperty();
if (property != null)
{
var dummyLogger = new DiagnosticsLogger<DbLoggerCategory.Model>(
new ScopedLoggerFactory(new LoggerFactory(), dispose: true),
new LoggingOptions(),
new DiagnosticListener(""),
_loggingDefinitions);

var conventionalValueGenerated = new RelationalValueGeneratorConvention(dummyLogger).GetValueGenerated(property);
var conventionalValueGenerated = RelationalValueGeneratorConvention.GetValueGenerated(property);
if (conventionalValueGenerated == ValueGenerated.OnAdd)
{
property.ValueGenerated = ValueGenerated.Never;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
Expand All @@ -29,9 +28,8 @@ namespace Microsoft.EntityFrameworkCore.Proxies.Internal
public class ProxiesConventionSetCustomizer : IConventionSetCustomizer
{
private readonly IDbContextOptions _options;
private readonly IConstructorBindingFactory _constructorBindingFactory;
private readonly IProxyFactory _proxyFactory;
private readonly IDiagnosticsLogger<DbLoggerCategory.Model> _logger;
private readonly ProviderConventionSetBuilderDependencies _conventionSetBuilderDependencies;
private readonly LazyLoaderParameterBindingFactoryDependencies _lazyLoaderParameterBindingFactoryDependencies;

/// <summary>
Expand All @@ -41,17 +39,15 @@ public class ProxiesConventionSetCustomizer : IConventionSetCustomizer
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public ProxiesConventionSetCustomizer(
[NotNull] IDbContextOptions options,
[NotNull] IConstructorBindingFactory constructorBindingFactory,
[NotNull] IProxyFactory proxyFactory,
[NotNull] IDiagnosticsLogger<DbLoggerCategory.Model> logger,
[NotNull] LazyLoaderParameterBindingFactoryDependencies lazyLoaderParameterBindingFactoryDependencies)
[NotNull] IDbContextOptions options,
[NotNull] LazyLoaderParameterBindingFactoryDependencies lazyLoaderParameterBindingFactoryDependencies,
[NotNull] ProviderConventionSetBuilderDependencies conventionSetBuilderDependencies)
{
_options = options;
_constructorBindingFactory = constructorBindingFactory;
_proxyFactory = proxyFactory;
_logger = logger;
_options = options;
_lazyLoaderParameterBindingFactoryDependencies = lazyLoaderParameterBindingFactoryDependencies;
_conventionSetBuilderDependencies = conventionSetBuilderDependencies;
}

/// <summary>
Expand All @@ -62,13 +58,14 @@ public ProxiesConventionSetCustomizer(
/// </summary>
public virtual ConventionSet ModifyConventions(ConventionSet conventionSet)
{
conventionSet.ModelFinalizedConventions.Add(
ConventionSet.AddBefore(
conventionSet.ModelFinalizedConventions,
new ProxyBindingRewriter(
_lazyLoaderParameterBindingFactoryDependencies,
_proxyFactory,
_constructorBindingFactory,
_logger,
_options.FindExtension<ProxiesOptionsExtension>()));
_options.FindExtension<ProxiesOptionsExtension>(),
_lazyLoaderParameterBindingFactoryDependencies,
_conventionSetBuilderDependencies),
typeof(ValidatingConvention));

return conventionSet;
}
Expand Down
14 changes: 6 additions & 8 deletions src/EFCore.Proxies/Proxies/Internal/ProxyBindingRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Microsoft.EntityFrameworkCore.Proxies.Internal
Expand Down Expand Up @@ -43,16 +42,15 @@ private static readonly PropertyInfo _lazyLoaderProperty
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public ProxyBindingRewriter(
[NotNull] LazyLoaderParameterBindingFactoryDependencies lazyLoaderParameterBindingFactoryDependencies,
[NotNull] IProxyFactory proxyFactory,
[NotNull] IConstructorBindingFactory bindingFactory,
[NotNull] IDiagnosticsLogger<DbLoggerCategory.Model> logger,
[CanBeNull] ProxiesOptionsExtension options)
[CanBeNull] ProxiesOptionsExtension options,
[NotNull] LazyLoaderParameterBindingFactoryDependencies lazyLoaderParameterBindingFactoryDependencies,
[NotNull] ProviderConventionSetBuilderDependencies conventionSetBuilderDependencies)
{
_directBindingConvention = new ConstructorBindingConvention(bindingFactory, logger);
_lazyLoaderParameterBindingFactoryDependencies = lazyLoaderParameterBindingFactoryDependencies;
_proxyFactory = proxyFactory;
_options = options;
_lazyLoaderParameterBindingFactoryDependencies = lazyLoaderParameterBindingFactoryDependencies;
_directBindingConvention = new ConstructorBindingConvention(conventionSetBuilderDependencies);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// 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;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// 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;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

// ReSharper disable once CheckNamespace
Expand Down
Loading

0 comments on commit ba7c54a

Please sign in to comment.