I want to find out the best way for a method to notify calling code of
situations such as validation errors, etc. that may occur during method
execution.
e.g. say I have a method (business logic layer) that inserts a user into a
database, and returns the newly created UserID:
public function CreateUser(userName as string, userEmail as string)
dim userID as integer
'Insert user details into Users table in database
'Set userID
return userID
end function
Two possible conditions must be handled:
1. The user name already exists in the database
2. The user email already exists in the database
What is the recommended way of reporting these conditions to the calling
code?
Maybe:
1. Create custom Exceptions such as NameClashException, EmailClashException.
The CreateUser function will throw the appropriate exception if any of
these situations occur.
The calling code (presentation layer) can then catch these exceptions
and notify the user as appropriate
2. Return Status enum.
The calling code can then examine the status enum and notify the user as
appropriate.
However, another way must be found for returning the userID, e.g. pass
ByRef:
public enum CreateUserStatus
Success
NameClash
EmailClash
end enum
public function CreateUser(byRef UserID as integer, userName as string,
userEmail as string)
dim userID as integer
dim status as CreateUserStatus
'Insert user details into Users table in database
'Set status and userID
return status
end function
I have read in MS docs that option 1 is not recommended as exceptions should
only be thrown if the assumptions of the method are breached.
A clashing name or email is therefore not really an exception. Also, new
exception classes would need to be created for every possible status apart
from success.
Option 2 seems messy since it makes sense for a CreateUser method to return
just the newly created user ID.
I want to find the optimal solution for this scenario and standardise my
method calls across the application to this solution.
Any ideas?
Hope this is clear.
- Sean. 3 2709
Sean,
Duplicate keys in the database sounds like Exceptions to me.
I would avoid your #2 as it is far too easy to ignore the return values.
I would consider combining both methods, and have a ClashException that
accepted either a status or a field name of what clashed, I would possible
either use or inherit from ArgumentException for this.
Throw New ArgumentException("The user name already exists in the database,
"userName")
Throw New ArgumentException("The user email already exists in the database,
"userEmail")
or
Throw New ClashException(ClashStatus.Email)
Throw New ClashException(ClashStatus.Name)
Also I would verify there was not a clash before I called the CreateUser
function, for example, when the user typed in the User name, I would
validate that the user name was distinct. When the user typed in the Email
name I would verify that the email name was distinct.
Hope this helps
Jay
"Sean Tynan" <se*****@eircom.REMOVE.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... I want to find out the best way for a method to notify calling code of situations such as validation errors, etc. that may occur during method execution.
e.g. say I have a method (business logic layer) that inserts a user into a database, and returns the newly created UserID:
public function CreateUser(userName as string, userEmail as string) dim userID as integer
'Insert user details into Users table in database 'Set userID
return userID end function
Two possible conditions must be handled:
1. The user name already exists in the database 2. The user email already exists in the database
What is the recommended way of reporting these conditions to the calling code?
Maybe:
1. Create custom Exceptions such as NameClashException,
EmailClashException. The CreateUser function will throw the appropriate exception if any
of these situations occur. The calling code (presentation layer) can then catch these exceptions and notify the user as appropriate
2. Return Status enum. The calling code can then examine the status enum and notify the user
as appropriate. However, another way must be found for returning the userID, e.g. pass ByRef:
public enum CreateUserStatus Success NameClash EmailClash end enum
public function CreateUser(byRef UserID as integer, userName as string, userEmail as string) dim userID as integer dim status as CreateUserStatus
'Insert user details into Users table in database 'Set status and userID
return status end function
I have read in MS docs that option 1 is not recommended as exceptions
should only be thrown if the assumptions of the method are breached. A clashing name or email is therefore not really an exception. Also, new exception classes would need to be created for every possible status apart from success.
Option 2 seems messy since it makes sense for a CreateUser method to
return just the newly created user ID.
I want to find the optimal solution for this scenario and standardise my method calls across the application to this solution.
Any ideas?
Hope this is clear.
- Sean.
Thanks Jay.
I think what you outlined is the most concise way of doing it.
Do you think other situations like invalid logons, missing data, data that
exceed maximum length, etc.
also fit into this approach?
Throw New AccountExpiredException()
Throw New MissingDataException("Phone")
Throw New MaximumLengthExceededException("Address")
I guess it all depends on whether these situations are considered
exceptions. Of course there is a danger here of using the CLR's exception
handling as a kind of communication process for transmitting data other than
return values between method calls. Still, the simplicity of this approach
is appealing.
Thanks again.
- Sean
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uK**************@TK2MSFTNGP10.phx.gbl... Sean, Duplicate keys in the database sounds like Exceptions to me.
I would avoid your #2 as it is far too easy to ignore the return values.
I would consider combining both methods, and have a ClashException that accepted either a status or a field name of what clashed, I would possible either use or inherit from ArgumentException for this.
Throw New ArgumentException("The user name already exists in the database, "userName") Throw New ArgumentException("The user email already exists in the
database, "userEmail")
or
Throw New ClashException(ClashStatus.Email) Throw New ClashException(ClashStatus.Name)
Also I would verify there was not a clash before I called the CreateUser function, for example, when the user typed in the User name, I would validate that the user name was distinct. When the user typed in the Email name I would verify that the email name was distinct.
Hope this helps Jay
"Sean Tynan" <se*****@eircom.REMOVE.net> wrote in message news:%2****************@tk2msftngp13.phx.gbl... I want to find out the best way for a method to notify calling code of situations such as validation errors, etc. that may occur during method execution.
e.g. say I have a method (business logic layer) that inserts a user into
a database, and returns the newly created UserID:
public function CreateUser(userName as string, userEmail as string) dim userID as integer
'Insert user details into Users table in database 'Set userID
return userID end function
Two possible conditions must be handled:
1. The user name already exists in the database 2. The user email already exists in the database
What is the recommended way of reporting these conditions to the calling code?
Maybe:
1. Create custom Exceptions such as NameClashException, EmailClashException. The CreateUser function will throw the appropriate exception if any of these situations occur. The calling code (presentation layer) can then catch these
exceptions and notify the user as appropriate
2. Return Status enum. The calling code can then examine the status enum and notify the
user as appropriate. However, another way must be found for returning the userID, e.g.
pass ByRef:
public enum CreateUserStatus Success NameClash EmailClash end enum
public function CreateUser(byRef UserID as integer, userName as string, userEmail as string) dim userID as integer dim status as CreateUserStatus
'Insert user details into Users table in database 'Set status and userID
return status end function
I have read in MS docs that option 1 is not recommended as exceptions should only be thrown if the assumptions of the method are breached. A clashing name or email is therefore not really an exception. Also, new exception classes would need to be created for every possible status
apart from success.
Option 2 seems messy since it makes sense for a CreateUser method to return just the newly created user ID.
I want to find the optimal solution for this scenario and standardise my method calls across the application to this solution.
Any ideas?
Hope this is clear.
- Sean.
Sean,
Unfortunately there is no easy answer.
Missing Data and Maximum Length Exceeded sounds like Validation to me, I
would use exceptions for Validation per se. I would use the Validating event
for Validation.
However it can be advisable to "validate" your parameters when you call a
method of an Object, especially if you do not know who is calling the
object, in this case I would raise exceptions...
Hope this helps
Jay
"Sean Tynan" <st****@NO--SPAM.missionmaker.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... Thanks Jay.
I think what you outlined is the most concise way of doing it.
Do you think other situations like invalid logons, missing data, data that exceed maximum length, etc.
also fit into this approach?
Throw New AccountExpiredException()
Throw New MissingDataException("Phone")
Throw New MaximumLengthExceededException("Address")
I guess it all depends on whether these situations are considered exceptions. Of course there is a danger here of using the CLR's exception handling as a kind of communication process for transmitting data other
than return values between method calls. Still, the simplicity of this approach is appealing.
Thanks again.
- Sean
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message news:uK**************@TK2MSFTNGP10.phx.gbl... Sean, Duplicate keys in the database sounds like Exceptions to me.
I would avoid your #2 as it is far too easy to ignore the return values.
I would consider combining both methods, and have a ClashException that accepted either a status or a field name of what clashed, I would
possible either use or inherit from ArgumentException for this.
Throw New ArgumentException("The user name already exists in the
database, "userName") Throw New ArgumentException("The user email already exists in the database, "userEmail")
or
Throw New ClashException(ClashStatus.Email) Throw New ClashException(ClashStatus.Name)
Also I would verify there was not a clash before I called the CreateUser function, for example, when the user typed in the User name, I would validate that the user name was distinct. When the user typed in the
Email name I would verify that the email name was distinct.
Hope this helps Jay
"Sean Tynan" <se*****@eircom.REMOVE.net> wrote in message news:%2****************@tk2msftngp13.phx.gbl... I want to find out the best way for a method to notify calling code of situations such as validation errors, etc. that may occur during
method execution.
e.g. say I have a method (business logic layer) that inserts a user
into a database, and returns the newly created UserID:
public function CreateUser(userName as string, userEmail as string) dim userID as integer
'Insert user details into Users table in database 'Set userID
return userID end function
Two possible conditions must be handled:
1. The user name already exists in the database 2. The user email already exists in the database
What is the recommended way of reporting these conditions to the
calling code?
Maybe:
1. Create custom Exceptions such as NameClashException, EmailClashException. The CreateUser function will throw the appropriate exception if
any of these situations occur. The calling code (presentation layer) can then catch these exceptions and notify the user as appropriate
2. Return Status enum. The calling code can then examine the status enum and notify the user as appropriate. However, another way must be found for returning the userID, e.g. pass ByRef:
public enum CreateUserStatus Success NameClash EmailClash end enum
public function CreateUser(byRef UserID as integer, userName as
string, userEmail as string) dim userID as integer dim status as CreateUserStatus
'Insert user details into Users table in database 'Set status and userID
return status end function
I have read in MS docs that option 1 is not recommended as exceptions should only be thrown if the assumptions of the method are breached. A clashing name or email is therefore not really an exception. Also,
new exception classes would need to be created for every possible status apart from success.
Option 2 seems messy since it makes sense for a CreateUser method to
return just the newly created user ID.
I want to find the optimal solution for this scenario and standardise
my method calls across the application to this solution.
Any ideas?
Hope this is clear.
- Sean.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: FLEB |
last post by:
I have a function which should convey two things upon its completion.
First, the return value of the function, consisting of some text. Second,
it should convey an "error status", which, could be...
|
by: Albert Tollkuçi |
last post by:
I have created a class ValidationResult which
looks like:
class ValidationResult
{
ValidationStatus Status;
ValidationItemCollection Items;
}
The ValidationItem is defined as below:
class...
|
by: Blaise Garant |
last post by:
Hi
I've made a stylesheet to transform my data into XSL-FO. This
stylesheet used to work with MSXSL 4.0 but I've got some issues in
..NET. First, I changed removed all the "node-set()" function...
|
by: tshad |
last post by:
I have class method that is passing back a boolean, but it always returns a
false;
The class is:
***************************************************************************
public bool Delete()...
|
by: Boltar |
last post by:
Hello,
I am trying to use a CustomValidator control to perform client-side
validation via a server callback.
I have an example below that validates if a textbox contains an odd
number.
I...
|
by: mabster |
last post by:
Hi gang,
I have a web service class I'm developing along the same lines as that
described here in MSDN:
http://tinyurl.com/63sl4
...
|
by: roger.dunham |
last post by:
Hi there,
I am writing an application that performs calculations on records
within a data table. There may be many records in a data table.
There are situations where the calculation may not...
|
by: Darren |
last post by:
Is there a way to determine a link to a file is valid (exists)?
I have found examples using httprequest, but I don't want to retrieve the
entire file, just verify it's existence.
These examples...
|
by: Bryan |
last post by:
I want my business objects to be able to do this:
class Person(base):
def __init__(self):
self.name = None
@base.validator
def validate_name(self):
if not self.name: return
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| | |