Exemplo n.º 1
0
 /// <summary>
 /// Constructor for the Handlebars entity types generator.
 /// </summary>
 /// <param name="annotationCodeGenerator">Annotation code generator.</param>
 /// <param name="entityTypeTemplateService">Template service for the entity types generator.</param>
 /// <param name="entityTypeTransformationService">Service for transforming entity definitions.</param>
 /// <param name="cSharpHelper">CSharp helper.</param>
 /// <param name="typeScriptHelper">TypeScript helper.</param>
 /// <param name="options">Handlebars scaffolding options.</param>
 public HbsTypeScriptEntityTypeGenerator(
     [NotNull] IAnnotationCodeGenerator annotationCodeGenerator,
     [NotNull] IEntityTypeTemplateService entityTypeTemplateService,
     [NotNull] IEntityTypeTransformationService entityTypeTransformationService,
     [NotNull] ICSharpHelper cSharpHelper,
     [NotNull] ITypeScriptHelper typeScriptHelper,
     [NotNull] ResolvingNamesService resolvingNamesService,
     [NotNull] IOptions <HandlebarsScaffoldingOptions> options)
     : base(annotationCodeGenerator, cSharpHelper)
 {
     EntityTypeTemplateService       = entityTypeTemplateService;
     EntityTypeTransformationService = entityTypeTransformationService;
     CSharpHelper          = cSharpHelper;
     TypeScriptHelper      = typeScriptHelper;
     ResolvingNamesService = resolvingNamesService;
     _options = options;
 }
Exemplo n.º 2
0
 /// <summary>
 /// Constructor for the Handlebars DbContext generator.
 /// </summary>
 /// <param name="providerConfigurationCodeGenerator">Generator for scaffolding provider.</param>
 /// <param name="annotationCodeGenerator">Annotation code generator.</param>
 /// <param name="cSharpHelper">CSharp helper.</param>
 /// <param name="resolvingNamesService">resolvingNamesService.</param>
 /// <param name="dbContextTemplateService">Template service for DbContext generator.</param>
 /// <param name="entityTypeTransformationService">Service for transforming entity definitions.</param>
 /// <param name="options">Handlebars scaffolding options.</param>
 public HbsCSharpDbContextGenerator(
     [NotNull] IProviderConfigurationCodeGenerator providerConfigurationCodeGenerator,
     [NotNull] IAnnotationCodeGenerator annotationCodeGenerator,
     [NotNull] IDbContextTemplateService dbContextTemplateService,
     [NotNull] IEntityTypeTransformationService entityTypeTransformationService,
     [NotNull] ICSharpHelper cSharpHelper,
     [NotNull] ResolvingNamesService resolvingNamesService,
     [NotNull] IOptions <HandlebarsScaffoldingOptions> options)
     : base(providerConfigurationCodeGenerator, annotationCodeGenerator, cSharpHelper)
 {
     ProviderConfigurationCodeGenerator = providerConfigurationCodeGenerator;
     AnnotationCodeGenerator            = annotationCodeGenerator;
     CSharpHelper                    = cSharpHelper;
     DbContextTemplateService        = dbContextTemplateService;
     EntityTypeTransformationService = entityTypeTransformationService;
     ResolvingNamesService           = resolvingNamesService;
     _options = options;
 }
        /// <summary>
        /// Constructor for the Handlebars entity types generator.
        /// </summary>
        /// <param name="annotationCodeGenerator">Annotation code generator.</param>
        /// <param name="cSharpHelper">CSharp helper.</param>
        /// <param name="entityTypeTemplateService">Template service for the entity types generator.</param>
        /// <param name="entityTypeTransformationService">Service for transforming entity definitions.</param>
        /// <param name="options">Handlebar scaffolding options.</param>
        public HbsCSharpEntityTypeGenerator(
            [NotNull] IAnnotationCodeGenerator annotationCodeGenerator,
            [NotNull] ICSharpHelper cSharpHelper,
            [NotNull] IEntityTypeTemplateService entityTypeTemplateService,
            [NotNull] IEntityTypeTransformationService entityTypeTransformationService,
            [NotNull] ResolvingNamesService resolvingNamesService,
            [NotNull] IOptions <HandlebarsScaffoldingOptions> options)
            : base(annotationCodeGenerator, cSharpHelper)
        {
            Check.NotNull(annotationCodeGenerator, nameof(annotationCodeGenerator));
            Check.NotNull(cSharpHelper, nameof(cSharpHelper));

            AnnotationCodeGenerator         = annotationCodeGenerator;
            CSharpHelper                    = cSharpHelper;
            EntityTypeTemplateService       = entityTypeTemplateService;
            EntityTypeTransformationService = entityTypeTransformationService;
            ResolvingNamesService           = resolvingNamesService;
            _options = options;
        }
