Skip to content

Commit

Permalink
Add public API to build provider-specific convention sets without usi…
Browse files Browse the repository at this point in the history
…ng DI

Fixes dotnet#3529
  • Loading branch information
AndriySvyryd committed Dec 9, 2015
1 parent 5ae361b commit 3445477
Show file tree
Hide file tree
Showing 33 changed files with 141 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<Compile Include="Infrastructure\Internal\SqlServerModelSource.cs" />
<Compile Include="Internal\SqlServerModelValidator.cs" />
<Compile Include="Infrastructure\Internal\SqlServerOptionsExtension.cs" />
<Compile Include="Metadata\Conventions\Internal\SqlServerConventionSetBuilder.cs" />
<Compile Include="Metadata\Conventions\SqlServerConventionSetBuilder.cs" />
<Compile Include="Metadata\Conventions\Internal\SqlServerValueGenerationStrategyConvention.cs" />
<Compile Include="Metadata\Internal\SqlServerAnnotationNames.cs" />
<Compile Include="Metadata\Internal\SqlServerIndexBuilderAnnotations.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.Data.Entity.Infrastructure.Internal;
using Microsoft.Data.Entity.Internal;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Metadata.Conventions.Internal;
using Microsoft.Data.Entity.Metadata.Conventions;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Migrations.Internal;
using Microsoft.Data.Entity.Query.ExpressionTranslators.Internal;
Expand Down Expand Up @@ -50,7 +50,7 @@ public static class SqlServerEntityFrameworkServicesBuilderExtensions
/// }
/// </code>
/// </example>
/// <param name="serviceCollection"> The <see cref="IServiceCollection" /> to add services to. </param>
/// <param name="builder"> The <see cref="IServiceCollection" /> to add services to. </param>
/// <returns>
/// A builder that allows further Entity Framework specific setup of the <see cref="IServiceCollection" />.
/// </returns>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using JetBrains.Annotations;
using Microsoft.Data.Entity.Metadata.Conventions.Internal;
using Microsoft.Data.Entity.Storage;
using Microsoft.Data.Entity.Storage.Internal;
using Microsoft.Data.Entity.Utilities;

