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

RevEng: Remove Razor dependency #3393

Closed
wants to merge 1 commit into from
Closed
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 @@ -5,8 +5,6 @@
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Relational.Design.ReverseEngineering;
using Microsoft.Data.Entity.Relational.Design.ReverseEngineering.Internal;
using Microsoft.Data.Entity.Relational.Design.ReverseEngineering.Internal.Templating;
using Microsoft.Data.Entity.Relational.Design.ReverseEngineering.Internal.Templating.Compilation;
using Microsoft.Data.Entity.SqlServer.Design.ReverseEngineering;
using Microsoft.Data.Entity.SqlServer.Design.Utilities;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -18,14 +16,13 @@ public class SqlServerDesignTimeServices
public virtual void ConfigureDesignTimeServices([NotNull] IServiceCollection serviceCollection)
{
serviceCollection
.AddSingleton<MetadataReferencesProvider>()
.AddSingleton<ICompilationService, RoslynCompilationService>()
.AddSingleton<RazorTemplating>()
.AddSingleton<IDatabaseMetadataModelProvider, SqlServerMetadataModelProvider>()
.AddSingleton<IRelationalAnnotationProvider, SqlServerAnnotationProvider>()
.AddSingleton<SqlServerLiteralUtilities>()
.AddSingleton<ConfigurationFactory, SqlServerConfigurationFactory>()
.AddSingleton<CodeWriter, RazorTemplateCodeWriter>();
.AddSingleton<DbContextWriter, DbContextWriter>()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: where the types are the same, using just one type argument has the same effect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.

.AddSingleton<EntityTypeWriter, EntityTypeWriter>()
.AddSingleton<CodeWriter, StringBuilderCodeWriter>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,10 @@
<Compile Include="ReverseEngineering\FileSystemFileService.cs" />
<Compile Include="ReverseEngineering\IDatabaseMetadataModelProvider.cs" />
<Compile Include="ReverseEngineering\IFileService.cs" />
<Compile Include="ReverseEngineering\Internal\RazorTemplateCodeWriter.cs" />
<Compile Include="ReverseEngineering\Internal\DbContextWriter.cs" />
<Compile Include="ReverseEngineering\Internal\EntityTypeWriter.cs" />
<Compile Include="ReverseEngineering\Internal\StringBuilderCodeWriter.cs" />
<Compile Include="ReverseEngineering\Internal\TableSelectionSet.cs" />
<Compile Include="ReverseEngineering\Internal\Templating\Compilation\CompiledAssemblyResult.cs" />
<Compile Include="ReverseEngineering\Internal\Templating\Compilation\CompilationResult.cs" />
<Compile Include="ReverseEngineering\Internal\Templating\Compilation\ICompilationService.cs" />
<Compile Include="ReverseEngineering\Internal\Templating\Compilation\MetadataReferencesProvider.cs" />
<Compile Include="ReverseEngineering\Internal\Templating\Compilation\RoslynCompilationService.cs" />
<Compile Include="ReverseEngineering\Internal\Templating\RazorReverseEngineeringBase.cs" />
<Compile Include="ReverseEngineering\Internal\Templating\RazorTemplating.cs" />
<Compile Include="ReverseEngineering\Internal\Templating\RazorTemplatingHost.cs" />
<Compile Include="ReverseEngineering\Internal\Templating\TemplateProcessingException.cs" />
<Compile Include="ReverseEngineering\Internal\Templating\TemplateResult.cs" />
<Compile Include="ReverseEngineering\MetadataModelNameMapper.cs" />
<Compile Include="ReverseEngineering\ReverseEngineerFiles.cs" />
<Compile Include="ReverseEngineering\ReverseEngineeringConfiguration.cs" />
Expand All @@ -113,12 +105,6 @@
<CustomToolNamespace>Microsoft.Data.Entity.Internal</CustomToolNamespace>
</Content>
<None Include="project.json" />
<EmbeddedResource Include="ReverseEngineering\Internal\Templates\DbContextTemplate.cshtml">
<LogicalName>EntityFramework.Relational.Design.ReverseEngineering.Internal.Templates.DbContextTemplate.cshtml</LogicalName>
</EmbeddedResource>
<EmbeddedResource Include="ReverseEngineering\Internal\Templates\EntityTypeTemplate.cshtml">
<LogicalName>EntityFramework.Relational.Design.ReverseEngineering.Internal.Templates.EntityTypeTemplate.cshtml</LogicalName>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EntityFramework.Relational\EntityFramework.Relational.csproj">
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,6 @@
<data name="ContextClassNotValidCSharpIdentifier" xml:space="preserve">
<value>The context class name passed in, {contextClassName}, is not a valid C# identifier.</value>
</data>
<data name="ErrorRunningDbContextTemplate" xml:space="preserve">
<value>There was an error running the DbContext template. Message: {errorMessage}</value>
</data>
<data name="ErrorRunningEntityTypeTemplate" xml:space="preserve">
<value>There was an error running the EntityType template. Message: {errorMessage}</value>
</data>
<data name="ProjectPathRequired" xml:space="preserve">
<value>ProjectPath is required to generate code.</value>
</data>
Expand All @@ -144,15 +138,9 @@
<data name="RootNamespaceRequired" xml:space="preserve">
<value>Root namespace of the project is required to generate code.</value>
</data>
<data name="TemplateProcessingFailed" xml:space="preserve">
<value>Template Processing Failed: {errorMessages}</value>
</data>
<data name="UnableToAddNavigationProperty" xml:space="preserve">
<value>Unable to add a Navigation Property referencing type {referencedEntityTypeName} because of errors generating that EntityType.</value>
</data>
<data name="UnableToCreateMetadataReference" xml:space="preserve">
<value>Unable to create metadata reference from name {metadataReferenceName}.</value>
</data>
<data name="UnableToGenerateEntityType" xml:space="preserve">
<value>Unable to generate EntityType {entityTypeName}. {errorMessage}</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public virtual List<NavigationPropertyInitializerConfiguration>
public virtual string ErrorMessageAnnotation
=> (string)EntityType[RelationalMetadataModelProvider.AnnotationNameEntityTypeError];

public virtual PropertyConfiguration FindPropertyConfiguration([NotNull] Property property)
public virtual PropertyConfiguration FindPropertyConfiguration([NotNull] IProperty property)
{
Check.NotNull(property, nameof(property));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,225 @@
// 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.Collections.Generic;
using JetBrains.Annotations;
using Microsoft.Data.Entity.Internal;
using Microsoft.Data.Entity.Relational.Design.ReverseEngineering.Configuration;
using Microsoft.Data.Entity.Relational.Design.Utilities;
using Microsoft.Data.Entity.Utilities;

namespace Microsoft.Data.Entity.Relational.Design.ReverseEngineering.Internal
{
public class DbContextWriter
{
private readonly ModelUtilities _modelUtilities;
private readonly CSharpUtilities _cSharpUtilities;
private IndentedStringBuilder _sb;
private ModelConfiguration _model;

public DbContextWriter(
[NotNull] ModelUtilities modelUtilities,
[NotNull] CSharpUtilities cSharpUtilities)
{
Check.NotNull(modelUtilities, nameof(modelUtilities));
Check.NotNull(cSharpUtilities, nameof(cSharpUtilities));

_modelUtilities = modelUtilities;
_cSharpUtilities = cSharpUtilities;
}

public virtual string WriteCode(
[NotNull] ModelConfiguration modelConfiguration)
{
Check.NotNull(modelConfiguration, nameof(modelConfiguration));

_model = modelConfiguration;
_sb = new IndentedStringBuilder();

_sb.AppendLine("using Microsoft.Data.Entity;");
_sb.AppendLine("using Microsoft.Data.Entity.Metadata;");
_sb.AppendLine();
_sb.AppendLine("namespace " + _model.Namespace());
_sb.AppendLine("{");
using (_sb.Indent())
{
AddClass();
}
_sb.Append("}");

return _sb.ToString();
}

public virtual void AddClass()
{
var className =
string.IsNullOrWhiteSpace(_model.CustomConfiguration.ContextClassName)
? _model.ClassName()
: _model.CustomConfiguration.ContextClassName;
_sb.AppendLine("public partial class " + className + " : DbContext");
_sb.AppendLine("{");
using (_sb.Indent())
{
AddOnConfiguring();
AddOnModelCreating();
AddDbSetProperties();
}
_sb.AppendLine("}");
}

public virtual void AddOnConfiguring()
{
_sb.AppendLine("protected override void OnConfiguring(DbContextOptionsBuilder options)");
_sb.AppendLine("{");
using (_sb.Indent())
{
foreach (var optionsBuilderConfig in _model.OnConfiguringConfigurations)
{
_sb.AppendLine("options." + optionsBuilderConfig.FluentApi + ";");
}
}
_sb.AppendLine("}");
}

public virtual void AddOnModelCreating()
{
_sb.AppendLine();
_sb.AppendLine("protected override void OnModelCreating(ModelBuilder modelBuilder)");
_sb.AppendLine("{");

using (_sb.Indent())
{
var first = true;
foreach (var entityConfig in _model.OrderedEntityConfigurations())
{
var fluentApiConfigurations = entityConfig.GetFluentApiConfigurations(_model.CustomConfiguration.UseFluentApiOnly);
var propertyConfigurations = entityConfig.GetPropertyConfigurations(_model.CustomConfiguration.UseFluentApiOnly);
var relationshipConfigurations = entityConfig.GetRelationshipConfigurations(_model.CustomConfiguration.UseFluentApiOnly);
if (fluentApiConfigurations.Count == 0
&& propertyConfigurations.Count == 0
&& relationshipConfigurations.Count == 0)
{
continue;
}

if (!first)
{
_sb.AppendLine();
}
first = false;

_sb.AppendLine("modelBuilder.Entity<" + entityConfig.EntityType.Name + ">(entity =>");
_sb.AppendLine("{");
using (_sb.Indent())
{
var shouldAddNewLine = AddEntityFluentApi(fluentApiConfigurations);
shouldAddNewLine = AddPropertyConfigurations(shouldAddNewLine, propertyConfigurations);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about this some more - will make shouldAddNewLine into member rather than passing it around.

shouldAddNewLine = AddRelationshipConfigurations(shouldAddNewLine, relationshipConfigurations);
}
_sb.AppendLine("});");
}
}

_sb.AppendLine("}");
}

public virtual bool AddEntityFluentApi(
[NotNull] List<IFluentApiConfiguration> fluentApiConfigurations)
{
Check.NotNull(fluentApiConfigurations, nameof(fluentApiConfigurations));

var shouldAddNewLine = false;
foreach (var entityFluentApi in fluentApiConfigurations)
{
if (shouldAddNewLine)
{
_sb.AppendLine();
}
shouldAddNewLine = true;
_sb.AppendLine("entity." + entityFluentApi.FluentApi + ";");
}

return shouldAddNewLine;
}

public virtual bool AddPropertyConfigurations(
bool shouldAddNewLine,
[NotNull] List<PropertyConfiguration> propertyConfigurations)
{
Check.NotNull(propertyConfigurations, nameof(propertyConfigurations));

foreach (var propertyConfig in propertyConfigurations)
{
var propertyConfigurationLines =
_modelUtilities.LayoutPropertyConfigurationLines(
propertyConfig, "property", " ", _model.CustomConfiguration.UseFluentApiOnly);
if (shouldAddNewLine)
{
_sb.AppendLine();
}
shouldAddNewLine = true;

if (propertyConfigurationLines.Count == 1)
{
foreach (var line in propertyConfigurationLines)
{
_sb.AppendLine("entity.Property(e => e." + propertyConfig.Property.Name + ")" + line + ";");
}
}
else
{
_sb.AppendLine("entity.Property(e => e." + propertyConfig.Property.Name + ")");
using (_sb.Indent())
{
var lineCount = 0;
foreach (string line in propertyConfigurationLines)
{
var outputLine = line;
if (++lineCount == propertyConfigurationLines.Count)
{
outputLine = line + ";";
}
_sb.AppendLine(outputLine);
}
}
}
}

return shouldAddNewLine;
}

public virtual bool AddRelationshipConfigurations(
bool shouldAddNewLine,
[NotNull] List<RelationshipConfiguration> relationshipConfigurations)
{
Check.NotNull(relationshipConfigurations, nameof(relationshipConfigurations));

foreach (var relationshipConfig in relationshipConfigurations)
{
if (shouldAddNewLine)
{
_sb.AppendLine();
}
shouldAddNewLine = true;
_sb.AppendLine("entity."
+ _modelUtilities.LayoutRelationshipConfigurationLine(
relationshipConfig, "d", "p")
+ ";");
}

return shouldAddNewLine;
}

public virtual void AddDbSetProperties()
{
_sb.AppendLine();
foreach(var entityConfig in _model.OrderedEntityConfigurations())
{
_sb.AppendLine("public virtual DbSet<"
+ entityConfig.EntityType.Name
+ "> " + entityConfig.EntityType.Name
+ " { get; set; }");
}
}
}
}
Loading