IntentArchitect / Support

A repository dedicated to handling issues and support queries
3 stars 0 forks source link

Generating a CSharpFile from ClassModel #47

Closed jpseini closed 1 year ago

jpseini commented 1 year ago

Ask a question

Is there existing functionality in any Intent modules, which is able to take a ClassModel instance (designed in the Domain Designer), and automatically convert it to a CSharpFile (the same as if it was generated using the builder pattern)? Or is the way to do that conversion up to the builder of the module (me in this instance) to do that mapping and setup of the class using the builder pattern to create an instance of CSharpFile?

JonathanLydall commented 1 year ago

Hi @jpseini,

It's the latter, the current model is that the module/template authors needs to decide for themselves how to call the CSharpFileBuilder for a particular element type from Intent.

Are you just looking for an example, or do you find yourself having to make a lot of different templates to generate domain classes?

jpseini commented 1 year ago

Thanks @JonathanLydall

Neither of those options - with all the configuration/design which can be done in the Domain Designer, I was just "concerned" that I might end up missing some translation between the two. But its not an issue, I'll just make sure to cater for all possible configuration in the translation.

jpseini commented 1 year ago

@JonathanLydall - do you have an example of "converting" a ClassModel to a CSharpFile? Just so I can make sure I'm on the right track and doing it correctly?

garethbaars commented 1 year ago

Hi @jpseini,

Here's a quick example:

CSharpFile = new CSharpFile(this.GetNamespace(), this.GetFolderPath())
    .AddClass($"{Model.Name}", @class =>
    {
        foreach (var attr in Model.Attributes)
        {
            @class.AddProperty(GetTypeName(attr), attr.Name.ToPascalCase());
        }
        foreach (var associations in Model.AssociatedClasses)
        {
            @class.AddProperty(GetTypeName(associations), associations.Name.ToPascalCase());
        }
        foreach (var operation in Model.Operations)
        {
            @class.AddMethod(GetTypeName(operation), operation.Name.ToPascalCase(), method =>
            {
                foreach (var parameter in operation.Parameters)
                {
                    method.AddParameter(GetTypeName(parameter), parameter.Name.ToCamelCase());
                }
            });
        }
    });

If the model is as follows: image

This would output the following code: image

Does this give you what you are looking for?

jpseini commented 1 year ago

Perfect - thanks @JonathanLydall and @garethbaars (apologies)