Re: Data/Business Object Tier Best Practices
"Doug Taylor" wrote:
[color=blue]
> On Thu, 23 Jun 2005 16:55:01 -0700, "JeremyHolt "
> <JeremyHolt@dis cussions.micros oft.com> wrote:
>[color=green]
> >Hi,
> >
> >I have previously written fairly large projects using datasets/dataviews
> >with good results.
> >However, ever since having got hold of VB.Net Beta 2, I have been looking at
> >generics - List(Of type), and been messing around with the class designer.
> >
> >I am now starting a new project, where the backend is presently an
> >antiquated DB2 database running on an AS/400 which may or may not move to SQL
> >Server, and the UI will be both Windows Forms and Explorer.
> >
> >Playing around with the class designer, I have created my model, which is
> >essentially classes and collections of classes. The designer allows me to
> >visualise very clearly the interrelationsh ips between the various classes. In
> >the past, I have always started with the database and built the application
> >from there on up. Since the prospect of actually getting the data in and out
> >of the AS/400 is so daunting, I decided to build the middle tier (business
> >rules) first and then worry about the data access layer.
> >
> >However, I have now hit a mental brick wall. I can't see how and where I
> >will be able to update/insert the back-end data from these classes. I
> >downloaded the TimeTracker v 2.0 Starter Kit from asp.net, but since I'm not
> >very goods with C#, haven't been able to really figure out the logic between
> >the n tiers.
> >
> >I'm in a bit of a quandry - I would really like to continue using the class
> >designer to build the application, but would also like to continue to use
> >datasets etc because of their ease of use.
> >
> >Is there somewhere I can go which will point me in the direction of a hybrid
> >model - or must I decide now which way I want to go?[/color]
>
> Jeremy,
>
> You are basically using a model which is known as business objects,
> i.e. you are putting a layer in place between the Database Layer and
> the Presentation Layer that understands your business rules. An
> alternative approach of course is to place these business rules in the
> database as stored procedures and triggers.
>
> There are good reasons for both of these approaches.
>
> Your problem is as you say is to tie the business objects to the
> database, there are two common approaches to this, the first is to
> have as a private member a typed record in each class and a typed
> recordset in the collection and use that to interact with the
> database. The second is to give your objects the ability to access
> the database directly. Again both approaches are valid. There are a
> number of articles on the internet that discuss the merits of these
> approaches.
>
> Doug Taylor[color=green]
> >
> >Many thanks
> >Jeremy Holt
> >[/color]
>
>[/color]
Doug,
Many thanks for your thoughts on this. I had previoulsy thought about a
private typed record in the class, but then found myself inserting the data
into the class properties from the dataset - this seemed rather
counter-intuituve in that if I already have the typed dataset, why would I
want to transfer this data back into my "typed" class. I also have a problem
visualising how one would handle the "collection ".
In my business model, I have defined the following classes:
Class Product
ProductID as integer
SampleSize as double
Description as string
Grade as string
MoistureContent as double
Ash as double
Rotten as double
End Class
Class LaboratoryAnaly sis
LabID as integer
Date as date
Products as List(Of Product)
End Class
Class Truck
TruckID as integer
NumberPlate as string
Origin as string
Quantity as integer
LabAnalysis as List(Of LaboratoryAnaly sis)
End Class
Class Warehouse
WarehouseID as integer
Trucks as List(Of Truck)
End Class
I tend to think in terms of stored procs/triggers filling/updating datasets,
producing dataviews which are consumed by the UI. Single records (master part
of master/details) are obtained through passing a parameter to the stored
proc. As I said in my previous post, I know it works, and its relatively easy
to do.
However, I am constantly drawn back to the idea of classes/collection of
classes, because of the elegance of the Class Designer.
In my "dataset world" in order to fill my Warehouse I would have a stored
proc which queried the database and would do an inner join accross the tables
to give me the "collection " of Trucks - in reality a dataview, which would
continue to drill down through the LaboratoryAnaly sis to the Products that
were analysed in the laboratory etc. My query would thus be able to give me
how many tons of Rotten Apples I have in the Warehouse.
In my "Generics world" my Business Objects code would be something like:
Dim TotalWeight as integer
Dim RottenWeight as double
For each T as Truck in Warehouse.Truck s
TotalWeight += T.Quantity
For each L as LaboratoryAnaly sis IN T.LabAnalysis
For each P as Product in L.Products
RottenWeight += (T.Quantity * P.Rotten)
Next
Next
Next
Assuming I'm along the right lines so far, how would I get the data, and
update the data into my collections? I guess something like:
Class Product
ProductID as integer
SampleSize as double
Description as string
Grade as string
MoistureContent as double
Ash as double
Rotten as double
Sub Load(ProductID as integer)
Dim ds as dsProducts
' Fill dataset
'SELECT ProductID, SampleSize, Description, Grade, MoistureContent , Ash,
Rotten FROM Products WHERE ProductID BETWEEN COALESCE(-1, '@ProductID) AND
COALESCE(65535, @ProductID)
For Each row as dsProducts.Prod uctsRow in dsProducts
Me.SampleSize = row.SampleSize
Me.Description = row.Description
etc.
Next
End Sub
End Class
However, this is where I run into my mental "brick wall":
1) Should Class Product not just expose the dataset, saving me the effort of
the loop to fill the class properties with the row details?
2) How do I update Class Product? Assuming that I'm using a DataGridSource
(asp.net or win.forms), if the souce of these UI elements is a dataview, then
I can use all of ADO.Net's capababilities, i.e. Merge, GetChanges(data set)
etc.
If I carry on down the road of my "Generics" model do I have to lose all the
"cool" things about ADO.Net? Alternativley, how do I model my "traditiona l
database oriented" design using Class Designer?
I do hope that I have been able to clearly express my confusion, and hope
that someone could point me in the right direction.
Many thanks
Jeremy Holt
"Doug Taylor" wrote:
[color=blue]
> On Thu, 23 Jun 2005 16:55:01 -0700, "JeremyHolt "
> <JeremyHolt@dis cussions.micros oft.com> wrote:
>[color=green]
> >Hi,
> >
> >I have previously written fairly large projects using datasets/dataviews
> >with good results.
> >However, ever since having got hold of VB.Net Beta 2, I have been looking at
> >generics - List(Of type), and been messing around with the class designer.
> >
> >I am now starting a new project, where the backend is presently an
> >antiquated DB2 database running on an AS/400 which may or may not move to SQL
> >Server, and the UI will be both Windows Forms and Explorer.
> >
> >Playing around with the class designer, I have created my model, which is
> >essentially classes and collections of classes. The designer allows me to
> >visualise very clearly the interrelationsh ips between the various classes. In
> >the past, I have always started with the database and built the application
> >from there on up. Since the prospect of actually getting the data in and out
> >of the AS/400 is so daunting, I decided to build the middle tier (business
> >rules) first and then worry about the data access layer.
> >
> >However, I have now hit a mental brick wall. I can't see how and where I
> >will be able to update/insert the back-end data from these classes. I
> >downloaded the TimeTracker v 2.0 Starter Kit from asp.net, but since I'm not
> >very goods with C#, haven't been able to really figure out the logic between
> >the n tiers.
> >
> >I'm in a bit of a quandry - I would really like to continue using the class
> >designer to build the application, but would also like to continue to use
> >datasets etc because of their ease of use.
> >
> >Is there somewhere I can go which will point me in the direction of a hybrid
> >model - or must I decide now which way I want to go?[/color]
>
> Jeremy,
>
> You are basically using a model which is known as business objects,
> i.e. you are putting a layer in place between the Database Layer and
> the Presentation Layer that understands your business rules. An
> alternative approach of course is to place these business rules in the
> database as stored procedures and triggers.
>
> There are good reasons for both of these approaches.
>
> Your problem is as you say is to tie the business objects to the
> database, there are two common approaches to this, the first is to
> have as a private member a typed record in each class and a typed
> recordset in the collection and use that to interact with the
> database. The second is to give your objects the ability to access
> the database directly. Again both approaches are valid. There are a
> number of articles on the internet that discuss the merits of these
> approaches.
>
> Doug Taylor[color=green]
> >
> >Many thanks
> >Jeremy Holt
> >[/color]
>
>[/color]
Doug,
Many thanks for your thoughts on this. I had previoulsy thought about a
private typed record in the class, but then found myself inserting the data
into the class properties from the dataset - this seemed rather
counter-intuituve in that if I already have the typed dataset, why would I
want to transfer this data back into my "typed" class. I also have a problem
visualising how one would handle the "collection ".
In my business model, I have defined the following classes:
Class Product
ProductID as integer
SampleSize as double
Description as string
Grade as string
MoistureContent as double
Ash as double
Rotten as double
End Class
Class LaboratoryAnaly sis
LabID as integer
Date as date
Products as List(Of Product)
End Class
Class Truck
TruckID as integer
NumberPlate as string
Origin as string
Quantity as integer
LabAnalysis as List(Of LaboratoryAnaly sis)
End Class
Class Warehouse
WarehouseID as integer
Trucks as List(Of Truck)
End Class
I tend to think in terms of stored procs/triggers filling/updating datasets,
producing dataviews which are consumed by the UI. Single records (master part
of master/details) are obtained through passing a parameter to the stored
proc. As I said in my previous post, I know it works, and its relatively easy
to do.
However, I am constantly drawn back to the idea of classes/collection of
classes, because of the elegance of the Class Designer.
In my "dataset world" in order to fill my Warehouse I would have a stored
proc which queried the database and would do an inner join accross the tables
to give me the "collection " of Trucks - in reality a dataview, which would
continue to drill down through the LaboratoryAnaly sis to the Products that
were analysed in the laboratory etc. My query would thus be able to give me
how many tons of Rotten Apples I have in the Warehouse.
In my "Generics world" my Business Objects code would be something like:
Dim TotalWeight as integer
Dim RottenWeight as double
For each T as Truck in Warehouse.Truck s
TotalWeight += T.Quantity
For each L as LaboratoryAnaly sis IN T.LabAnalysis
For each P as Product in L.Products
RottenWeight += (T.Quantity * P.Rotten)
Next
Next
Next
Assuming I'm along the right lines so far, how would I get the data, and
update the data into my collections? I guess something like:
Class Product
ProductID as integer
SampleSize as double
Description as string
Grade as string
MoistureContent as double
Ash as double
Rotten as double
Sub Load(ProductID as integer)
Dim ds as dsProducts
' Fill dataset
'SELECT ProductID, SampleSize, Description, Grade, MoistureContent , Ash,
Rotten FROM Products WHERE ProductID BETWEEN COALESCE(-1, '@ProductID) AND
COALESCE(65535, @ProductID)
For Each row as dsProducts.Prod uctsRow in dsProducts
Me.SampleSize = row.SampleSize
Me.Description = row.Description
etc.
Next
End Sub
End Class
However, this is where I run into my mental "brick wall":
1) Should Class Product not just expose the dataset, saving me the effort of
the loop to fill the class properties with the row details?
2) How do I update Class Product? Assuming that I'm using a DataGridSource
(asp.net or win.forms), if the souce of these UI elements is a dataview, then
I can use all of ADO.Net's capababilities, i.e. Merge, GetChanges(data set)
etc.
If I carry on down the road of my "Generics" model do I have to lose all the
"cool" things about ADO.Net? Alternativley, how do I model my "traditiona l
database oriented" design using Class Designer?
I do hope that I have been able to clearly express my confusion, and hope
that someone could point me in the right direction.
Many thanks
Jeremy Holt
Comment