Ejemplo n.º 1
0
        public E2ETestBase(ITestOutputHelper output)
        {
            _output = output;

            var serviceCollection = new ServiceCollection();

            GetFactory().AddMetadataProviderServices(serviceCollection);
            var serviceProvider = serviceCollection
                                  .AddScoped(typeof(ILogger), sp => _logger            = new InMemoryCommandLogger("E2ETest"))
                                  .AddScoped(typeof(IFileService), sp => InMemoryFiles = new InMemoryFileService())
                                  .BuildServiceProvider();

            Generator             = serviceProvider.GetRequiredService <ReverseEngineeringGenerator>();
            MetadataModelProvider = serviceProvider.GetRequiredService <IDatabaseMetadataModelProvider>();

            // set current cultures to English because expected results for error messages
            // (both those output to the Logger and those put in comments in the .cs files)
            // are in English
#if DNXCORE50
            CultureInfo.CurrentCulture   = new CultureInfo("en-US");
            CultureInfo.CurrentUICulture = new CultureInfo("en-US");
#else
            Thread.CurrentThread.CurrentCulture   = new CultureInfo("en-US");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
#endif
        }
Ejemplo n.º 2
0
        public ScaffoldingDto ScaffoldDatabase(string connectionString, string rootNamespace, string contextName)
        {
            var scaffUtils           = new ScaffoldingUtilities();
            var csharpUtils          = new CSharpUtilities();
            var provider             = new SqlServerAnnotationProvider();
            var customConfiguration  = new CustomConfiguration(connectionString, contextName, rootNamespace, true);
            var entityTypeWriter     = new EntityTypeWriter(csharpUtils);
            var dbContextWriter      = new SingularDbContextWriter(scaffUtils, csharpUtils);
            var configurationFactory = new ConfigurationFactory(provider, csharpUtils, scaffUtils);
            var loggerFactory        = new LoggerFactory();
            var logger        = loggerFactory.CreateLogger <SqlServerDatabaseModelFactory>();
            var revEngeConfig = new ReverseEngineeringConfiguration {
                ConnectionString = connectionString
            };
            var sqlScaffoldModelFactory = new SingularScaffoldingModelFactory(new LoggerFactory(),
                                                                              new SqlServerTypeMapper(), new SqlServerDatabaseModelFactory(logger), new CandidateNamingService());
            var codeWriter = new StringBuilderCodeWriter(new FileSystemFileService(), dbContextWriter, entityTypeWriter);

            var generator          = new ReverseEngineeringGenerator(sqlScaffoldModelFactory, configurationFactory, codeWriter);
            var metaModel          = generator.GetMetadataModel(revEngeConfig);
            var modelConfiguration = configurationFactory.CreateModelConfiguration(metaModel, customConfiguration);
            var dbContextData      = dbContextWriter.WriteCode(modelConfiguration);
            var scaffoldingDto     = new ScaffoldingDto
            {
                DbContextSource = dbContextData,
                ModelSources    = modelConfiguration.EntityConfigurations
                                  .Select(entityConfiguration => entityTypeWriter.WriteCode(entityConfiguration))
                                  .ToImmutableList()
            };

            return(scaffoldingDto);
        }
Ejemplo n.º 3
0
        public virtual IEnumerable <string> ReverseEngineer(
            [NotNull] string providerAssemblyName,
            [NotNull] string connectionString,
            [NotNull] string rootNamespace,
            [NotNull] string projectDir)
        {
            Check.NotNull(providerAssemblyName, nameof(providerAssemblyName));
            Check.NotEmpty(connectionString, nameof(connectionString));
            Check.NotEmpty(rootNamespace, nameof(rootNamespace));
            Check.NotEmpty(projectDir, nameof(projectDir));

            var assembly = Assembly.Load(new AssemblyName(providerAssemblyName));

            if (assembly == null)
            {
                throw new InvalidOperationException(Strings.CannotFindAssembly(providerAssemblyName));
            }

            var configuration = new ReverseEngineeringConfiguration()
            {
                ProviderAssembly = assembly,
                ConnectionString = connectionString,
                Namespace        = rootNamespace,
                OutputPath       = projectDir
            };

            var generator = new ReverseEngineeringGenerator(_serviceProvider);

            return(generator.GenerateAsync(configuration).Result);
        }
