Skip to content

Commit

Permalink
Add missing design-time services
Browse files Browse the repository at this point in the history
  • Loading branch information
bricelam committed Mar 9, 2021
1 parent d40410d commit 67ec2b2
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 33 deletions.
35 changes: 25 additions & 10 deletions src/EFCore.Design/Design/DesignTimeServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@

using System;
using System.Diagnostics;
using System.Linq;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Migrations.Design;
using Microsoft.EntityFrameworkCore.Migrations.Internal;
Expand Down Expand Up @@ -46,41 +49,53 @@ public static IServiceCollection AddEntityFrameworkDesignTimeServices(
}

return services
.AddSingleton(reporter)
.AddSingleton<AnnotationCodeGeneratorDependencies>()
.AddSingleton<CSharpMigrationOperationGeneratorDependencies>()
.AddSingleton<CSharpMigrationsGeneratorDependencies>()
.AddSingleton<CSharpSnapshotGeneratorDependencies>()
.AddSingleton<MigrationsCodeGeneratorDependencies>()
.AddSingleton<ModelCodeGeneratorDependencies>()
.AddSingleton<ProviderCodeGeneratorDependencies>()
.AddSingleton<TypeMappingSourceDependencies>()
.AddSingleton<RelationalTypeMappingSourceDependencies>()
.AddSingleton<ValueConverterSelectorDependencies>()
.AddSingleton<DiagnosticSource>(new DiagnosticListener(DbLoggerCategory.Name))
.AddSingleton<IAnnotationCodeGenerator, AnnotationCodeGenerator>()
.AddSingleton<ICandidateNamingService, CandidateNamingService>()
.AddSingleton<IConstructorBindingFactory, ConstructorBindingFactory>()
.AddSingleton<ICSharpDbContextGenerator, CSharpDbContextGenerator>()
.AddSingleton<ICSharpEntityTypeGenerator, CSharpEntityTypeGenerator>()
.AddSingleton<ICSharpHelper, CSharpHelper>()
.AddSingleton<ICSharpMigrationOperationGenerator, CSharpMigrationOperationGenerator>()
.AddSingleton<ICSharpSnapshotGenerator, CSharpSnapshotGenerator>()
.AddSingleton<ICSharpUtilities, CSharpUtilities>()
.AddSingleton<IDbContextLogger, NullDbContextLogger>()
.AddSingleton(typeof(IDiagnosticsLogger<>), typeof(DiagnosticsLogger<>))
.AddSingleton<IInterceptors, Interceptors>()
.AddSingleton<DiagnosticSource>(new DiagnosticListener(DbLoggerCategory.Name))
.AddSingleton<ILoggingOptions, LoggingOptions>()
.AddSingleton<IMemberClassifier, MemberClassifier>()
.AddSingleton<IMigrationsCodeGenerator, CSharpMigrationsGenerator>()
.AddSingleton<IMigrationsCodeGeneratorSelector, MigrationsCodeGeneratorSelector>()
.AddSingleton<IModelCodeGenerator, CSharpModelGenerator>()
.AddSingleton<IModelCodeGeneratorSelector, ModelCodeGeneratorSelector>()
.AddSingleton<IAnnotationCodeGenerator, AnnotationCodeGenerator>()
.AddSingleton<IModelRuntimeInitializer, ModelRuntimeInitializer>()
.AddSingleton<IModelValidator, RelationalModelValidator>()
.AddSingleton<INamedConnectionStringResolver>(
new DesignTimeConnectionStringResolver(applicationServiceProviderAccessor))
.AddSingleton(reporter)
.AddSingleton<IParameterBindingFactories, ParameterBindingFactories>()
.AddSingleton<IPluralizer, HumanizerPluralizer>()
.AddSingleton<IPropertyParameterBindingFactory, PropertyParameterBindingFactory>()
.AddSingleton<IRegisteredServices>(new RegisteredServices(services.Select(s => s.ServiceType)))
.AddSingleton<IReverseEngineerScaffolder, ReverseEngineerScaffolder>()
.AddSingleton<IScaffoldingModelFactory, RelationalScaffoldingModelFactory>()
.AddSingleton<IScaffoldingTypeMapper, ScaffoldingTypeMapper>()
.AddSingleton<ITypeMappingSource>(p => p.GetService<IRelationalTypeMappingSource>())
.AddSingleton<IValueConverterSelector, ValueConverterSelector>()
.AddSingleton<IDbContextLogger, NullDbContextLogger>()
.AddSingleton<MigrationsCodeGeneratorDependencies>()
.AddSingleton<ModelCodeGeneratorDependencies>()
.AddSingleton<ModelRuntimeInitializerDependencies>()
.AddSingleton<ModelValidatorDependencies>()
.AddSingleton<ProviderCodeGeneratorDependencies>()
.AddSingleton<RelationalModelValidatorDependencies>()
.AddSingleton<RelationalTypeMappingSourceDependencies>()
.AddSingleton<RuntimeModelDependencies>()
.AddSingleton<TypeMappingSourceDependencies>()
.AddSingleton<ValueConverterSelectorDependencies>()
.AddTransient<MigrationsScaffolderDependencies>()
.AddTransient<IMigrationsScaffolder, MigrationsScaffolder>()
.AddTransient<ISnapshotModelProcessor, SnapshotModelProcessor>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@

