コード例 #1
0
        public static SyntaxTree BuildDto(EntityMetadata entity, SyntaxTree existingDto = null, string dtoNamespace = null, string mapperNamespace = null, bool generateMapper = true, bool addContractAttrs = false)
        {
            CompilationUnitSyntax root = null;

            if (existingDto == null)
            {
                var rawTree = BuildOutline(dtoNamespace, entity);

                var commentAppender = new EmptyTreeCommentAppender();
                root = commentAppender.Visit(rawTree.GetRoot()) as CompilationUnitSyntax;
            }
            else
            {
                var existingRoot = existingDto.GetRoot();

                var finder = new CustomCodeLocator();
                finder.Visit(existingRoot);

                var rewriter = new GeneratedCodeRemover(finder);
                existingRoot = rewriter.Visit(existingRoot);

                if (rewriter.FirstCustomProperty == null)
                {
                    var commentAppender = new EmptyTreeCommentAppender();
                    root = commentAppender.Visit(existingRoot) as CompilationUnitSyntax;
                }
                else
                {
                    var commentWrapper = new CustomCodeCommentWrapper(rewriter);
                    root = commentWrapper.Visit(existingRoot) as CompilationUnitSyntax;
                }
            }

            if (generateMapper)
            {
                root = root.AppendUsing(mapperNamespace, entity.Namespace);
            }

            if (addContractAttrs)
            {
                root = root.AppendUsing("System.Runtime.Serialization");
            }

            var generatedPropertiesAppender = new GeneratedPropertiesAppender(entity, addContractAttrs);

            root = generatedPropertiesAppender.Visit(root) as CompilationUnitSyntax;

            var newLineRemover = new NewLineRemover();

            root = newLineRemover.Visit(root) as CompilationUnitSyntax;

            if (!generateMapper)
            {
                var mapperRemover = new MapperRemover();
                root = mapperRemover.Visit(root) as CompilationUnitSyntax;
            }

            return(SyntaxFactory.SyntaxTree(root));
        }