473,714 Members | 3,077 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Your opinion please

I'm currently creating a database class, which contains a member called
Open(). With this method users can open different databases. When a user
tries to open a database which happens to be secured with useraccounts
(username, password etc), the user should be notifed to login.

Now, in my opinion their are 3 ways to cope with situation:
1. Exit the method nicely with an errorcode (eg access denied, specify
accountinformat ion and try again)
2. Fire an exception
3. Fire an event (eg a LoginEvent, with cancel option) and wait until the
user specifies his username, password
etc within an exceptionhandle r. This means that the Open() must wait
until the user responses.

I'm not looking for a good solution, but I'm more interested in opions with
good arguments.

Is there anybody out there who´s willing to share his opinion?

thnx

DD

Nov 28 '06 #1
20 1599
number one is the way to go.
2 is not applicable because the situation you describe is an expected one.
3. Is too complex for a beginner, and probably overkill anyway.

Best of luck.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"C# Beginner" wrote:
I'm currently creating a database class, which contains a member called
Open(). With this method users can open different databases. When a user
tries to open a database which happens to be secured with useraccounts
(username, password etc), the user should be notifed to login.

Now, in my opinion their are 3 ways to cope with situation:
1. Exit the method nicely with an errorcode (eg access denied, specify
accountinformat ion and try again)
2. Fire an exception
3. Fire an event (eg a LoginEvent, with cancel option) and wait until the
user specifies his username, password
etc within an exceptionhandle r. This means that the Open() must wait
until the user responses.

I'm not looking for a good solution, but I'm more interested in opions with
good arguments.

Is there anybody out there who´s willing to share his opinion?

thnx

DD

Nov 28 '06 #2
Peter is correct, as is his opinion. :)

The first option makes sense: you try to open something that you know
may or may not open, and the result indicates the status of the
operation. Since you may or may not be able to open the database,
TryOpen() may be more morally correct, but since the Try- prefix on
method names already means something subtly different, and Open()
already means what you're trying to do, Open(), as you had it, is
probably the best name too.

The second option involves an exception. As Peter stated, nothing
exceptional has happened. Exceptions are to be reserved for things like
users unplugging USB devices while we're accessing them, or disks not
having enough space to write log or temporary files. Throwing an
exception carries overhead; while overhead isn't something to be
eliminated, this case doesn't even follow the exception metaphor, so
it's purely wasted overhead.

Stating that the third is "too complicated" may be an understatement.
There are times and places when you want very, very heavyweight objects
that do things like fire events and wait for results. The only two I
can think of off the top of my head are either where the user is very
close to the object model or where the system has some ability to cope
with things like missing information and broken connections.

The first case is for things like APIs and user-programmable systems.
In these cases, you sometimes want "wrapper objects" around your core
objects. The wrappers make the API more like a UI, which is appropriate
in those circumstances. I'd have a hard time believing that this is the
best model for any API, however.

The second case is for an "intelligen t" system. I've designed systems
that have things like defaults and base cases. If you tell them
something, they try different permutations until either something
works, or they run out of options. There are many ways to do this, but
one is to fire an event and wait for more information. For instance, if
you have a default username and password, you can fire an event, wait
for a timeout, then try the default if nothing's been received. This is
a highly complex system model and good design specifies that you never
do this unless there's a really good reason to do so.

Go with the simplest door number one.
Stephan

Peter wrote:
number one is the way to go.
2 is not applicable because the situation you describe is an expected one.
3. Is too complex for a beginner, and probably overkill anyway.

Best of luck.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"C# Beginner" wrote:
I'm currently creating a database class, which contains a member called
Open(). With this method users can open different databases. When a user
tries to open a database which happens to be secured with useraccounts
(username, password etc), the user should be notifed to login.

Now, in my opinion their are 3 ways to cope with situation:
1. Exit the method nicely with an errorcode (eg access denied, specify
accountinformat ion and try again)
2. Fire an exception
3. Fire an event (eg a LoginEvent, with cancel option) and wait until the
user specifies his username, password
etc within an exceptionhandle r. This means that the Open() must wait
until the user responses.

I'm not looking for a good solution, but I'm more interested in opions with
good arguments.

Is there anybody out there who´s willing to share his opinion?

thnx

DD




Nov 28 '06 #3
thnx ssamuel and peter

The reason I asked for opions is that I'm, as my name states, a C# beginner.
Though I'm learning the language very quickly, I seem to have difficulties
with drawing a line between a class and a program. Which tasks should be
handled by the class and which ones should be handled by the program. Should
login attempts be handled by the class or handled by the program? etc.