using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.SqlServer.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Scaffolding.Internal;
using Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

[assembly: DesignTimeProviderServices("Microsoft.EntityFrameworkCore.SqlServer.Design.Internal.SqlServerDesignTimeServices")]

namespace Microsoft.EntityFrameworkCore.SqlServer.Design.Internal
{
/// <summary>
Expand All @@ -30,10 +36,11 @@ public class SqlServerDesignTimeServices : IDesignTimeServices
/// </summary>
public virtual void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
=> serviceCollection
.AddSingleton<LoggingDefinitions, SqlServerLoggingDefinitions>()
.AddSingleton<IRelationalTypeMappingSource, SqlServerTypeMappingSource>()
.AddSingleton<IAnnotationCodeGenerator, SqlServerAnnotationCodeGenerator>()
.AddSingleton<IDatabaseModelFactory, SqlServerDatabaseModelFactory>()
.AddSingleton<IModelValidator, SqlServerModelValidator>()
.AddSingleton<IProviderConfigurationCodeGenerator, SqlServerCodeGenerator>()
.AddSingleton<IAnnotationCodeGenerator, SqlServerAnnotationCodeGenerator>();
.AddSingleton<IRelationalTypeMappingSource, SqlServerTypeMappingSource>()
.AddSingleton<LoggingDefinitions, SqlServerLoggingDefinitions>();
}
}
8 changes: 0 additions & 8 deletions src/EFCore.SqlServer/Properties/AssemblyInfo.cs

This file was deleted.

13 changes: 10 additions & 3 deletions src/EFCore.Sqlite.Core/Design/Internal/SqliteDesignTimeServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,21 @@

using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.Sqlite.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Sqlite.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Sqlite.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Sqlite.Scaffolding.Internal;
using Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;

#nullable enable

[assembly: DesignTimeProviderServices("Microsoft.EntityFrameworkCore.Sqlite.Design.Internal.SqliteDesignTimeServices")]

namespace Microsoft.EntityFrameworkCore.Sqlite.Design.Internal
{
/// <summary>
Expand All @@ -30,10 +36,11 @@ public class SqliteDesignTimeServices : IDesignTimeServices
/// </summary>
public virtual void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
=> serviceCollection
.AddSingleton<LoggingDefinitions, SqliteLoggingDefinitions>()
.AddSingleton<IRelationalTypeMappingSource, SqliteTypeMappingSource>()
.AddSingleton<IAnnotationCodeGenerator, AnnotationCodeGenerator>()
.AddSingleton<IDatabaseModelFactory, SqliteDatabaseModelFactory>()
.AddSingleton<IModelValidator, SqliteModelValidator>()
.AddSingleton<IProviderConfigurationCodeGenerator, SqliteCodeGenerator>()
.AddSingleton<IAnnotationCodeGenerator, AnnotationCodeGenerator>();
.AddSingleton<IRelationalTypeMappingSource, SqliteTypeMappingSource>()
.AddSingleton<LoggingDefinitions, SqliteLoggingDefinitions>();
}
}
8 changes: 0 additions & 8 deletions src/EFCore.Sqlite.Core/Properties/AssemblyInfo.cs

This file was deleted.

68 changes: 68 additions & 0 deletions test/EFCore.Relational.Specification.Tests/DesignTimeTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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 System.Reflection;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Migrations.Design;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