Exemplo n.º 4
0
        private void GenerateRelationship(IEntityType entityType, IForeignKey foreignKey, bool useDataAnnotations, IndentedStringBuilder sb)
        {
            var canUseDataAnnotations = true;
            var annotations           = AnnotationCodeGenerator
                                        .FilterIgnoredAnnotations(foreignKey.GetAnnotations())
                                        .ToDictionary(a => a.Name, a => a);

            AnnotationCodeGenerator.RemoveAnnotationsHandledByConventions(foreignKey, annotations);
            // TODO: Resolve TransformNavPropertyName() method
            NavEntityPropertyInfo navEntityPropertyInfo = ResolvingNamesService.ResolvingName(entityType, foreignKey);

            var lines = new List <string>
            {
                $".{nameof(EntityTypeBuilder.HasOne)}("
                + (foreignKey.DependentToPrincipal != null ? $"d => d.{EntityTypeTransformationService.TransformNavPropertyName(navEntityPropertyInfo.FieldName, navEntityPropertyInfo.ForeginEntityName, navEntityPropertyInfo.EntityName,foreignKey.DependentToPrincipal?.Name, foreignKey.PrincipalToDependent?.DeclaringType.Name, navEntityPropertyInfo.PropertyIsNullable)}" : null)
                + ")",
                $".{(foreignKey.IsUnique ? nameof(ReferenceNavigationBuilder.WithOne) : nameof(ReferenceNavigationBuilder.WithMany))}"
                + "("
                + (foreignKey.PrincipalToDependent != null ? $"p => p.{EntityTypeTransformationService.TransformNavPropertyName(navEntityPropertyInfo.FieldName, navEntityPropertyInfo.ForeginEntityName, navEntityPropertyInfo.EntityName, foreignKey.PrincipalToDependent?.Name, foreignKey.DependentToPrincipal?.DeclaringType.Name, navEntityPropertyInfo.PropertyIsNullable)}" : null)
                + ")"
            };

            if (!foreignKey.PrincipalKey.IsPrimaryKey())
            {
                Func <string, string, string> tmpNameTransformFunction = (tmpName, tmpType) =>
                {
                    return(EntityTypeTransformationService.TransformNavPropertyName(navEntityPropertyInfo.FieldName, navEntityPropertyInfo.ForeginEntityName, navEntityPropertyInfo.EntityName, tmpName, tmpType, navEntityPropertyInfo.PropertyIsNullable));
                };
                canUseDataAnnotations = false;
                lines.Add(
                    $".{nameof(ReferenceReferenceBuilder.HasPrincipalKey)}"
                    + (foreignKey.IsUnique ? $"<{EntityTypeTransformationService.TransformPropertyName(((ITypeBase)foreignKey.PrincipalEntityType).DisplayName(), "")}>" : "")
                    + $"(p => {GenerateLambdaToKey(foreignKey.PrincipalKey.Properties, "p", tmpNameTransformFunction)})");
            }

            lines.Add(
                $".{nameof(ReferenceReferenceBuilder.HasForeignKey)}"
                + (foreignKey.IsUnique ? $"<{GetEntityTypeName(foreignKey.PrincipalEntityType, EntityTypeTransformationService.TransformTypeEntityName(((ITypeBase)foreignKey.DeclaringEntityType).DisplayName()))}>" : "")
                + $"(d => {GenerateLambdaToKey(foreignKey.Properties, "d", EntityTypeTransformationService.TransformPropertyName)})");

            var defaultOnDeleteAction = foreignKey.IsRequired
                ? DeleteBehavior.Cascade
                : DeleteBehavior.ClientSetNull;

            if (foreignKey.DeleteBehavior != defaultOnDeleteAction)
            {
                canUseDataAnnotations = false;
                lines.Add(
                    $".{nameof(ReferenceReferenceBuilder.OnDelete)}({CSharpHelper.Literal(foreignKey.DeleteBehavior)})");
            }

            if (!string.IsNullOrEmpty((string)foreignKey[RelationalAnnotationNames.Name]))
            {
                canUseDataAnnotations = false;
            }

            lines.AddRange(
                AnnotationCodeGenerator.GenerateFluentApiCalls(foreignKey, annotations).Select(m => CSharpHelper.Fragment(m))
                .Concat(GenerateAnnotations(annotations.Values)));

            if (!useDataAnnotations ||
                !canUseDataAnnotations)
            {
                AppendMultiLineFluentApi(foreignKey.DeclaringEntityType, lines, sb);
            }
        }