Continuing the saga of my Web Service Interface project, I am now trying to find the best way of handling client-side errors. I think that I have almost completed the research phase, and the illustrated code all works as I intend, but before I continue with the development phase I want to check whether this is the best way of handling the situation so far.
Imagine this scenario: using MANASYS Jazz I have created a CICS (=mainframe) REST web service named MyJSv with a program JSPG2. This follows the Swagger standard making it easy to invoke with SOAPUI, Postman, or other test utilities, but for various reasons the JSON schemas lack a full description of the interface. For example in the schema Function is a string of any length, actually it must have one of the values "E", "U", "A", or "D" meaning "Enquiry", "Update", etc., and EMPNO is any text input, but actually must be an integer in the range 1 to 999999. Now YOU want to consume this service in your program, which may be a web page, windows form, mobile app, ... written in C#, VB, or whatever. To make it easy for you I have provided an interface that hides as much as possible of the complexity of JSPG2 from you, and builds in the full rules of the interface. Just as a simple dialog created the web service program MyJsv/JSPG2, a few clicks in a MANASYS Jazz dialog has created an interface for JSPG2 into YOUR project, creating a folder MyJSV into this project with various objects relating to service MyJSv and operation JSPG2.
Here is the structure of the research project, WindowsApp, so far. Imagine that everything in folder MyJSv has been generated by MANASYS Jazz, and you have written the rest (Form1). I expect that after R&D MyJSv will be a separate project within your solution, not a folder within the same project, allowing your part to be written in languages other than C#, and for the MyJSv project to be used in several different solutions.
WindowsApp has a form Form1.cs with a textbox for entering a value of EMPNO, a label for displaying messages, and a button called btnEnquiry with this code: -
With a valid EMPNO value this works, and you see the message "Response is in ojspg2" (which debugging confirms). When you enter "xxx" into txtEmpno.text the error was detected and message "Jazz Data Error, EMPNO:Value is not numeric" displayed.
JazzDataExceptions are produced from this code in JSPG2Client.cs: -
So far so good. But the UI would be better if the error were reported when the value of EMPNO were entered, so that you repeat the try/catch code in a txtEmpno event handler: -
Now the error is trapped when it is easiest to correct.
The research project WindowsApp has dealt with a single function (Enquiry) with only a couple of fields, each with simple validation requirements. In the production situation the interface might deal with a few dozen input and output fields, some of which may have complex validation requirements like check digits and pattern validation. There may be several different types of function (Enquiry, Update, Add, Delete, New Order, Process Order ...). There are rules about when functions are valid, for example an Update must follow an enquiry. There may be conversational requirements: authentication is the obvious one, but there might be requirements about a conversation sequence. WIth this in mind: -
Questions.
Imagine this scenario: using MANASYS Jazz I have created a CICS (=mainframe) REST web service named MyJSv with a program JSPG2. This follows the Swagger standard making it easy to invoke with SOAPUI, Postman, or other test utilities, but for various reasons the JSON schemas lack a full description of the interface. For example in the schema Function is a string of any length, actually it must have one of the values "E", "U", "A", or "D" meaning "Enquiry", "Update", etc., and EMPNO is any text input, but actually must be an integer in the range 1 to 999999. Now YOU want to consume this service in your program, which may be a web page, windows form, mobile app, ... written in C#, VB, or whatever. To make it easy for you I have provided an interface that hides as much as possible of the complexity of JSPG2 from you, and builds in the full rules of the interface. Just as a simple dialog created the web service program MyJsv/JSPG2, a few clicks in a MANASYS Jazz dialog has created an interface for JSPG2 into YOUR project, creating a folder MyJSV into this project with various objects relating to service MyJSv and operation JSPG2.
Here is the structure of the research project, WindowsApp, so far. Imagine that everything in folder MyJSv has been generated by MANASYS Jazz, and you have written the rest (Form1). I expect that after R&D MyJSv will be a separate project within your solution, not a folder within the same project, allowing your part to be written in languages other than C#, and for the MyJSv project to be used in several different solutions.
WindowsApp has a form Form1.cs with a textbox for entering a value of EMPNO, a label for displaying messages, and a button called btnEnquiry with this code: -
C#:
private void btnEnquire_Click(object sender, EventArgs e)
{
try
{
jspg2Client.Function = "E";
jspg2Client.EMPNO = txtEmpno.Text;
ojspg2 = jspg2Client.Post();
lblMessage.Text = "Response is in ojspg2";
}
catch (MyJSv.JazzDataException ex)
{
lblMessage.Text = "Jazz Data Error, " + ex.FieldName + ":" + ex.Message;
}
}
JazzDataExceptions are produced from this code in JSPG2Client.cs: -
C#:
public class JSPG2Client
{
...
// Web Service Input Properties - Visible request message
...
private string _EMPNO = null;
public string EMPNO // => RequestJSPG2.JZ_Employee.EMPNO
{
get => _EMPNO;
set
{
_EMPNO = null;
_EMPNO = IsInt(value, 0, 999999, "EMPNO");
ijspg2.JSPG2.IJSPG2.JZ_Employee.EMPNO = _EMPNO;
}
}
// Functions used to check input properties
...
private string IsInt(string Value, int MinVal, int maxval, string FieldName)
{
int IX = 0;
if (!int.TryParse(Value, out IX))
{
throw new JazzDataException("Value is Not Numeric", FieldName);
}
if (IX < MinVal || IX > maxval)
{
throw new JazzDataException("Value out of range", FieldName);
}
return Value;
}
C#:
private void txtEmpno_TextChanged(object sender, EventArgs e)
{
lblMessage.Text = "";
try
{
jspg2Client.EMPNO = txtEmpno.Text;
}
catch (MyJSv.JazzDataException ex)
{
lblMessage.Text = "Jazz Data Error, " + ex.FieldName + ":" + ex.Message;
}
}
The research project WindowsApp has dealt with a single function (Enquiry) with only a couple of fields, each with simple validation requirements. In the production situation the interface might deal with a few dozen input and output fields, some of which may have complex validation requirements like check digits and pattern validation. There may be several different types of function (Enquiry, Update, Add, Delete, New Order, Process Order ...). There are rules about when functions are valid, for example an Update must follow an enquiry. There may be conversational requirements: authentication is the obvious one, but there might be requirements about a conversation sequence. WIth this in mind: -
Questions.
- Within MyJSv I want to generate the best code possible. Can the validation code above be improved?
- You've discovered MyJSv / JSPG2. How do you want to find out how to use it? You'll need to know what the functions are, the rules about any sequence requirements, and the rules of the fields in their input and output messages? As developer of MANASYS Jazz there is no problem about providing Help for the function "Generate Web Service Interface", but I have to think about the tools that I need to provide to the user of this function, who generates MyJSv/JSPG2 and its interface and provides the documentation that YOU, the consumer of the web service, needs. The developer of MyJSv/JSPG2 is, in theory, somebody who works for a mainframe user (bank etc). I'd like this developer to be able to provide Help in the form that you want it, perhaps help functions for JSPG2Client, and for each of its properties and methods, not just a web page that you have to look up.
- Currently output data is returned with
C#:
ojspg2 = jspg2Client.Post();