|
-
Jun 16th, 2004, 02:46 AM
#1
Thread Starter
Frenzied Member
How to perform property conversions?
Ok, in my business tier I have a class called Car, it has a
property of type double
Now this is a potential null field in the database
in my car class I have a method called load which calls the dataaccess tier and returns a datareader of the carinfo...
SOmehow a string must be passed to the presentation layer in the form of a double.
Because when the user access the carpage (with car id)
I do like this
dim mycar as new car(id)
txtname.text = mycar.name
txtprice.text = mycar.price
.
.
.
if I encounter a dbnull here it is very bad. But where should I define the logic that says
"If the price property of the car object is dbnull, then it should say "not available" in the presentation layer"?
And it feels bad to define the price property as string, although it would solve my problems...
kind regards
Henrik
-
Jun 16th, 2004, 04:42 AM
#2
Addicted Member
Can you not allow NULL in the first place?
Otherwise set the string in the presentation layer.
-
Jun 16th, 2004, 05:47 AM
#3
Thread Starter
Frenzied Member
Well, what do you mean? I can see two problems with your suggestion:
1)In the requirement spec our customer has clearly specified the presentation layer and what fields she want and what it should say if there is a value (and if there isn't a value) In this case the customer want a N/A string in the textbox.
2)How should I handle the property which has type double if I encounter a dbnull?? I can't set it to 0.00 because that is incorrect and defy the business rules. And I can't do like this
my method in the car class :
VB Code:
'Called by the car constructor, hence it's private
private sub Load(byval carid as int)
dim dr as new sqldatareader
dr = datatier.getcarbyid(carid)
while not dr.read
m_carid = carid
m_carname = dr.items("name").ToString
m_dblPrice = cdbl(dr.items("price"))
loop
'etc etc etc
As you can see in the code, I will get an exception if I try to
m_dblPrice = cdbl(dr.items("price"))
set the property price to a dbnull value (from the items collection in the dr)
How can I solve this problem using proper 3 tier architecture? Is there a pattern for this?
kind regards
Henrik
ps Apologize if my explanation of the problem was vague at first... ds
-
Jun 16th, 2004, 01:44 PM
#4
PowerPoster
Well, I have had the same problem myself.
What I did, was look at what the strongly typed dataset code was doing to solve this problem. It really is unimaginative. This is based off a three column table. ChildID can't be null as it is the primary key, but the other two can be null. Basically they add in two methods that can be used to set null, and check for null.
(C# code, but you can get the point)
Code:
public int ChildID {
get {
return ((int)(this[this.tableChild.ChildIDColumn]));
}
set {
this[this.tableChild.ChildIDColumn] = value;
}
}
public string ChildName {
get {
try {
return
((string)(this[this.tableChild.ChildNameColumn]));
}
catch (System.InvalidCastException e) {
throw new
System.Data.StrongTypingException("StrongTyping_CannotAccessDBNull", e);
}
}
set {
this[this.tableChild.ChildNameColumn] = value;
}
}
public int ParentID {
get {
try {
return
((int)(this[this.tableChild.ParentIDColumn]));
}
catch (System.InvalidCastException e) {
throw new
System.Data.StrongTypingException("StrongTyping_CannotAccessDBNull", e);
}
}
set {
this[this.tableChild.ParentIDColumn] = value;
}
}
public bool IsChildNameNull() {
return this.IsNull(this.tableChild.ChildNameColumn);
}
public void SetChildNameNull() {
this[this.tableChild.ChildNameColumn] = System.Convert.DBNull;
}
public bool IsParentIDNull() {
return this.IsNull(this.tableChild.ParentIDColumn);
}
public void SetParentIDNull() {
this[this.tableChild.ParentIDColumn] = System.Convert.DBNull;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|