Ejemplo n.º 4
0
        public void E2ETest()
        {
            // set current cultures to English because expected results for error messages
            // (both those output to the Logger and those put in comments in the .cs files)
            // are in English
#if DNXCORE50
            CultureInfo.CurrentCulture   = new CultureInfo("en-US");
            CultureInfo.CurrentUICulture = new CultureInfo("en-US");
#else
            Thread.CurrentThread.CurrentCulture   = new CultureInfo("en-US");
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
#endif

            var assembly      = Assembly.Load(new AssemblyName(DatabaseTool._defaultReverseEngineeringProviderAssembly));
            var configuration = new ReverseEngineeringConfiguration
            {
                ProviderAssembly = assembly,
                ConnectionString = E2EConnectionString,
                Namespace        = TestNamespace,
                OutputPath       = TestOutputDir
            };

            var serviceProvider = SetupServiceProvider();
            var fileService     = new InMemoryFileService();
            serviceProvider.AddService(typeof(IFileService), fileService);
            var logger = new InMemoryCommandLogger("E2ETest");
            serviceProvider.AddService(typeof(ILogger), logger);

            var expectedFileContents = InitializeExpectedFileContents();

            var generator = new ReverseEngineeringGenerator(serviceProvider);
            var filePaths = generator.GenerateAsync(configuration).Result;

            Assert.Equal(_E2ETestExpectedWarnings.Count, logger.WarningMessages.Count);
            // loop over warnings instead of using the collection form of Assert.Equal()
            // to give better error messages if it does fail. Similarly for file paths below.
            var i = 0;
            foreach (var expectedWarning in _E2ETestExpectedWarnings)
            {
                Assert.Equal(expectedWarning, logger.WarningMessages[i++]);
            }
            Assert.Equal(0, logger.InformationMessages.Count);
            Assert.Equal(0, logger.VerboseMessages.Count);

            var expectedFilePaths = _E2ETestExpectedFileNames.Select(name => TestOutputDir + @"\" + name);
            Assert.Equal(expectedFilePaths.Count(), filePaths.Count);
            i = 0;
            foreach (var expectedFilePath in expectedFilePaths)
            {
                Assert.Equal(expectedFilePath, filePaths[i++]);
            }

            foreach (var fileName in _E2ETestExpectedFileNames)
            {
                var fileContents = fileService.RetrieveFileContents(TestOutputDir, fileName);
                Assert.Equal(expectedFileContents[fileName], fileContents);
            }
        }
 public SqlServerEntityTypeCodeGenerator(
     [NotNull] ReverseEngineeringGenerator generator,
     [NotNull] IEntityType entityType,
     [NotNull] string namespaceName)
     : base(generator, entityType, namespaceName)
 {
     Check.NotNull(generator, nameof(generator));
     Check.NotNull(entityType, nameof(entityType));
     Check.NotNull(namespaceName, nameof(namespaceName));
 }
        public void Constructs_correct_canonical_paths(
            string rootNamespace, string projectPath, string outputPath, string expectedNamepace,
            string canonicalizedFullOutputPath, string canonicalizedRelativeOutputPath)
        {
            var outputNamespaceAndPaths = ReverseEngineeringGenerator.ConstructNamespaceAndCanonicalizedPaths(
                rootNamespace, projectPath, outputPath);

            Assert.Equal(expectedNamepace, outputNamespaceAndPaths.Namespace);
            Assert.Equal(canonicalizedFullOutputPath, outputNamespaceAndPaths.CanonicalizedFullOutputPath);
            Assert.Equal(canonicalizedRelativeOutputPath, outputNamespaceAndPaths.CanonicalizedRelativeOutputPath);
        }
Ejemplo n.º 7
0
 public SqlServerDbContextCodeGenerator(
     [NotNull] ReverseEngineeringGenerator generator,
     [NotNull] IModel model, [NotNull] string namespaceName,
     [CanBeNull] string className, [NotNull] string connectionString)
     : base(generator, model, namespaceName, className, connectionString)
 {
     Check.NotNull(generator, nameof(generator));
     Check.NotNull(model, nameof(model));
     Check.NotEmpty(namespaceName, nameof(namespaceName));
     Check.NotEmpty(connectionString, nameof(connectionString));
 }
Ejemplo n.º 8
0
        public virtual EntityTypeCodeGenerator GetEntityTypeModelCodeGenerator(
            [NotNull]  ReverseEngineeringGenerator generator,
            [NotNull] EntityTypeGeneratorModel entityTypeGeneratorModel)
        {
            Check.NotNull(generator, nameof(generator));
            Check.NotNull(entityTypeGeneratorModel, nameof(entityTypeGeneratorModel));

            return(new SqlServerEntityTypeCodeGenerator(
                       generator,
                       entityTypeGeneratorModel.EntityType,
                       entityTypeGeneratorModel.Namespace));
        }
Ejemplo n.º 9
0
        public virtual DbContextCodeGenerator GetContextModelCodeGenerator(
            [NotNull] ReverseEngineeringGenerator generator,
            [NotNull] DbContextGeneratorModel dbContextGeneratorModel)
        {
            Check.NotNull(generator, nameof(generator));
            Check.NotNull(dbContextGeneratorModel, nameof(dbContextGeneratorModel));

            return(new SqlServerDbContextCodeGenerator(
                       generator,
                       dbContextGeneratorModel.MetadataModel,
                       dbContextGeneratorModel.Namespace,
                       dbContextGeneratorModel.ClassName,
                       dbContextGeneratorModel.ConnectionString));
        }
Ejemplo n.º 10
0
        protected E2ETestBase(ITestOutputHelper output)
        {
            _output = output;

            var serviceProvider = ConfigureDesignTimeServices(
                new ServiceCollection()
                .AddScaffolding()
                .AddLogging())
                                  .AddSingleton(typeof(IFileService), sp => InMemoryFiles = new InMemoryFileService()).BuildServiceProvider();

            _logger = new InMemoryCommandLogger("E2ETest", _output);
            serviceProvider.GetService <ILoggerFactory>().AddProvider(new TestLoggerProvider(_logger));

            Generator = serviceProvider.GetRequiredService <ReverseEngineeringGenerator>();
            ScaffoldingModelFactory = serviceProvider.GetRequiredService <IScaffoldingModelFactory>();
        }
Ejemplo n.º 11
0
        public E2ETestBase(ITestOutputHelper output)
        {
            _output = output;

            var serviceCollection = new ServiceCollection()
                                    .AddLogging();

            GetFactory().AddMetadataProviderServices(serviceCollection);
            serviceCollection.AddSingleton(typeof(IFileService), sp => InMemoryFiles = new InMemoryFileService());

            var serviceProvider = serviceCollection.BuildServiceProvider();

            _logger = new InMemoryCommandLogger("E2ETest", _output);
            serviceProvider.GetService <ILoggerFactory>().AddProvider(new TestLoggerProvider(_logger));

            Generator             = serviceProvider.GetRequiredService <ReverseEngineeringGenerator>();
            MetadataModelProvider = serviceProvider.GetRequiredService <IDatabaseMetadataModelProvider>();
        }
Ejemplo n.º 12
0
        protected E2ETestBase(ITestOutputHelper output)
        {
            _output = output;

            var serviceBuilder = new ServiceCollection()
                                 .AddScaffolding()
                                 .AddLogging();

            ConfigureDesignTimeServices(serviceBuilder);

            var serviceProvider = serviceBuilder
                                  .AddSingleton(typeof(IFileService), sp => InMemoryFiles = new InMemoryFileService()).BuildServiceProvider();

            _reporter = new InMemoryOperationReporter(_output);
            serviceProvider.GetService <ILoggerFactory>().AddProvider(new LoggerProvider(categoryName => new OperationLogger(categoryName, _reporter)));

            Generator = serviceProvider.GetRequiredService <ReverseEngineeringGenerator>();
            ScaffoldingModelFactory = serviceProvider.GetRequiredService <IScaffoldingModelFactory>();
        }
Ejemplo n.º 13
0
        public string GetSchemaSource(string connectionString, string assemblyNamespace, bool withUsings = true)
        {
            var loggerFactory = new LoggerFactory().AddConsole();

            var ssTypeMap     = new Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerTypeMapper();
            var ssDbFac       = new SqlServerDatabaseModelFactory(loggerFactory: loggerFactory);
            var ssScaffoldFac = new SqlServerScaffoldingModelFactory(
                loggerFactory: loggerFactory,
                typeMapper: ssTypeMap,
                databaseModelFactory: ssDbFac
                );

            var ssAnnotationProvider = new Microsoft.EntityFrameworkCore.Metadata.SqlServerAnnotationProvider();
            var csUtils    = new CSharpUtilities();
            var scaffUtils = new ScaffoldingUtilities();

            var confFac = new ConfigurationFactory(
                extensionsProvider: ssAnnotationProvider,
                cSharpUtilities: csUtils,
                scaffoldingUtilities: scaffUtils
                );
            var fs = new InMemoryFileService();
            var sb = new StringBuilderCodeWriter(
                fileService: fs,
                dbContextWriter: new DbContextWriter(
                    scaffoldingUtilities: scaffUtils,
                    cSharpUtilities: csUtils
                    ),
                entityTypeWriter: new EntityTypeWriter(cSharpUtilities: csUtils, scaffoldingUtilities: scaffUtils)
                );

            var rGen = new ReverseEngineeringGenerator(
                loggerFactory: loggerFactory,
                scaffoldingModelFactory: ssScaffoldFac,
                configurationFactory: confFac,
                codeWriter: sb
                );

            var outputPath  = @"C:\temp";
            var programName = "Ctx";
            var conf        = new ReverseEngineeringConfiguration
            {
                ConnectionString     = connectionString,
                ContextClassName     = programName,
                ProjectPath          = "na",
                ProjectRootNamespace = assemblyNamespace,
                OutputPath           = outputPath
            };

            var output   = new StringBuilder();
            var resFiles = rGen.GenerateAsync(conf);

            resFiles.Wait();

            var ctx = CreateContext(fs.RetrieveFileContents(outputPath, programName + ".cs"), isLibrary: withUsings);

            Console.WriteLine("CreateContext", ctx);
            if (!withUsings)
            {
                ctx = StripHeaderLines(3, ctx);
            }
            else
            {
                output.Append(_refs);
            }
            output.Append(ctx);
            foreach (var fpath in resFiles.Result.EntityTypeFiles)
            {
                output.Append(StripHeaderLines(4, fs.RetrieveFileContents(outputPath, System.IO.Path.GetFileName(fpath))));
            }
            return(output.ToString());
        }