namespace Microsoft.Data.Entity.Metadata.Conventions.Internal
namespace Microsoft.Data.Entity.Metadata.Conventions
{
public class SqlServerConventionSetBuilder : RelationalConventionSetBuilder
{
Expand All @@ -24,5 +26,9 @@ public override ConventionSet AddConventions(ConventionSet conventionSet)

return conventionSet;
}

public static ConventionSet Build()
=> new SqlServerConventionSetBuilder(new SqlServerTypeMapper())
.AddConventions(new CoreConventionSetBuilder().CreateConventionSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Data.Entity.Infrastructure.Internal;
using Microsoft.Data.Entity.Internal;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Metadata.Conventions;
using Microsoft.Data.Entity.Metadata.Conventions.Internal;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Migrations.Internal;
Expand Down
2 changes: 1 addition & 1 deletion src/EntityFramework.Sqlite/EntityFramework.Sqlite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
<Compile Include="Query\ExpressionTranslators\Internal\SqliteCompositeMemberTranslator.cs" />
<Compile Include="Storage\Internal\SqliteDatabaseCreator.cs" />
<Compile Include="Storage\Internal\SqliteRelationalConnection.cs" />
<Compile Include="Metadata\Conventions\Internal\SqliteConventionSetBuilder.cs" />
<Compile Include="Metadata\Conventions\SqliteConventionSetBuilder.cs" />
<Compile Include="Infrastructure\SqliteDbContextOptionsBuilder.cs" />
<Compile Include="SqliteDbContextOptionsBuilderExtensions.cs" />
<Compile Include="Infrastructure\SqliteEntityFrameworkServicesBuilderExtensions.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Data.Entity.Infrastructure.Internal;
using Microsoft.Data.Entity.Internal;
using Microsoft.Data.Entity.Metadata.Conventions.Internal;
using Microsoft.Data.Entity.Metadata.Conventions;
using Microsoft.Data.Entity.Metadata.Internal;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Migrations.Internal;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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.Data.Entity.Metadata.Conventions.Internal;
using Microsoft.Data.Entity.Storage;
using Microsoft.Data.Entity.Storage.Internal;

namespace Microsoft.Data.Entity.Metadata.Conventions
{
public class SqliteConventionSetBuilder : RelationalConventionSetBuilder
{
public SqliteConventionSetBuilder([NotNull] IRelationalTypeMapper typeMapper)
: base(typeMapper)
{
}

public static ConventionSet Build()
=> new SqliteConventionSetBuilder(new SqliteTypeMapper())
.AddConventions(new CoreConventionSetBuilder().CreateConventionSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Data.Entity.Infrastructure.Internal;
using Microsoft.Data.Entity.Internal;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Metadata.Conventions;
using Microsoft.Data.Entity.Metadata.Conventions.Internal;
using Microsoft.Data.Entity.Metadata.Internal;
using Microsoft.Data.Entity.Migrations;
Expand Down
29 changes: 15 additions & 14 deletions test/EntityFramework.Core.Tests/EntityFramework.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="Infrastructure\LoggingModelValidatorTest.cs" />
<Compile Include="Infrastructure\NonThrowingModelValidatorTest.cs" />
<Compile Include="Infrastructure\ThrowingModelValidatorTest.cs" />
<Compile Include="Metadata\Conventions\ConventionSetBuilderTests.cs" />
<Compile Include="Metadata\Internal\ClrPropertySetterFactoryTest.cs" />
<Compile Include="Metadata\Internal\CollectionTypeFactoryTest.cs" />
<Compile Include="Metadata\EntityTypeExtensionsTest.cs" />
Expand All @@ -83,18 +84,18 @@
<Compile Include="Metadata\Internal\InternalPropertyBuilderTest.cs" />
<Compile Include="Metadata\Internal\InternalRelationshipBuilderTest.cs" />
<Compile Include="Metadata\MetadataBuilderTest.cs" />
<Compile Include="Metadata\ModelConventions\BaseTypeDiscoveryConventionTest.cs" />
<Compile Include="Metadata\ModelConventions\CascadeDeleteConventionTest.cs" />
<Compile Include="Metadata\ModelConventions\ConventionDispatcherTest.cs" />
<Compile Include="Metadata\ModelConventions\DerivedTypeDiscoveryConventionTest.cs" />
<Compile Include="Metadata\ModelConventions\ForeignKeyPropertyDiscoveryConventionTest.cs" />
<Compile Include="Metadata\ModelConventions\KeyConventionTest.cs" />
<Compile Include="Metadata\ModelConventions\EntityTypeAttributeConventionTest.cs" />
<Compile Include="Metadata\ModelConventions\ModelCleanupConventionTest.cs" />
<Compile Include="Metadata\ModelConventions\NavigationAttributeConventionTest.cs" />
<Compile Include="Metadata\ModelConventions\PropertyAttributeConventionTest.cs" />
<Compile Include="Metadata\ModelConventions\PropertyMappingValidationConventionTest.cs" />
<Compile Include="Metadata\ModelConventions\RelationshipDiscoveryConventionTest.cs" />
<Compile Include="Metadata\Conventions\Internal\BaseTypeDiscoveryConventionTest.cs" />
<Compile Include="Metadata\Conventions\Internal\CascadeDeleteConventionTest.cs" />
<Compile Include="Metadata\Conventions\Internal\ConventionDispatcherTest.cs" />
<Compile Include="Metadata\Conventions\Internal\DerivedTypeDiscoveryConventionTest.cs" />
<Compile Include="Metadata\Conventions\Internal\ForeignKeyPropertyDiscoveryConventionTest.cs" />
<Compile Include="Metadata\Conventions\Internal\KeyConventionTest.cs" />
<Compile Include="Metadata\Conventions\Internal\EntityTypeAttributeConventionTest.cs" />
<Compile Include="Metadata\Conventions\Internal\ModelCleanupConventionTest.cs" />
<Compile Include="Metadata\Conventions\Internal\NavigationAttributeConventionTest.cs" />
<Compile Include="Metadata\Conventions\Internal\PropertyAttributeConventionTest.cs" />
<Compile Include="Metadata\Conventions\Internal\PropertyMappingValidationConventionTest.cs" />
<Compile Include="Metadata\Conventions\Internal\RelationshipDiscoveryConventionTest.cs" />
<Compile Include="Metadata\NavigationExtensionsTest.cs" />
<Compile Include="Extensions\QueryableExtensionsTest.cs" />
<Compile Include="Infrastructure\TypedAnnotationTest.cs" />
Expand Down Expand Up @@ -129,8 +130,8 @@
<Compile Include="Metadata\Internal\ModelTest.cs" />
<Compile Include="Metadata\Internal\NavigationTest.cs" />
<Compile Include="Metadata\Internal\PropertyTest.cs" />
<Compile Include="Metadata\ModelConventions\KeyDiscoveryConventionTest.cs" />
<Compile Include="Metadata\ModelConventions\PropertyDiscoveryConventionTest.cs" />
<Compile Include="Metadata\Conventions\Internal\KeyDiscoveryConventionTest.cs" />
<Compile Include="Metadata\Conventions\Internal\PropertyDiscoveryConventionTest.cs" />
<Compile Include="Query\TaskResultAsyncEnumerableTest.cs" />
<Compile Include="Storage\DatabaseProviderSelectorTest.cs" />
<Compile Include="TestUtilities\ListLogger.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using Microsoft.Data.Entity.Metadata.Conventions.Internal;
using Xunit;

namespace Microsoft.Data.Entity.Metadata.Conventions.Tests
{
public class ConventionSetBuilderTests
{
[Fact]
public virtual IModel Can_build_a_model_with_default_conventions_without_DI()
{
var modelBuilder = new ModelBuilder(GetConventionSet());
modelBuilder.Entity<Product>();

var model = modelBuilder.Model;
Assert.Equal(2, model.GetEntityTypes().Single().GetProperties().Count());
return model;
}

protected virtual ConventionSet GetConventionSet() => new CoreConventionSetBuilder().CreateConventionSet();

[Table("ProductTable")]
protected class Product
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
}
}
10 changes: 4 additions & 6 deletions test/EntityFramework.Microbenchmarks/InitializationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// 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;
Expand All @@ -8,6 +8,7 @@
using EntityFramework.Microbenchmarks.Core.Models.AdventureWorks.TestHelpers;
using EntityFramework.Microbenchmarks.Models.AdventureWorks;
using Microsoft.Data.Entity;
using Microsoft.Data.Entity.Metadata.Conventions;
using Microsoft.Data.Entity.Metadata.Conventions.Internal;
using Microsoft.Data.Entity.Storage.Internal;
using Xunit;
Expand Down Expand Up @@ -52,11 +53,8 @@ public void InitializeAndSaveChanges_AdventureWorks(IMetricCollector collector,
public void BuildModel_AdventureWorks(IMetricCollector collector)
{
collector.StartCollection();

var conventions = new SqlServerConventionSetBuilder(new SqlServerTypeMapper())
.AddConventions(new CoreConventionSetBuilder().CreateConventionSet());

var builder = new ModelBuilder(conventions);

var builder = new ModelBuilder(SqlServerConventionSetBuilder.Build());
AdventureWorksContext.ConfigureModel(builder);

var model = builder.Model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ApiConsistencyTest.cs" />
<Compile Include="Metadata\Conventions\SqlServerConventionSetBuilderTests.cs" />
<Compile Include="Metadata\InternalSqlServerMetadataBuilderExtensionsTest.cs" />
<Compile Include="Metadata\ModelConventions\SqlServerValueGenerationStrategyConventionTest.cs" />
<Compile Include="Metadata\Conventions\SqlServerValueGenerationStrategyConventionTest.cs" />
<Compile Include="Metadata\SqlServerBuilderExtensionsTest.cs" />
<Compile Include="Metadata\SqlServerMetadataExtensionsTest.cs" />
<Compile Include="Migrations\SqlServerHistoryRepositoryTest.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.Linq;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Metadata.Conventions;
using Microsoft.Data.Entity.Metadata.Conventions.Tests;
using Xunit;

namespace Microsoft.Data.Entity.SqlServer.Metadata.Conventions.Tests
{
public class SqlServerConventionSetBuilderTests : ConventionSetBuilderTests
{
public override IModel Can_build_a_model_with_default_conventions_without_DI()
{
var model = base.Can_build_a_model_with_default_conventions_without_DI();

Assert.Equal("ProductTable", model.GetEntityTypes().Single().SqlServer().TableName);

return model;
}

protected override ConventionSet GetConventionSet() => SqlServerConventionSetBuilder.Build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Linq;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Metadata.Internal;
using Microsoft.Data.Entity.Tests;
using Xunit;

namespace Microsoft.Data.Entity.SqlServer.Tests.Metadata.Conventions
namespace Microsoft.Data.Entity.Metadata.Conventions.Tests
{
public class SqlServerValueGenerationStrategyConventionTest
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.Data.Entity.Infrastructure.Internal;
using Microsoft.Data.Entity.Internal;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Metadata.Conventions.Internal;
using Microsoft.Data.Entity.Metadata.Conventions;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Migrations.Internal;
using Microsoft.Data.Entity.Query.ExpressionTranslators.Internal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ApiConsistencyTest.cs" />
<Compile Include="Metadata\Conventions\SqliteConventionSetBuilderTests.cs" />
<Compile Include="SqliteDbContextOptionsBuilderExtensionsTest.cs" />
<Compile Include="Metadata\Builders\SqliteBuilderExtensionsTest.cs" />
<Compile Include="Metadata\Internal\SqliteInternalMetadataBuilderExtensionsTest.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using Microsoft.Data.Entity.Infrastructure.Internal;
using Microsoft.Data.Entity.Internal;
using Microsoft.Data.Entity.Metadata.Conventions.Internal;
using Microsoft.Data.Entity.Metadata.Conventions;
using Microsoft.Data.Entity.Metadata.Internal;
using Microsoft.Data.Entity.Migrations;
using Microsoft.Data.Entity.Migrations.Internal;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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.Linq;
using Xunit;

namespace Microsoft.Data.Entity.Metadata.Conventions.Tests
{
public class SqliteConventionSetBuilderTests : ConventionSetBuilderTests
{
public override IModel Can_build_a_model_with_default_conventions_without_DI()
{
var model = base.Can_build_a_model_with_default_conventions_without_DI();

Assert.Equal("ProductTable", model.GetEntityTypes().Single().Sqlite().TableName);

return model;
}

protected override ConventionSet GetConventionSet() => SqliteConventionSetBuilder.Build();
}
}

0 comments on commit 3445477

Please sign in to comment.