namespace Microsoft.EntityFrameworkCore
{
public abstract class DesignTimeTestBase<TFixture> : IClassFixture<TFixture>
where TFixture : DesignTimeTestBase<TFixture>.DesignTimeFixtureBase
{
protected TFixture Fixture { get; }

protected DesignTimeTestBase(TFixture fixture)
=> Fixture = fixture;

protected abstract Assembly ProviderAssembly { get; }

[ConditionalFact]
public void Can_get_reverse_engineering_services()
{
using var context = Fixture.CreateContext();
var serviceCollection = new ServiceCollection()
.AddEntityFrameworkDesignTimeServices();
((IDesignTimeServices)Activator.CreateInstance(
ProviderAssembly.GetType(
ProviderAssembly.GetCustomAttribute<DesignTimeProviderServicesAttribute>().TypeName,
throwOnError: true))!)
.ConfigureDesignTimeServices(serviceCollection);
using var services = serviceCollection.BuildServiceProvider();

var reverseEngineerScaffolder = services.GetService<IReverseEngineerScaffolder>();

Assert.NotNull(reverseEngineerScaffolder);
}

[ConditionalFact]
public void Can_get_migrations_services()
{
using var context = Fixture.CreateContext();
var serviceCollection = new ServiceCollection()
.AddEntityFrameworkDesignTimeServices()
.AddDbContextDesignTimeServices(context);
((IDesignTimeServices)Activator.CreateInstance(
ProviderAssembly.GetType(
ProviderAssembly.GetCustomAttribute<DesignTimeProviderServicesAttribute>().TypeName,
throwOnError: true))!)
.ConfigureDesignTimeServices(serviceCollection);
using var services = serviceCollection.BuildServiceProvider();

var migrationsScaffolder = services.GetService<IMigrationsScaffolder>();

Assert.NotNull(migrationsScaffolder);
}

public abstract class DesignTimeFixtureBase : SharedStoreFixtureBase<PoolableDbContext>
{
protected override string StoreName
=> "DesignTimeTest";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\EFCore.Design\EFCore.Design.csproj" />
<ProjectReference Include="..\..\src\EFCore.Relational\EFCore.Relational.csproj" />
<ProjectReference Include="..\EFCore.Specification.Tests\EFCore.Specification.Tests.csproj" />
</ItemGroup>
Expand Down
26 changes: 26 additions & 0 deletions test/EFCore.SqlServer.FunctionalTests/DesignTimeSqlServerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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.Reflection;
using Microsoft.EntityFrameworkCore.SqlServer.Design.Internal;
using Microsoft.EntityFrameworkCore.TestUtilities;

namespace Microsoft.EntityFrameworkCore
{
public class DesignTimeSqlServerTest : DesignTimeTestBase<DesignTimeSqlServerTest.DesignTimeSqlServerFixture>
{
public DesignTimeSqlServerTest(DesignTimeSqlServerFixture fixture)
: base(fixture)
{
}

protected override Assembly ProviderAssembly
=> typeof(SqlServerDesignTimeServices).Assembly;

public class DesignTimeSqlServerFixture : DesignTimeFixtureBase
{
protected override ITestStoreFactory TestStoreFactory
=> SqlServerTestStoreFactory.Instance;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\EFCore.Design\EFCore.Design.csproj" />
<ProjectReference Include="..\..\src\EFCore.SqlServer.NTS\EFCore.SqlServer.NTS.csproj" />
<ProjectReference Include="..\EFCore.Relational.Specification.Tests\EFCore.Relational.Specification.Tests.csproj" />
</ItemGroup>
Expand Down
26 changes: 26 additions & 0 deletions test/EFCore.Sqlite.FunctionalTests/DesignTimeSqliteTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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.Reflection;
using Microsoft.EntityFrameworkCore.Sqlite.Design.Internal;
using Microsoft.EntityFrameworkCore.TestUtilities;

namespace Microsoft.EntityFrameworkCore
{
public class DesignTimeSqliteTest : DesignTimeTestBase<DesignTimeSqliteTest.DesignTimeSqliteFixture>
{
public DesignTimeSqliteTest(DesignTimeSqliteFixture fixture)
: base(fixture)
{
}

protected override Assembly ProviderAssembly
=> typeof(SqliteDesignTimeServices).Assembly;

public class DesignTimeSqliteFixture : DesignTimeFixtureBase
{
protected override ITestStoreFactory TestStoreFactory
=> SqliteTestStoreFactory.Instance;
}
}
}

0 comments on commit 67ec2b2

Please sign in to comment.