Sparx Systems Forum
Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: gavinbarton 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
-
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 db.AddInParameter(cmd, %paramName%, %DBType%, %paramName%);
You can then list over that template with %list="Parameter__Add" @separator="\n" @indent="\t"%
All that's left of that is how you've defined %DBType%.
-
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
-
Tagged values is the standard extension mechanism and can be accessed in the code templates with %opTag:"tagName"%.
-
thanks again.