I thought by using an event, the class could take control over the situation
(just give me a name and password and I handle it) intead of shoving the
problem to the program (access denied, solve it yourself), as I have
experienced using VB6 trying to open a secured database.

I've read a lot of books and I've learned to create execeptions, events &
delegates and so, but I never read a book explaining to me in which
situations I should use exceptions or events (I'm refering to dutch books).

Though solutions are good but I'm more interested in opinions because it's
the best way to learn.

thnx again

DD


"ssamuel" <ss*****@gmail. comschreef in bericht
news:11******** *************@1 4g2000cws.googl egroups.com...
Peter is correct, as is his opinion. :)

The first option makes sense: you try to open something that you know
may or may not open, and the result indicates the status of the
operation. Since you may or may not be able to open the database,
TryOpen() may be more morally correct, but since the Try- prefix on
method names already means something subtly different, and Open()
already means what you're trying to do, Open(), as you had it, is
probably the best name too.

The second option involves an exception. As Peter stated, nothing
exceptional has happened. Exceptions are to be reserved for things like
users unplugging USB devices while we're accessing them, or disks not
having enough space to write log or temporary files. Throwing an
exception carries overhead; while overhead isn't something to be
eliminated, this case doesn't even follow the exception metaphor, so
it's purely wasted overhead.

Stating that the third is "too complicated" may be an understatement.
There are times and places when you want very, very heavyweight objects
that do things like fire events and wait for results. The only two I
can think of off the top of my head are either where the user is very
close to the object model or where the system has some ability to cope
with things like missing information and broken connections.

The first case is for things like APIs and user-programmable systems.
In these cases, you sometimes want "wrapper objects" around your core
objects. The wrappers make the API more like a UI, which is appropriate
in those circumstances. I'd have a hard time believing that this is the
best model for any API, however.

The second case is for an "intelligen t" system. I've designed systems
that have things like defaults and base cases. If you tell them
something, they try different permutations until either something
works, or they run out of options. There are many ways to do this, but
one is to fire an event and wait for more information. For instance, if
you have a default username and password, you can fire an event, wait
for a timeout, then try the default if nothing's been received. This is
a highly complex system model and good design specifies that you never
do this unless there's a really good reason to do so.

Go with the simplest door number one.
Stephan

Peter wrote:
number one is the way to go.
2 is not applicable because the situation you describe is an expected one.
3. Is too complex for a beginner, and probably overkill anyway.

Best of luck.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"C# Beginner" wrote:
I'm currently creating a database class, which contains a member called
Open(). With this method users can open different databases. When a user
tries to open a database which happens to be secured with useraccounts
(username, password etc), the user should be notifed to login.

Now, in my opinion their are 3 ways to cope with situation:
1. Exit the method nicely with an errorcode (eg access denied, specify
accountinformat ion and try again)
2. Fire an exception
3. Fire an event (eg a LoginEvent, with cancel option) and wait until
the
user specifies his username, password
etc within an exceptionhandle r. This means that the Open() must wait
until the user responses.

I'm not looking for a good solution, but I'm more interested in opions
with
good arguments.

Is there anybody out there who´s willing to share his opinion?

thnx

DD



Nov 28 '06 #4
Hi,

I prefer option #4 :)

As a library method, if Open is going to be used against databases that
could potentially, but not necessarily, require credentials, I think it
should have two overloads: one that takes credentials and one that doesn't.
When credentials are required, the one that doesn't accept them should
thrown an InvalidOperatio nException. When credentials aren't required but
are supplied anyway, either ignore them or throw an
InvalidOperatio nException, just to be safe.

You should provide a method or property that allows the calling code to
check whether credentials are required so that it may prompt the user for
them, if necessary, and avoid an exception. A method named,
"RequiresCreden tials" or a property named, "CredentialsReq uired" sounds
reasonable to me.

--
Dave Sexton

"C# Beginner" <de********@ora nge.nlwrote in message
news:45******** *************** @news.wanadoo.n l...
I'm currently creating a database class, which contains a member called
Open(). With this method users can open different databases. When a user
tries to open a database which happens to be secured with useraccounts
(username, password etc), the user should be notifed to login.

Now, in my opinion their are 3 ways to cope with situation:
1. Exit the method nicely with an errorcode (eg access denied, specify
accountinformat ion and try again)
2. Fire an exception
3. Fire an event (eg a LoginEvent, with cancel option) and wait until the
user specifies his username, password
etc within an exceptionhandle r. This means that the Open() must wait
until the user responses.

