Author Topic: Code Template  (Read 4025 times)

gavinbarton

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Code Template
« on: July 11, 2007, 12:30:13 pm »
I have been using the CTF in EA 7 to create code however I have hit a wall with looping and parameters. I would like to create a code template that utilises the parameters defined in an operation.  

For example, I have an operation defined as:

public DataSet GetName(string Name, string Address)
{

   Database db = DatabaseFactory.CreateDatabase();
   DbCommand cmd = db.GetStoredProcCommand("search");

   db.AddInParameter(cmd, "Name", DbType.String, Name);
   db.AddInParameter(cmd, "Address", DbType.String, Address);

   return db.ExecuteDataSet(cmd);
}

The built in templates produce the following:

public DataSet GetName(string Name, string Address)
{
   return null;
}

I can easily add in:

public DataSet GetName(string Name, string Address)
{

   Database db = DatabaseFactory.CreateDatabase();
   DbCommand cmd = db.GetStoredProcCommand("search");

   return db.ExecuteDataSet(cmd);
}

I would like to create a CTF operation template to process the parameters so that for each parameter it adds:

   db.AddInParameter(cmd, %ParamName%, %DBType%, %ParamName%);

The template for the method would be:

public DataSet OperationName(params)
{
   Database db = DatabaseFactory.CreateDatabase();
   DbCommand cmd = db.GetStoredProcCommand(%SP_NAME%);

   foreach(param in params)
   {
      db.AddInParameter(cmd, %ParamName%, %DBType%, %ParamName%);
   }

   return db.ExecuteDataSet(cmd);

}


In the EA guide an example uses %list="Parameter" @separator=", "% to fetch all of the parameters but there appears to be no looping mechanism to loop through each parameter so that I can:
1) Work out the type
2) Grab the name

The second thing that I would like to work out is if there is a way to pass in variables for a particular operation.  For example the operation could have a stored procedure naame associated to it.

Any help with the above would be greatly appreciated.

Thanks,
Gavin



« Last Edit: July 11, 2007, 12:30:40 pm by gavinbarton »

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8085
  • Karma: +118/-20
    • View Profile
Re: Code Template
« Reply #1 on: July 11, 2007, 12:35:43 pm »
This is the situation that custom templates are for.

http://www.sparxsystems.com.au/EAUserGuide/index.html?customtemplates.htm

Create a template of type parameter called Add.  This creates a template Parameter__Add that you can enter
Code: [Select]
db.AddInParameter(cmd, %paramName%, %DBType%, %paramName%);

You can then list over that template with
Code: [Select]
%list="Parameter__Add" @separator="\n" @indent="\t"%

All that's left of that is how you've defined %DBType%.

gavinbarton

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Code Template
« Reply #2 on: July 11, 2007, 01:17:25 pm »
Thanks that worked a treat.

Is there anyway in store additional custom properties on an 0peration.  In my example I have a stored procedure name, if I can set it as a custom property then the template can pick it up and included it.

I look at the alias but I dont think this would be suitable.

Thanks for the help

Eve

  • EA Administrator
  • EA Guru
  • *****
  • Posts: 8085
  • Karma: +118/-20
    • View Profile
Re: Code Template
« Reply #3 on: July 11, 2007, 01:43:58 pm »
Tagged values is the standard extension mechanism and can be accessed in the code templates with %opTag:"tagName"%.

gavinbarton

  • EA Novice
  • *
  • Posts: 4
  • Karma: +0/-0
  • I love YaBB 1G - SP1!
    • View Profile
Re: Code Template
« Reply #4 on: July 11, 2007, 02:07:40 pm »
thanks again.