Way to build classes

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

Im make an application to register users...

This application first check if the user exists, and if not, add the user to
database.

I have:

------------- class database---------------:

- A method to verify if the user exists.
* if the user exists call a method that fire an event (UserExists event)
* if the user dont exists call a method that fire an event
(UsernotExists event)

- A method to add a user.
* IF the user was added call a method that fire an event(Useradded event)
* IF the user was added call a method that fire an event(Usernotadded
event)

----------------class User---------------------:

- this class is suscribed to the UserExists and UserDontExists events.
- this class is suscribed to the UserAdded and UsernotAdded events.

- A method called Manage User:
* Makes an instance of the database class, and call to the userexist
method

----------------------------------------------------

I would like to know what its the best way to do that, cause following this
way i follow, i need to have 4 events in the database class (user exists,
user dont exists, user added, user dont added), and comunicate this events
with the class user, and then have another 4 events for the user class to
comunicate these events to the WebForm.aspx.cs that its the page that makes
an instance of the User Class when the button of register its clicked.

Any advice would be grateful.
 
The key to your question is in the name of your "events". Events should
indicate actions or changes - i.e. past tense verbs.

"Exists" or "does not exist" is a yes/no fact, not a verb. Though "added"
is a verb, it is also a yes/no fact. Either way, you aren't really looking
to respond to that event, you're looking for a yes/no value indicating the
success of the add operation.

Because of these considerations, you should:

1. Consider using a boolean property for Exists and testing for, and
setting the value of, that property in your constructor.
2. Consider returning a boolean value from your AddUser method and
responding to the return value rather than to an event. In the AddUser
method, set the Exists property based on the success or failure of the
AddUser method.

HTH

DalePres
MCAD, MCDBA, MCSE
 
Hi DalePres,

Thanks for this clear explanation, i modify my application to use boolean
variables to see if the operation its completed or not, with a little
difference. I observed in some websites that its good to use for this kind of
stuff ref variables... And works perfect, a method of class can call to a
method in another class passing a ref bool variable to another method in
another class, and so on... This way when i make the first call to the method
passing the variable by reference, i can see if the operation was sucessfully
complete...

Thanks DalePres.
Kind Regards.
Josema.
DalePres said:
The key to your question is in the name of your "events". Events should
indicate actions or changes - i.e. past tense verbs.

"Exists" or "does not exist" is a yes/no fact, not a verb. Though "added"
is a verb, it is also a yes/no fact. Either way, you aren't really looking
to respond to that event, you're looking for a yes/no value indicating the
success of the add operation.

Because of these considerations, you should:

1. Consider using a boolean property for Exists and testing for, and
setting the value of, that property in your constructor.
2. Consider returning a boolean value from your AddUser method and
responding to the return value rather than to an event. In the AddUser
method, set the Exists property based on the success or failure of the
AddUser method.

HTH

DalePres
MCAD, MCDBA, MCSE
 
Back
Top