I'm not looking for a good solution, but I'm more interested in opions
with good arguments.

Is there anybody out there who´s willing to share his opinion?

thnx

DD



Nov 28 '06 #5
Dave Sexton <dave@jwa[remove.this]online.comwrote :
I prefer option #4 :)

As a library method, if Open is going to be used against databases that
could potentially, but not necessarily, require credentials, I think it
should have two overloads: one that takes credentials and one that doesn't.
When credentials are required, the one that doesn't accept them should
thrown an InvalidOperatio nException. When credentials aren't required but
are supplied anyway, either ignore them or throw an
InvalidOperatio nException, just to be safe.

You should provide a method or property that allows the calling code to
check whether credentials are required so that it may prompt the user for
them, if necessary, and avoid an exception. A method named,
"RequiresCreden tials" or a property named, "CredentialsReq uired" sounds
reasonable to me.
This is almost exactly what I was going to post. Special return codes
almost always feel wrong to me, and this feels like a much better
solution to me.

(With regards to the overhead of exceptions, I'd say it should be
regarded as completely irrelevant - the cost of talking to a database
is going to make the overhead of throwing an exception utterly
insignificant. The cost of throwing exceptions should only be seriously
considered when the method in question could reasonably be called
*very* frequently. That doesn't mean that exceptions are always
appropriate to use, of course - but the reasons for not using them
should usually be in terms of appropriate behaviour, not performance.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 28 '06 #6
Hi Jon,
This is almost exactly what I was going to post. Special return codes
almost always feel wrong to me, and this feels like a much better
solution to me.
I certainly agree :)
(With regards to the overhead of exceptions, I'd say it should be
regarded as completely irrelevant - the cost of talking to a database
is going to make the overhead of throwing an exception utterly
insignificant. The cost of throwing exceptions should only be seriously
considered when the method in question could reasonably be called
*very* frequently. That doesn't mean that exceptions are always
appropriate to use, of course - but the reasons for not using them
should usually be in terms of appropriate behaviour, not performance.)
I agree with this as well. The database will certainly be the bottle neck
in almost all situations. And when an exception is thrown due to invalid
state or arguments, as in my suggestion, performance should be of little
concern since you most likely have identified a bug in the calling code.
Fixing it should be the priority and you should be thankful that you threw
an exception instead of using a return code ;)

--
Dave Sexton
Nov 28 '06 #7
If I interpreted all the responses well, it seems that it is more a question
of how one feels about a certain option. One thing's clear. Nobody is font
of firing events in this situation.

