Attribute from Custom Property

Posts   
 
    
bhoatvolue
User
Posts: 6
Joined: 02-Sep-2022
# Posted on: 11-Oct-2022 11:57:16   

I have a database which has custom properties explaining what tables and fields are for. I get those successfully through entity generation and I also get them as xml comments in generated entity code. However, i would like to add the properties as [GraphQLDescription(<custom prop>)] attribute to entity classes and its members. I can't see how i can do that. Is there a way?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 11-Oct-2022 17:54:05   

There's no built-in way to do that currently. We have an attribute system which can use macros but there's no macro defined to e.g. pull a custom property with a given name into the attribute.

There's a way to do it, but it requires you to define a small plugin. This plugin is then executed after you have synced with the relational model in the database and which will traverse all entities and for each entity all fields and create/update/remove the attribute on the fields (and entity definitions).

In the source code archive on the website we ship the sourcecode of the plugins we ship with the designer. You can start with these to get a feel of what a plugin looks like. More information about plugins: https://www.llblgen.com/Documentation/5.9/SDK/gui_implementingplugin.htm To run a plugin on a given designer event, you have to bind it to a designer event, which can be defined to be done automatically. If you look at the source of the PluralToSingularConverterPlugin, you see it binds to a designer event in its definition object so the designer will run that plugin whenever that event arises.

Custom properties are stored in the OutputSettingValuesContainer of an object. If you open the designer and load your project, right click the project node and select Plugins -> Project inspector plugin. This will give you a raw tree of the object graph that represents the project. If you navigate to EntityModel -> Vertices (which are the entities, it's a graph) and then to an entity definition, you see the OutputSettingValues property. You can access that in a plugin and read/write its properties. Custom Properties are stored as name-value pairs. Fields have the same object.

What you want to maintain now for entities and fields are entries in the AttributesWithRules list in an object's OutputSettingValuesContainer. These are pairs of string-rule objects (you can leave rule as null). So basically when your plugin is ran, you traverse all entities, and for each entity all fields, grab for each object the Customproperty you need and create/update/remove the attribute definition in its AttributesWithRules list. After you've done that, you should see the attributes in the list of Additional Attributes for a field or entity in its editor, code generation tab.

Links to documentation you likely need: Reference manual of designer classes: https://www.llblgen.com/Documentation/5.9/ReferenceManuals/Designer/index.html OutputSettingValuesContainer class reference manual: https://www.llblgen.com/Documentation/5.9/ReferenceManuals/Designer/html/516BD3FB.htm To obtain all entities in a project: https://www.llblgen.com/Documentation/5.9/ReferenceManuals/Designer/html/3383B12.htm To obtain all fields in an entity (you can specify if you want to receive inherited fields as well): https://www.llblgen.com/Documentation/5.9/ReferenceManuals/Designer/html/DBB572A4.htm there are more methods to your disposal of course, I just picked a few I think you might need.

After you've done all that, you can simply save your project and the attributes will be there, as well as generate code and the attributes should be generated into the code.

Be aware that the designer is a .net framework app, so a plugin has to be compiled for .net framework, not .net 6. To test code out, e.g. how to obtain all entities in a project, you can use the ElementSearch panel in the designer. This is a nice little REPL to traverse the object graph and see what works: https://www.llblgen.com/Documentation/5.9/Designer/Functionality%20Reference/ElementSearch.htm

Frans Bouma | Lead developer LLBLGen Pro
bhoatvolue
User
Posts: 6
Joined: 02-Sep-2022
# Posted on: 12-Oct-2022 09:57:22   

Very exhaustive and complete reply. Thank you! Now I should be able to achieve my goal. However, it would have been a nize detail if we had macros with arguments in attribute definition.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 12-Oct-2022 12:15:06   

bhoatvolue wrote:

Very exhaustive and complete reply. Thank you! Now I should be able to achieve my goal. However, it would have been a nize detail if we had macros with arguments in attribute definition.

True, the only problem with that is that it's currently a low-level string replace system and e.g. having $customProperty("name") macro will require a parsing of the string instead (to determine the name constant). So I agree it's a nice option, but likely not implemented very soon simple_smile

Frans Bouma | Lead developer LLBLGen Pro