Service replacement in EntityFrameworkCore.Design (DesignTimeServicesBuilder.cs)

733 views Asked by At

I am attempting to replace the StringBuilderCodeWriter in Microsoft.EntityFrameworkCore.Design with my own custom implementation in order to modify the scaffolding to generate repository pattern classes.

I am overriding the ConfigureServices method in the DesignTimeServicesBuilder to add my CustomStringBuilderCodeWritter but I can't get it to use my implementation.

I am testing this out in a .NET Core console app using EF Core 1.1

Program.cs

using System.Collections.Generic;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.Scaffolding.Internal;
using Microsoft.Extensions.DependencyInjection;
using System.Threading;

namespace RepositoryPattern
{
    public class Program
    { 
        public static void Main(string[] args)
        {
            IOperationReportHandler handler = new OperationReportHandler();
            IOperationReporter reporter = new OperationReporter(handler);

            var startup = new StartupInvoker(reporter, Assembly.Load(new AssemblyName("Microsoft.EntityFrameworkCore.Design")), "ADONET_DATA_DIR", @"ANOTHER_OUTPUT_PATH");
            CustomDesignTimeServicesBuilder servicesBuilder = new CustomDesignTimeServicesBuilder(startup);

            var services = servicesBuilder.Build("Microsoft.EntityFrameworkCore.SqlServer");

            var generator = services.GetRequiredService<ReverseEngineeringGenerator>();

            // The TableSelectionSet seems to be ignored for some reason, ignore for now.
            var tableSelectionSet = new TableSelectionSet(new List<string> { "Customers", "Employees" }, new List<string> { "dbo" });
            var configuration = new ReverseEngineeringConfiguration
            {
                ConnectionString = @"Server=(localdb)\Samples;Database=Northwind;Trusted_Connection=True;",
                ContextClassName = "NorthwindContext",
                ProjectPath = @"PROJECT_PATH_GOES_HERE",
                ProjectRootNamespace = "RepositoryPattern",
                OutputPath = @"WHERE_THE_GENERATED_FILES_GO",
                TableSelectionSet = tableSelectionSet,
                UseFluentApiOnly = true,
                OverwriteFiles = true
            };

            generator.GenerateAsync(configuration, new CancellationToken());
        }    
    }
}

CustomDesignTimeServicesBuilder.cs

using Microsoft.EntityFrameworkCore.Migrations.Design;
using Microsoft.EntityFrameworkCore.Scaffolding.Internal;
using Microsoft.EntityFrameworkCore.Design.Internal;
using Microsoft.Extensions.DependencyInjection;

namespace RepositoryPattern 
{
    public class CustomDesignTimeServicesBuilder : DesignTimeServicesBuilder
    {
        public CustomDesignTimeServicesBuilder(StartupInvoker startupInvoker) : base(startupInvoker)
        { }

        protected override IServiceCollection ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton<CSharpHelper>();
            services.AddSingleton<CSharpMigrationOperationGenerator>();
            services.AddSingleton<CSharpSnapshotGenerator>();
            services.AddSingleton<MigrationsCodeGenerator, CSharpMigrationsGenerator>();
            services.AddScaffolding();
            services.AddSingleton<StringBuilderCodeWriter, CustomStringBuilderCodeWriter>();
            services.AddLogging();

            return base.ConfigureServices(services);
        }
    }
}

CustomStringBuilderCodeWriter.cs This implementation creates in extra class while looping through EntityConfigurations to generate a repository style class.

using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using Microsoft.EntityFrameworkCore.Scaffolding.Configuration.Internal;
using Microsoft.EntityFrameworkCore.Scaffolding.Internal;
using Microsoft.EntityFrameworkCore.Utilities;

namespace RepositoryPattern
{
    /// <summary>
    ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
    ///     directly from your code. This API may change or be removed in future releases.
    /// </summary>
    public class CustomStringBuilderCodeWriter : StringBuilderCodeWriter
    {
        public CustomStringBuilderCodeWriter(
            IFileService fileService, 
            DbContextWriter dbContextWriter, 
            EntityTypeWriter entityTypeWriter) 
            : base(fileService, dbContextWriter, entityTypeWriter)
        { }

        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public override Task<ReverseEngineerFiles> WriteCodeAsync(
            ModelConfiguration modelConfiguration,
            string outputPath,
            string dbContextClassName,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            var resultingFiles = new ReverseEngineerFiles();

            var generatedCode = DbContextWriter.WriteCode(modelConfiguration);

            // output DbContext .cs file
            var dbContextFileName = dbContextClassName + FileExtension;
            var dbContextFileFullPath = FileService.OutputFile(
                outputPath, dbContextFileName, generatedCode);
            resultingFiles.ContextFile = dbContextFileFullPath;

            foreach (var entityConfig in modelConfiguration.EntityConfigurations)
            {
                generatedCode = EntityTypeWriter.WriteCode(entityConfig);

                // output EntityType poco .cs file
                var entityTypeFileName = entityConfig.EntityType.DisplayName() + FileExtension;
                var entityTypeFileFullPath = FileService.OutputFile(
                    outputPath, entityTypeFileName, generatedCode);
                resultingFiles.EntityTypeFiles.Add(entityTypeFileFullPath);

                RepositoryWriter repositoryWriter = new RepositoryWriter(new CSharpUtilities());
                generatedCode = repositoryWriter.WriteCode(entityConfig);

                // output Repository .cs file
                var repositoryFileName = entityConfig.EntityType.DisplayName() + "Repository" + FileExtension;
                var repositoryFileFullPath = FileService.OutputFile(
                    outputPath, repositoryFileName, generatedCode);
            }

            return Task.FromResult(resultingFiles);
        }
    }
}
2

There are 2 answers

0
Jamie On BEST ANSWER

My problem described above was located in the CustomDesignTimeServicesBuilder.cs file and was because of this line:

return base.ConfigureServices(services);

which needed to become...

return services;

Basically I believe I was overriding my injection by placing another call to the base DesignTimeServicesBuilder.

0
Ansil On

Try to use base abstract class CodeWriter instead of StringBuilderCodeWriter as follows:

public class CustomStringBuilderCodeWriter : CodeWriter

Then add service injection in CustomDesignTimeServicesBuilder as follows:

protected override IServiceCollection ConfigureServices(IServiceCollection services) {
    services.AddSingleton<CSharpHelper>();
    services.AddSingleton<CSharpMigrationOperationGenerator>();
    services.AddSingleton<CSharpSnapshotGenerator>();
    services.AddSingleton<MigrationsCodeGenerator, CSharpMigrationsGenerator>();
    services.AddScaffolding();
    services.AddSingleton<CodeWriter, CustomStringBuilderCodeWriter>();
    services.AddLogging();

    return services;
}