Jon, since you are a MVP, I want to ask you if there is a specific reason
why you feeling wrong about using returncodes (in this situation)? I guess
(but maybe I'm wrong) you are so experienced programming C# that you've
learned to appriceate throwing exceptions and clearly see the benefits
(which I lack because I'm not experienced) of it, or is just a (gut)feeling?

PS: I know my english isn't that great (causing myself getting into
trouble), so I want to make it clear that I'm not trying to offent you any
way !!

DD
"Jon Skeet [C# MVP]" <sk***@pobox.co mschreef in bericht
news:MP******** *************** *@msnews.micros oft.com...
Dave Sexton <dave@jwa[remove.this]online.comwrote :
>I prefer option #4 :)

As a library method, if Open is going to be used against databases that
could potentially, but not necessarily, require credentials, I think it
should have two overloads: one that takes credentials and one that
doesn't.
When credentials are required, the one that doesn't accept them should
thrown an InvalidOperatio nException. When credentials aren't required
but
are supplied anyway, either ignore them or throw an
InvalidOperati onException, just to be safe.

You should provide a method or property that allows the calling code to
check whether credentials are required so that it may prompt the user for
them, if necessary, and avoid an exception. A method named,
"RequiresCrede ntials" or a property named, "CredentialsReq uired" sounds
reasonable to me.

This is almost exactly what I was going to post. Special return codes
almost always feel wrong to me, and this feels like a much better
solution to me.

(With regards to the overhead of exceptions, I'd say it should be
regarded as completely irrelevant - the cost of talking to a database
is going to make the overhead of throwing an exception utterly
insignificant. The cost of throwing exceptions should only be seriously
considered when the method in question could reasonably be called
*very* frequently. That doesn't mean that exceptions are always
appropriate to use, of course - but the reasons for not using them
should usually be in terms of appropriate behaviour, not performance.)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 29 '06 #8
"Dave Sexton" <dave@jwa[remove.this]online.comwrote :
>You should provide a method or property that allows the calling code to
check whether credentials are required so that it may prompt the user for
them, if necessary, and avoid an exception. A method named,
"RequiresCrede ntials" or a property named, "CredentialsReq uired" sounds
reasonable to me.
I don't know... Think of this code:

bool isreq = db.CredentialsR equired();
if (isreq) db.Login(GetLog inInfo());
db.Access();

What happens if an administrator imposes a security requirement while
this thread is just part way through? Then the db.Access() call will
fail with the access fault. So you're going to need to handle the
access fault in any case.

--
Lucian
Nov 29 '06 #9
Hi Lucian,
>>You should provide a method or property that allows the calling code to
check whether credentials are required so that it may prompt the user for
them, if necessary, and avoid an exception. A method named,
"RequiresCred entials" or a property named, "CredentialsReq uired" sounds
reasonable to me.

I don't know... Think of this code:

bool isreq = db.CredentialsR equired();
if (isreq) db.Login(GetLog inInfo());
db.Access();
I envisioned the code to look more like this:

Database db = new Database(connSt ring);

bool secure = db.RequiresCred entials();

// or
// bool secure = db.CredentialsR equired;

if (secure)
db.Open(GetUser Credentials());
else
db.Open();

Where's the problem here?
What happens if an administrator imposes a security requirement while
this thread is just part way through? Then the db.Access() call will
fail with the access fault. So you're going to need to handle the
access fault in any case.
Multi-threading issues are an entirely different thing to consider. I
assume that the Database class will be immutable in that respect. It
doesn't make sense to change whether or not a connection requires
credentials asynchronously, IMO.

--
Dave Sexton
Nov 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
2041
by: Michael Kalina | last post by:
Hi everybody! I have three questions, maybe somebody could help me with it. 1. "If you have a minute or two..." I usually do not do a lot with CSS, but I decided to have my site run on pure xhtml and css. Now I made it, but I do not know, whether I "coded" a good css or not. If somebody has some minutes free, could you please post your opinion on my css, which you will find here:
6
1661
by: Tippy G | last post by:
My name is Glenn, I am an experienced VB5.0/6.0 programmer who is starting a new project using VB.NET. The project is simple but will be using a .mdb file for storing of data. Only one person will be accessing the database at one time and the database will be stored on a mapped shared folder. I have been reading on ADO.NET and am getting a little confused and wondering if I am am doing the right thing. The use of Data Adapters and...
192
9472
by: Vortex Soft | last post by:
http://www.junglecreatures.com/ Try it and tell me what's happenning in the Microsoft Corporation. Notes: VB, C# are CLS compliant
19
3849
by: Lisa Jones | last post by:
Hi I don’t want to change my name or anything :) but I wish I was able to save Sound into SQL server and retrieve it So my question is How do you save a wav file into a SQL server and how do you retrieve it Thanks so much Lisa Jone
2
4365
by: Martin P. Hellwig | last post by:
Hi all, I'm playing a bit with PostgreSQL, in which I've set me the target to create a python script which with user input creates a new user role and a database with that owner (connecting to template1 since I know that at least that db exists). Ok so I installed PostGreSQL and pygresql since it looked like that this is endorsed by PG, I had some trouble with the DB-API2 (complains about there is already a connection to template1,...
15
1895
by: =?Utf-8?B?TWljaGVsIFBvc3NldGggW01DUF0=?= | last post by:
In my opinion rethrowing exceptions without providing anny extra information is a totall waste Examples : in my opinion wrong : A: Public sub DoSomeStuff() Try do it
6
4805
by: Stephan Bourdon | last post by:
Hi, Your opinion please on the following subject: Is it acceptable to set the width and height of an image in ems or percents in CSS? The advantage for me is that images will scale up or down when the user changes the font size in the browser. But what of the disadvantages? In (X)HTML, the img-element will have a pixel-value for the width and height-attributes.
2
1485
by: cephal0n | last post by:
Hi All! First of I apologize for my previews post needing help on union select and not providing so more explanation, but thank you very much for your opinion and sugestion. I have thought about my problem and try to find a solution. I now have few answres and would like to ask for your expertise on how I handled my sql. I made an sql code, I used sql specific by creating query > new > design view> close the Show Table > right click> click...
0
8808
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8712
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9023
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7954
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5959
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2525
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2113
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.