Book a Demo

Author Topic: Creating a new template in C# code  (Read 5663 times)

Cleber.Hans

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Creating a new template in C# code
« on: April 18, 2012, 03:28:01 am »
Good afternoon.
I need to know how to create a specific template to generate the following codes to standard output in C #.

public class Pais{
public int PaisId{get; set;}
public string Nome{get; set;}
}

Thanks
« Last Edit: April 18, 2012, 03:28:26 am by CleberHans »

g.makulik

  • EA User
  • **
  • Posts: 355
  • Karma: +0/-0
    • View Profile
Re: Creating a new template in C# code
« Reply #1 on: April 18, 2012, 07:42:30 am »
Hi Hans,

It's a bit unclear for me, what exctly you want to know.

a) How to write a code generation template that generates this code
b) How to model a class that EA c# standard templates generate this
    code.

For a) I would recommend to have a look at the C# code generation templates.
If it's b) that should be fairly easy. Create a class named 'Pais' on a class diagram and give it the attributes 'PaisId' and 'Nome'. Change the attributes' stereotype to <<Property>>.
Ensure that the classes language is set to C#, and start code generation.

HTH
Günther
Using EA9.3, UML2.3, C++, linux, my brain, http://makulik.github.com/sttcl/

Cleber.Hans

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Creating a new template in C# code
« Reply #2 on: April 18, 2012, 08:00:07 am »
Gunter, good evening!

[size=12]What I want is to generate automatic properties in C #[/size].
For example, observe the class Country:

public class Pais {

      private string nomePais;
      private string siglaPais;
}//end Pais

The code should be:

public class Pais {

      public NomePais {get; set;}
      public string SiglaPais{get; set;};
}//end Pais

I have no idea how to generate this code.

Thanks
« Last Edit: April 18, 2012, 08:02:44 am by CleberHans »

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13523
  • Karma: +574/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: Creating a new template in C# code
« Reply #3 on: April 18, 2012, 03:32:00 pm »
Isn't there somewhere a checkbox that you can set to indicate that an attribute is a property?

Geert



KP

  • EA Administrator
  • EA Expert
  • *****
  • Posts: 2919
  • Karma: +55/-3
    • View Profile
Re: Creating a new template in C# code
« Reply #4 on: April 18, 2012, 04:44:40 pm »
The {get; set;} syntax can be achieved by giving an attribute the «property» stereotype. Don't click the "Property" checkbox for the attribute because that will give you the longhand syntax.
The Sparx Team
[email protected]

Cleber.Hans

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Creating a new template in C# code
« Reply #5 on: April 18, 2012, 11:05:25 pm »
Hi KP, thanks a lot for your answer.
I know your answer is correct, but let me replace my problem:

I would like the standard output (get set) generates the system was placed on the same line and PascalCasing rating.

public NomePais {get; set;}
 
That's it.
« Last Edit: April 18, 2012, 11:06:32 pm by CleberHans »

Makulik

  • EA User
  • **
  • Posts: 400
  • Karma: +0/-0
    • View Profile
Re: Creating a new template in C# code
« Reply #6 on: April 18, 2012, 11:56:41 pm »
Hi Hans,

Under 'Tools->Options->Source Code Engineering' there's a checkbox named 'Captialized Attribute Names for Properties'. Did you already try that?
If this won't help you would really need to adapt the C# Code Generation Templates accordingly. Have a look at the 'Attribute Declaration' template.
When I was looking at it I've also seen that it expects a tagged value 'Automatic' at the attribute to generate C# auto field properties.

HTH
Günther

Cleber.Hans

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Creating a new template in C# code
« Reply #7 on: April 19, 2012, 01:49:18 am »
Hi Gunther,
Their response worked.

      /// <summary>
      /// Designa o nome da cidade.
      /// </summary>
      public string NomeCidade{
            get{
                  return nomeCidade;
            }
            set{
                  nomeCidade = value;
            }
      }

Now I need to customize the output to remove
the code: (return nomeCidade and nomeCidade = value)

Thanks

g.makulik

  • EA User
  • **
  • Posts: 355
  • Karma: +0/-0
    • View Profile
Re: Creating a new template in C# code
« Reply #8 on: April 19, 2012, 02:48:44 am »
Quote

Now I need to customize the output to remove
the code: (return nomeCidade and nomeCidade = value)

I think that's achievable using the tagged value I mentioned ...

Günther

PS: As a hint I want to advise you really to have a look at the standard code generation templates, if you want to use code generation extensively. You should get a good grip, what might be generated exactly and the dependencies from code generation options, stereotypes and tagged values. You can access the templates using 'Settings->Code Generation Templates' and choose C# in the Language drop down.
You can find more information about the conditions used here:
http://www.sparxsystems.com/enterprise_architect_user_guide/9.3/standard_uml_models/fieldsubstitutionmacros.html
« Last Edit: April 19, 2012, 03:00:23 am by g.makulik »
Using EA9.3, UML2.3, C++, linux, my brain, http://makulik.github.com/sttcl/

Cleber.Hans

  • EA Novice
  • *
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Creating a new template in C# code
« Reply #9 on: April 19, 2012, 03:14:07 am »
Gunther, You're really fantastic. I study and I will post soon.

Again, thanks a lot.

Hans