Exemple #1
0
        private MethodDeclarationSyntax _CreateOutputClassWithMethod(TypeDefinition typeDefinition, PropertyDefinition propertyDefinition)
        {
            var _validationStatement = _CreatePropertyValidationStatement(propertyDefinition, _property => NameHelper.TextToMethodParameterName(_property.Name));
            var _typeCtorArguments   = _CreatePropertyArgumentList(
                typeDefinition,
                _property => (_property.Name == propertyDefinition.Name ? NameHelper.TextToMethodParameterName(propertyDefinition.Name) : NameHelper.TextToPublicPropertyName(_property.Name)));
            var _createTypeInstanceStatement = _CreateDeclareVariableAndCreateTypeInstanceStatement(typeDefinition, _typeCtorArguments, SyntaxFactory.Identifier("_instance"));
            var _typeValidationStatement     = _CreateTypeValidationStatement(typeDefinition, SyntaxFactory.IdentifierName("_instance"));
            var _returnTypeInstanceStatement = SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName("_instance"));

            return(SyntaxFactory
                   .MethodDeclaration(
                       SyntaxFactory.IdentifierName(typeDefinition.Name),
                       NameHelper.TextToPublicMethodIdentifier("With" + propertyDefinition.Name))
                   .AddParameterListParameters(SyntaxFactory
                                               .Parameter(NameHelper.TextToMethodParameterIdentifier(propertyDefinition.Name))
                                               .WithType(SyntaxFactory.IdentifierName(propertyDefinition.Type)))
                   .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
                   .WithBody(SyntaxFactory.Block()
                             .AddStatements(_validationStatement)
                             .AddStatements(_createTypeInstanceStatement)
                             .AddStatements(_typeValidationStatement)
                             .AddStatements(_returnTypeInstanceStatement)));
        }
Exemple #2
0
        private MethodDeclarationSyntax _CreateOutputClassCreateMethod(TypeDefinition typeDefinition)
        {
            var _propertyValidationStatements = _CreatePropertyValidationStatements(typeDefinition, _property => NameHelper.TextToMethodParameterName(_property.Name));
            var _typeCtorArguments            = _CreatePropertyArgumentList(typeDefinition, _property => (_property.DefaultValuePropertyName ?? NameHelper.TextToMethodParameterName(_property.Name)));
            var _createTypeInstanceStatement  = _CreateDeclareVariableAndCreateTypeInstanceStatement(typeDefinition, _typeCtorArguments, SyntaxFactory.Identifier("_instance"));
            var _typeValidationStatement      = _CreateTypeValidationStatement(typeDefinition, SyntaxFactory.IdentifierName("_instance"));
            var _returnTypeInstanceStatement  = SyntaxFactory.ReturnStatement(SyntaxFactory.IdentifierName("_instance"));

            return(SyntaxFactory
                   .MethodDeclaration(SyntaxFactory.IdentifierName(typeDefinition.Name), NameHelper.TextToPublicMethodIdentifier("Create"))
                   .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword), SyntaxFactory.Token(SyntaxKind.StaticKeyword))
                   .WithParameterList(SyntaxFactory.ParameterList(SyntaxFactory.SeparatedList(typeDefinition.Properties
                                                                                              .Where(_property => _property.DefaultValuePropertyName == null)
                                                                                              .Select(_property => SyntaxFactory
                                                                                                      .Parameter(NameHelper.TextToMethodParameterIdentifier(_property.Name))
                                                                                                      .WithType(SyntaxFactory.IdentifierName(_property.Type))))))
                   .WithBody(SyntaxFactory.Block()
                             .AddStatements(_propertyValidationStatements)
                             .AddStatements(_createTypeInstanceStatement)
                             .AddStatements(_typeValidationStatement)
                             .AddStatements(_returnTypeInstanceStatement)));
        }