Ejemplo n.º 1
0
        /// <summary>
        /// Gets the transform back code.
        /// </summary>
        public override IEnumerable <StatementSyntax> GetTransformBackCode()
        {
            // declare converter
            var converterName = GenerateCollectionHandlerName(PropertyMember.Name);
            var converterType = GetCollectionHandlerType(typeof(ReplaceEntriesCollectionHandler <,>), PropertyMember.TargetType, PropertyMember.Type);

            yield return(SyntaxHelper.GenerateVariableDeclarationAndObjectCreationStatement(converterName, converterType));

            yield return(GenerateCollectionNullCheck(PropertyMember.Type, "source", PropertyMember.Name));

            yield return(GenerateCollectionNullCheck(PropertyMember.TargetType, "target", PropertyMember.TargetName));

            // call the converter
            yield return(SyntaxHelper.GenerateStaticMethodCall("SynchronizeCollections", converterName, new[]
            {
                SyntaxFactory.Argument(SyntaxFactory.ParseName("target." + PropertyMember.TargetName)),
                SyntaxFactory.Argument(SyntaxFactory.ParseName("source." + PropertyMember.Name))
            }));
        }
        /// <summary>
        /// Gets the transform back code.
        /// </summary>
        public override IEnumerable <StatementSyntax> GetTransformBackCode()
        {
            // declare converter
            var converterName = GenerateCollectionHandlerName(PropertyMember.Name);
            var converterType = GetCollectionHandlerType(typeof(SyncCollectionHandler <,>), PropertyMember.TargetType, PropertyMember.Type);
            var elementTypes  = converterType.GetGenericArguments();

            yield return(SyntaxHelper.GenerateVariableDeclarationAndObjectCreationStatement(converterName, converterType));

            // set key accessors
            yield return(SyntaxHelper.GenerateAssignmentStatement(
                             SyntaxFactory.ParseName(converterName + ".KeySelector1"),
                             SyntaxFactory.ParseExpression("__x => __x." + KeyPropertyName)
                             ));

            yield return(SyntaxHelper.GenerateAssignmentStatement(
                             SyntaxFactory.ParseName(converterName + ".KeySelector2"),
                             SyntaxFactory.ParseExpression("__x => __x." + KeyPropertyName)
                             ));

            yield return(SyntaxHelper.GenerateAssignmentStatement(
                             SyntaxFactory.ParseName(converterName + ".NewItemFactory"),
                             SyntaxHelper.GenerateGenericName(typeof(PropertyConverter).FullName + ".Convert", elementTypes)
                             ));

            yield return(SyntaxHelper.GenerateAssignmentStatement(
                             SyntaxFactory.ParseName(converterName + ".UpdateItemAction"),
                             SyntaxHelper.GenerateGenericName(typeof(PropertyConverter).FullName + ".Populate", elementTypes)
                             ));

            yield return(GenerateCollectionNullCheck(PropertyMember.Type, "source", PropertyMember.Name));

            yield return(GenerateCollectionNullCheck(PropertyMember.TargetType, "target", PropertyMember.TargetName));

            // call the converter
            yield return(SyntaxHelper.GenerateStaticMethodCall("SynchronizeCollections", converterName, new[]
            {
                SyntaxFactory.Argument(SyntaxFactory.ParseName("target." + PropertyMember.TargetName)),
                SyntaxFactory.Argument(SyntaxFactory.ParseName("source." + PropertyMember.Name))
            }));
        }
        /// <summary>
        /// Renders the code.
        /// </summary>
        public override IEnumerable <MemberDeclarationSyntax> Render()
        {
            // TransformToSource method
            var method = SyntaxHelper.GenerateExtensionMethod("TransformToSource", Transform.SourceType.FullName, new []
            {
                SyntaxHelper.GenerateMethodParameter("target", Transform.TargetType.FullName, true)
            },
                                                              new[] {
                SyntaxHelper.GenerateAttribute(
                    typeof(DtoConvertFunctionAttribute),
                    SyntaxFactory.TypeOfExpression(SyntaxHelper.GenerateTypeSyntax(Transform.TargetType)),
                    SyntaxFactory.TypeOfExpression(SyntaxHelper.GenerateTypeSyntax(Transform.SourceType)),
                    SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression)
                    )
            })
                         .WithBody(SyntaxFactory.Block(
                                       new StatementSyntax[] {
                SyntaxHelper.GenerateStaticMethodCall("EnsureInitialized", typeof(PropertyConverter).FullName),
                SyntaxHelper.GenerateVariableDeclarationAndObjectCreationStatement("source", Transform.SourceType.FullName)
            }.Concat(
                                           Transform.Members.SelectMany(m => m.PropertyMemberRenderer.GetTransformBackCode()).ToArray()
                                           ).Concat(new[] {
                SyntaxFactory.ReturnStatement(SyntaxFactory.ParseName("source"))
            })
                                       ));

            // PopulateSource method
            var method2 = SyntaxHelper.GenerateExtensionMethod("PopulateSource", null, new[]
            {
                SyntaxHelper.GenerateMethodParameter("target", Transform.TargetType.FullName, true),
                SyntaxHelper.GenerateMethodParameter("source", Transform.SourceType.FullName, false)
            },
                                                               new[] {
                SyntaxHelper.GenerateAttribute(
                    typeof(DtoConvertFunctionAttribute),
                    SyntaxFactory.TypeOfExpression(SyntaxHelper.GenerateTypeSyntax(Transform.TargetType)),
                    SyntaxFactory.TypeOfExpression(SyntaxHelper.GenerateTypeSyntax(Transform.SourceType)),
                    SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression)
                    )
            })
                          .WithBody(SyntaxFactory.Block(
                                        new StatementSyntax[]
            {
                SyntaxHelper.GenerateStaticMethodCall("EnsureInitialized", typeof(PropertyConverter).FullName),
            }.Concat(
                                            Transform.Members.SelectMany(m => m.PropertyMemberRenderer.GetTransformBackCode()).ToArray()
                                            )
                                        ));

            // generate the static class
            var className = Transform.TargetType.Name + "Extensions";

            yield return(SyntaxHelper.GenerateNamespace(Transform.TargetType.Namespace, new MemberDeclarationSyntax[]
            {
                SyntaxHelper.GenerateClass(
                    className,
                    new[] { SyntaxKind.PublicKeyword, SyntaxKind.StaticKeyword },
                    new MemberDeclarationSyntax[] { method, method2 },
                    new[] { SyntaxHelper.GenerateAttribute(typeof(DtoGeneratedAttribute)) }
                    )
            },
                                                        GetUsingsForNamespace()));
        }