コード例 #1
0
        public virtual async Task <TemplateResult> RunTemplateAsync(string content,
                                                                    dynamic templateModel, IDatabaseMetadataModelProvider provider,
                                                                    CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            var host   = new RazorTemplatingHost(typeof(RazorReverseEngineeringBase));
            var engine = new RazorTemplateEngine(host);

            using (var reader = new StringReader(content))
            {
                var generatorResults = engine.GenerateCode(reader);

                if (!generatorResults.Success)
                {
                    var messages = generatorResults.ParserErrors.Select(e => e.Message);
                    return(new TemplateResult
                    {
                        GeneratedText = string.Empty,
                        ProcessingException = new TemplateProcessingException(messages)
                    });
                }

                provider.AddReferencesForTemplates(_metadataReferencesProvider);
                var references     = _metadataReferencesProvider.GetApplicationReferences();
                var templateResult = _compilationService.Compile(
                    new List <string> {
                    generatorResults.GeneratedCode
                }, references);
                if (templateResult.Messages.Any())
                {
                    return(new TemplateResult
                    {
                        GeneratedText = string.Empty,
                        ProcessingException = new TemplateProcessingException(templateResult.Messages)
                    });
                }

                var compiledObject = Activator.CreateInstance(templateResult.CompiledType);
                var razorTemplate  = compiledObject as RazorReverseEngineeringBase;

                var result = String.Empty;
                if (razorTemplate != null)
                {
                    razorTemplate.Model = templateModel;
                    //ToDo: If there are errors executing the code, they are missed here.
                    result = await razorTemplate.ExecuteTemplateAsync(cancellationToken);
                }

                return(new TemplateResult
                {
                    GeneratedText = result,
                    ProcessingException = null
                });
            }
        }
コード例 #2
0
        public virtual async Task<TemplateResult> RunTemplateAsync(string content,
            dynamic templateModel, IDatabaseMetadataModelProvider provider,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();

            var host = new RazorTemplatingHost(typeof(RazorReverseEngineeringBase));
            var engine = new RazorTemplateEngine(host);

            using (var reader = new StringReader(content))
            {
                var generatorResults = engine.GenerateCode(reader);

                if (!generatorResults.Success)
                {
                    var messages = generatorResults.ParserErrors.Select(e => e.Message);
                    return new TemplateResult
                    {
                        GeneratedText = string.Empty,
                        ProcessingException = new TemplateProcessingException(messages)
                    };
                }

                provider.AddReferencesForTemplates(_metadataReferencesProvider);
                var references = _metadataReferencesProvider.GetApplicationReferences();
                var templateResult = _compilationService.Compile(
                    new List<string> { generatorResults.GeneratedCode }, references);
                if (templateResult.Messages.Any())
                {
                    return new TemplateResult
                    {
                        GeneratedText = string.Empty,
                        ProcessingException = new TemplateProcessingException(templateResult.Messages)
                    };
                }

                var compiledObject = Activator.CreateInstance(templateResult.CompiledType);
                var razorTemplate = compiledObject as RazorReverseEngineeringBase;

                var result = String.Empty;
                if (razorTemplate != null)
                {
                    razorTemplate.Model = templateModel;
                    //ToDo: If there are errors executing the code, they are missed here.
                    result = await razorTemplate.ExecuteTemplateAsync(cancellationToken);
                }

                return new TemplateResult
                {
                    GeneratedText = result,
                    ProcessingException = null
                };
            }
        }