Book a Demo

Author Topic: Naming problem when using transfomation to C#  (Read 6704 times)

Ophir Yoktan

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Naming problem when using transfomation to C#
« on: January 25, 2009, 07:54:13 pm »
Hi,

I'm trying to create a transformation between my abstract classes (language = <none>) to C# classes.
public attributes in the classes are converted to private fields, and public c# properties.
The names of the generated fields and properties are identical, and causes collisions.
Is it possible to generate the fields with some kind of prefix to avoid the problem?
(I assume editing the transformation templates might help, but I'm not sure where and how exactly)


«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Naming problem when using transfomation to C#
« Reply #1 on: January 25, 2009, 10:53:26 pm »
Hi Ophir,

Yes you can. Don't tear up the templates just yet, or at least not just for this.

Open the Tools | Options | Source Code Engineering dialog. Near the bottom you'll see a block of settings under the line of text beginning with "Remove prefixes..." Set up your attributes with the prefix and check the Property box. When EA generates code for the properties it will remove the prefix, avoiding the collision.

HTH, David
No, you can't have it!

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Naming problem when using transfomation to C#
« Reply #2 on: January 27, 2009, 08:25:06 am »
Of course, if you don't want to include the prefix in your original model, you will need to modify the attribute and connector templates to add a prefix into the name.

You would then need to modify the Attribute__Property and Linked Attribute templates to change the attribute_name tagged value to match up with the correct prefix.
« Last Edit: January 27, 2009, 08:26:41 am by simonm »

Ophir Yoktan

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Naming problem when using transfomation to C#
« Reply #3 on: February 01, 2009, 09:28:17 pm »
Quote
Of course, if you don't want to include the prefix in your original model, you will need to modify the attribute and connector templates to add a prefix into the name.

You would then need to modify the Attribute__Property and Linked Attribute templates to change the attribute_name tagged value to match up with the correct prefix.

This appears to be what I need, however I didn't find out the right syntax to change
%Property(attName,$type)%
to
%Property(prefix + attName,$type)%

Ophir

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8110
  • Karma: +119/-20
    • View Profile
Re: Naming problem when using transfomation to C#
« Reply #4 on: February 02, 2009, 08:01:48 am »
You'll need to explicitly assign the concatenated string to a variable.

For a literal string as the prefix.
$attName = "m_" + %attName%

For a variable as the prefix.
$attName = $prefix + %attName%

For both.
%Property($attName, $type)%

Ophir Yoktan

  • EA Novice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Naming problem when using transfomation to C#
« Reply #5 on: February 06, 2009, 12:34:18 am »
Thanks,

this adds a prefix to the property name.

What I want is to add a prefix to the member variable, and not the property name.

I tried doing a similar change in the Attribute template.
the names were correct, but the generated property code pointed to the unprefixed member name.

Ophir

philchudley

  • EA User
  • **
  • Posts: 750
  • Karma: +22/-0
  • EA Consultant / Trainer - Sparx Europe
    • View Profile
Re: Naming problem when using transfomation to C#
« Reply #6 on: February 07, 2009, 02:23:22 am »
My simple answer would be, if the property code is incorrect, then modify the Property macro to also use your prefix as in the Attribute  macro

Cheers
Models are great!
Correct models are even greater!

frik_stuart

  • EA Novice
  • *
  • Posts: 6
  • Karma: +0/-0
  • MDA is the future!
    • View Profile
Re: Naming problem when using transfomation to C#
« Reply #7 on: February 10, 2009, 12:20:24 pm »
There is another alternative. EA provides a mechanism for you to associate the property and underlying attribute, and lets you choose a name for the Property.

Do the following:
- Go to the Attributes dialog for you class and select the attribute that you want a property for.
- Check the "Property" checkbox. This will bring up a dialog called "Create Property Implementation"
- You can specify the name of the Property in the "Name" field and also specify whether you want Get and/or Set scope

Although you will have to do this for all your properties, it does provide better control than changing the generation templates which may cause you pain in future when you upgrade your EA version etc.

hroberts

  • EA Novice
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Re: Naming problem when using transfomation to C#
« Reply #8 on: February 14, 2009, 01:28:01 pm »
You can modify the property stereotype for the operation template and accomplish this.   I did this exact same thing for vb.net generation.

Matt Edwards

  • EA Novice
  • *
  • Posts: 17
  • Karma: +0/-0
    • View Profile
Re: Naming problem when using transfomation to C#
« Reply #9 on: March 12, 2009, 11:52:58 am »
Quote
Hi Ophir,

Yes you can. Don't tear up the templates just yet, or at least not just for this.

Open the Tools | Options | Source Code Engineering dialog. Near the bottom you'll see a block of settings under the line of text beginning with "Remove prefixes..." Set up your attributes with the prefix and check the Property box. When EA generates code for the properties it will remove the prefix, avoiding the collision.

HTH, David

This seems a little backward -- usually you only want to diagram the public interface, with property code being generated as it appears in the model. This setting should allow one to add a prefix to the private implementation field, which noone will ever see or care about.

«Midnight»

  • EA Guru
  • *****
  • Posts: 5651
  • Karma: +0/-0
  • That nice Mister Grey
    • View Profile
Re: Naming problem when using transfomation to C#
« Reply #10 on: March 12, 2009, 10:26:16 pm »
You could look at it that way. But try a different viewpoint and it might seem more natural...

You might want to ensure different names for the private (internal) variable name and the public property name. Some (many) languages enforce this by disallowing field and variables with the same name.

This way you can decide that the public interface will have 'natural' names - the ones you will show on a structural diagram during design. You can now 'denature' (for lack of a better term) these names for the internal code. The internal names will be private (or protected or whatever) and not (usually) shown on high-level design diagrams. The internal names should follow some kind of simple convention. EA allows you to do this via the settings we've discussed, then generates code in a predictable manner while conforming to the expectations set out above.

I'm neither supporting nor defending the EA implementation, just saying that it works.

One thing that (IMHO) could be improved is to add the ability to override it for each language. The current implementation would remain, becoming the default for cases where no explicit override was specified. [That means that there would have to be some way to explicitly clear the setting for each language.] This seems (again, IMHO) to be something that would fit well withing the paradigm that EA uses for language options.

David
No, you can't have it!