473,626 Members | 3,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Flexibility vs Reliability

My co-worker and I are debating Flexibility vs. Reliability.

For example this method is highly flexible:
Public Shared Function GetList(ByVal whereClause As String) As String
strSQL = "SELECT field1,field2,f ield3,field4,fi eld5 FROM table1 "
If whereClause <> String.Empty Then
strSQL &= "WHERE " & whereClause
End If
Return strSQL
End Function

My co-worker agress that it is flexible but would prefer to use more
reliable techniques like Overloading and Strong Typing of parameters.

Public Shared Function GetList(ByVal field1Value As String, ByVal
field2Value As Integer) As String
strSQL = "SELECT field1,field2,f ield3,field4,fi eld5 FROM table1 "
strSQL &= "WHERE field1='" & field1Value & "' AND field2=" &
field2Value
Return strSQL
End Function

My argument is that you can't predict every possible combination of field
values and arguments so the number of where clauses and overloaded methods
would grow to ridiculous proportions.

Have other people faced this issue? If so what did you think about it?

Appreciate any insights and experiences.

Thanks!
--
Joe Fallon

Nov 20 '05 #1
20 3186
Hi Joe,

I'd want to add maintainability and utility (which includes memorability)
to the mix. Now we have a four-dimensional space. In practice in that whole
arena, I imagine that you'd find most cases clustered around the same little
area (as judged over the whole space). Zooming in you'd perhaps discover some
overall patterns, but really, each case has to be given its position based on
its own merits.

Each of these qualities is subjective. There comes a point where one more
overload tips the scale for Joe, but Richard could happily add two more.
Neither are right. Next month, when Martin joins the project, he has to learn
either the complete list of overloads or the limits of the all-encompassing
parameter.

Are you really using table1 and field1, field2, etc. That's a better area
for improvemnt I would have thought. ;-)

But if you're looking for personal opinions, I'd go for maximum
flexibility

Public Shared Function GetList (ByVal strSQL As String) As String
Return strSQL
End Function
SCNR ;-)

Regards,
Fergus
Nov 20 '05 #2
"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote...
My co-worker and I are debating Flexibility vs. Reliability.
Hi Joe: I think it's nice when we can have these types of discussions in a
newsgroup.
My argument is that you can't predict every possible combination of field
values and arguments so the number of where clauses and overloaded methods
would grow to ridiculous proportions.


One of the criteria for choosing one over the other is "who has access to
it" meaning if GetList() is used by the UI developer then your first example
is fine and it is fair to trust the UI developer will pass a legal string or
suffer the consequences. If a user's input would be used to supply the
where clause however then you should consider stricter control.

Another measure is "have there been any problems?" Meaning... should we be
overly concerned about a goofy where clause being passed when it has never
happened? If your flexible implementation fails every day causing chaos and
disruption then "sure" it's time to parse the dang string and add an "are
you sure?" dialog box or reassign the person doing it.

You would want to support "LIKE" and >, >=, < and <= also so the
combinations continue to grow... not to mention OR, BETWEEN and the
parenthetical grouping of some conditions.

I also don't think I would overload the method if I wanted to control things
a bit more. I'd probably name the methods something related to what the
various versions did, i.e. GetListWhereNam e(), GetListWhereId( ),
GetListWhereSta te() or whatever. They might feed a generic GetList but if
you try to overload GetList() you will run out of unique signatures long
before you are even close to done.

Tom

Nov 20 '05 #3
Joe,
I would go for your first example, However I would overload GetList, instead
of checking for String.Empty.
Const strSQL As String = "SELECT field1,field2,f ield3,field4,fi eld5 FROM table1"
Public Shared Function GetList(ByVal whereClause As String) As String Return strSQL & " WHERE " & whereClause End Function Public Shared Function GetList() As String
Return strSQL
End Function
Notice that the first overload with a string assumes that you have a
properly formed where clause, where as the second is the 'all' case.

Rather then attempt your second function, for largely the same reasons you
state. If I wanted this flexibity I would recommend implementing a Query
Object pattern.

http://www.martinfowler.com/eaaCatalog/queryObject.html

Which will allow significantly more flexibity.

The Query Object Pattern is from Martin Fowler's "Patterns of Enterprise
Application Architecture" from Addison Wesley

http://www.martinfowler.com/books.html#eaa

A simplified form of the Query Object would be to have your second function
accept an ParamArray of Criteria objects, where each Criteria object has a
field, a comparison operator and a value.

Something like (incomplete, untested):
' You could just use a string instead of defining the Enum.
Public Enum Operator
None
Equal
NotEqual
GreaterThen
End Enum

Public Class Criteria
private m_field As String
private m_operator As Operator
private m_value As String
Public Sub New(field As String, operator As Operator, value As
Integer)
Public Sub New(field As String, operator As Operator, value As
Double)
Public Sub New(field As String, operator As Operator, value As
String)
...
Public Overrides Function ToString() As String
End Class

Public Shared Function GetList(ByVal ParamArray criterias() as Criteria)
As String
Dim sb As New StringBuilder(" SELECT * FROM WHEREVER ")
if criterias.Lengt h > 0 then
sb.Append(" WHERE ")
End If
For Each criteria As Criteria In criterias
sb.Append(crite ria) ' uses the overloaded ToString function
sb.Append(" AND ")
Next
if criterias.Lengt h > 0 Then
sb.Length -= 5 ' remove the trailing AND
End If
Return sb.ToString()
End Function

Then when you call it, you would use:

GetList(New Criteria("Field 1", Operator.Equal, 1), New
Criteria("Field 2", Operator.NotEqu al, 2))

The advantage of this simplified Query Object is that Criteria's overloaded
constructors can handle quoting & converting the value correctly.

Although you made the GetList shared, I see more value in it being
overridable on an object. Where you have Table Data Gateway objects, where
the 'SQL' for each table in its own object, this object have overridable
methods to get & invoke those methods. The first & third methods support
polymorphism better then the second. The problem of course is you are
exposing field names, Martin's book has ideas on how to mitigate that also.

http://www.martinfowler.com/eaaCatal...taGateway.html

Hope this helps
Jay

"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. .. My co-worker and I are debating Flexibility vs. Reliability.

For example this method is highly flexible:
Public Shared Function GetList(ByVal whereClause As String) As String
strSQL = "SELECT field1,field2,f ield3,field4,fi eld5 FROM table1 "
If whereClause <> String.Empty Then
strSQL &= "WHERE " & whereClause
End If
Return strSQL
End Function

My co-worker agress that it is flexible but would prefer to use more
reliable techniques like Overloading and Strong Typing of parameters.

Public Shared Function GetList(ByVal field1Value As String, ByVal
field2Value As Integer) As String
strSQL = "SELECT field1,field2,f ield3,field4,fi eld5 FROM table1 "
strSQL &= "WHERE field1='" & field1Value & "' AND field2=" &
field2Value
Return strSQL
End Function

My argument is that you can't predict every possible combination of field
values and arguments so the number of where clauses and overloaded methods
would grow to ridiculous proportions.

Have other people faced this issue? If so what did you think about it?

Appreciate any insights and experiences.

Thanks!
--
Joe Fallon


Nov 20 '05 #4
Jay,
I appreciate the comments.

For the current project I will recommend the flexible option.

Good to know the more advanced techniques are available though.
--
Joe Fallon
"Jay B. Harlow [MVP - Outlook]" <Ja********@ema il.msn.com> wrote in message
news:ei******** ******@TK2MSFTN GP09.phx.gbl...
Joe,
I would go for your first example, However I would overload GetList, instead of checking for String.Empty.
Const strSQL As String = "SELECT field1,field2,f ield3,field4,fi eld5
FROM table1"
Public Shared Function GetList(ByVal whereClause As String) As String Return strSQL & " WHERE " & whereClause
End Function

Public Shared Function GetList() As String
Return strSQL
End Function


Notice that the first overload with a string assumes that you have a
properly formed where clause, where as the second is the 'all' case.

Rather then attempt your second function, for largely the same reasons you
state. If I wanted this flexibity I would recommend implementing a Query
Object pattern.

http://www.martinfowler.com/eaaCatalog/queryObject.html

Which will allow significantly more flexibity.

The Query Object Pattern is from Martin Fowler's "Patterns of Enterprise
Application Architecture" from Addison Wesley

http://www.martinfowler.com/books.html#eaa

A simplified form of the Query Object would be to have your second

function accept an ParamArray of Criteria objects, where each Criteria object has a
field, a comparison operator and a value.

Something like (incomplete, untested):
' You could just use a string instead of defining the Enum.
Public Enum Operator
None
Equal
NotEqual
GreaterThen
End Enum

Public Class Criteria
private m_field As String
private m_operator As Operator
private m_value As String
Public Sub New(field As String, operator As Operator, value As
Integer)
Public Sub New(field As String, operator As Operator, value As
Double)
Public Sub New(field As String, operator As Operator, value As
String)
...
Public Overrides Function ToString() As String
End Class

Public Shared Function GetList(ByVal ParamArray criterias() as Criteria) As String
Dim sb As New StringBuilder(" SELECT * FROM WHEREVER ")
if criterias.Lengt h > 0 then
sb.Append(" WHERE ")
End If
For Each criteria As Criteria In criterias
sb.Append(crite ria) ' uses the overloaded ToString function
sb.Append(" AND ")
Next
if criterias.Lengt h > 0 Then
sb.Length -= 5 ' remove the trailing AND
End If
Return sb.ToString()
End Function

Then when you call it, you would use:

GetList(New Criteria("Field 1", Operator.Equal, 1), New
Criteria("Field 2", Operator.NotEqu al, 2))

The advantage of this simplified Query Object is that Criteria's overloaded constructors can handle quoting & converting the value correctly.

Although you made the GetList shared, I see more value in it being
overridable on an object. Where you have Table Data Gateway objects, where
the 'SQL' for each table in its own object, this object have overridable
methods to get & invoke those methods. The first & third methods support
polymorphism better then the second. The problem of course is you are
exposing field names, Martin's book has ideas on how to mitigate that also.
http://www.martinfowler.com/eaaCatal...taGateway.html

Hope this helps
Jay

"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.phx.gbl. ..
My co-worker and I are debating Flexibility vs. Reliability.

For example this method is highly flexible:
Public Shared Function GetList(ByVal whereClause As String) As String
strSQL = "SELECT field1,field2,f ield3,field4,fi eld5 FROM table1 "
If whereClause <> String.Empty Then
strSQL &= "WHERE " & whereClause
End If
Return strSQL
End Function

My co-worker agress that it is flexible but would prefer to use more
reliable techniques like Overloading and Strong Typing of parameters.

Public Shared Function GetList(ByVal field1Value As String, ByVal
field2Value As Integer) As String
strSQL = "SELECT field1,field2,f ield3,field4,fi eld5 FROM table1 "
strSQL &= "WHERE field1='" & field1Value & "' AND field2=" &
field2Value
Return strSQL
End Function

My argument is that you can't predict every possible combination of

field values and arguments so the number of where clauses and overloaded methods would grow to ridiculous proportions.

Have other people faced this issue? If so what did you think about it?

Appreciate any insights and experiences.

Thanks!
--
Joe Fallon



Nov 20 '05 #5
Tom,
Points taken.
Good reasoning.

I agree.
--
Joe Fallon
"Tom Leylan" <ge*@iamtiredof spam.com> wrote in message
news:52******** ************@tw ister.nyc.rr.co m...
"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote...
My co-worker and I are debating Flexibility vs. Reliability.
Hi Joe: I think it's nice when we can have these types of discussions in

a newsgroup.
My argument is that you can't predict every possible combination of field values and arguments so the number of where clauses and overloaded methods would grow to ridiculous proportions.
One of the criteria for choosing one over the other is "who has access to
it" meaning if GetList() is used by the UI developer then your first

example is fine and it is fair to trust the UI developer will pass a legal string or suffer the consequences. If a user's input would be used to supply the
where clause however then you should consider stricter control.

Another measure is "have there been any problems?" Meaning... should we be overly concerned about a goofy where clause being passed when it has never
happened? If your flexible implementation fails every day causing chaos and disruption then "sure" it's time to parse the dang string and add an "are
you sure?" dialog box or reassign the person doing it.

You would want to support "LIKE" and >, >=, < and <= also so the
combinations continue to grow... not to mention OR, BETWEEN and the
parenthetical grouping of some conditions.

I also don't think I would overload the method if I wanted to control things a bit more. I'd probably name the methods something related to what the
various versions did, i.e. GetListWhereNam e(), GetListWhereId( ),
GetListWhereSta te() or whatever. They might feed a generic GetList but if
you try to overload GetList() you will run out of unique signatures long
before you are even close to done.

Tom

Nov 20 '05 #6
;-)
--
Joe Fallon
"Fergus Cooney" <fi****@post.co m> wrote in message
news:OB******** ******@TK2MSFTN GP12.phx.gbl...
Hi Joe,

I'd want to add maintainability and utility (which includes memorability) to the mix. Now we have a four-dimensional space. In practice in that whole arena, I imagine that you'd find most cases clustered around the same little area (as judged over the whole space). Zooming in you'd perhaps discover some overall patterns, but really, each case has to be given its position based on its own merits.

Each of these qualities is subjective. There comes a point where one more overload tips the scale for Joe, but Richard could happily add two more.
Neither are right. Next month, when Martin joins the project, he has to learn either the complete list of overloads or the limits of the all-encompassing parameter.

Are you really using table1 and field1, field2, etc. That's a better area for improvemnt I would have thought. ;-)

But if you're looking for personal opinions, I'd go for maximum
flexibility

Public Shared Function GetList (ByVal strSQL As String) As String
Return strSQL
End Function
SCNR ;-)

Regards,
Fergus

Nov 20 '05 #7
On Sat, 1 Nov 2003 12:30:40 -0500, "Joe Fallon"
<jf******@nospa mtwcny.rr.com> wrote:
My co-worker and I are debating Flexibility vs. Reliability.

For example this method is highly flexible:
Public Shared Function GetList(ByVal whereClause As String) As String
strSQL = "SELECT field1,field2,f ield3,field4,fi eld5 FROM table1 "
If whereClause <> String.Empty Then
strSQL &= "WHERE " & whereClause
End If
Return strSQL
End Function

My co-worker agress that it is flexible but would prefer to use more
reliable techniques like Overloading and Strong Typing of parameters.

Public Shared Function GetList(ByVal field1Value As String, ByVal
field2Value As Integer) As String
strSQL = "SELECT field1,field2,f ield3,field4,fi eld5 FROM table1 "
strSQL &= "WHERE field1='" & field1Value & "' AND field2=" &
field2Value
Return strSQL
End Function

My argument is that you can't predict every possible combination of field
values and arguments so the number of where clauses and overloaded methods
would grow to ridiculous proportions.
Implement a GenericSearchOb ject that has properties of all the fields
that could possibly be used. Inside the Set of each of those
properties, set a flag to indicate the developer has set it. When it
builds the SQL, you can Append just the fields and values that were
set.

Have other people faced this issue? If so what did you think about it?

Appreciate any insights and experiences.

Thanks!


I hold the opinion that there should be absolutely no SQL in the a UI
module. In fact, there should be no SQL outside of a DAL. It is the
job of the DAL to disguise the implementation of the objects (i.e.
tables in a database, or the type of the database itself). For this
reason, I abhor data-bound controls (but agree they are useful as a
short-cut) when used directly with generic constructs such as DataSet
or DataTable.

Here's an example of how I model a database application, and you'll
see how I fit in the generic search object:

We are building a system to hold people their bank accounts. We first
list the interfaces we will require to be exposed by a DAL - these are
located in one library.

-begin namespace/library Banking-
Interface IBankDatabase

Interface IPerson
Interface IPersonCollecti on
Interface IPersonSearch

Interface IBankAccount
Interface IBankAccountCol lection
Interface IBankAccountSea rch
-end namespace/library Banking-

If we have a SQL Server back-end, we would implement the following
objects in a library:

-begin namespace/library Banking.DataAcc ess.SqlServer-
Public Class SqlServerBankDa tabase Implements IBankDatabase

Public Class SqlServerPerson Implements IPerson
Public Class SqlServerPerson Collection Implements IPersonCollecti on
Public Class SqlServerPerson Search Implements IPersonSearch

Public Class SqlServerBankAc count Implements IBankAccount
Public Class SqlServerBankAc countCollection Implements
IBankAccountCol ection
Public Class SqlServerBankAc countSearch Implements IBankAccountSea rch
-end namespace/library Banking.DataAcc ess.SqlServer-

We also want to allow objects to be stored in a local MDB. Therefore,
we implement the following objects in another library:

-begin namespace/library Banking.DataAcc ess.Access-
Public Class AccessBankDatab ase Implements IBankDatabase

Public Class AccessPerson Implements IPerson
Public Class AccessPersonCol lection Implements IPersonCollecti on
Public Class AccessPersonSea rch Implements IPersonSearch

Public Class AccessBankAccou nt Implements IBankAccount
Public Class AccessBankAccou ntCollection Implements
IBankAccountCol ection
Public Class AccessBankAccou ntSearch Implements IBankAccountSea rch
-end namespace/library Banking.DataAcc ess.Access-

In our "Banking" library, we also need an object to create the
relevant database objects:

-begin namespace/library Banking-
Public Class BankingDatabase Factory
Public Shared Function CreateSqlServer ( _
hostName,userID ,password) As _
IBankDatabase
Return New SqlServerBankDa tabase(hostName ,userID, _
password)
End Function

Public Shared Function CreateAccess( _
filename) As
IBankDatabase
Return New AccessBankDatab ase(filename)
End Function
End Class
-end namespace/library Banking-

Now we can reference just the interfaces library ("Banking") in our UI
(or BL or whatever) without needing to know how to write SQL. In the
UI code, we could have a routine something like this:

-begin example.vb-
IBankDatabase oBank = BankingDatabase Factory.CreateS qlServer( _
"localhost","fr ed","password ")

Public Sub ShowEverything( )
Dim oPerson As IPerson
For Each oPerson In oBank.People
Debug.WriteLine (oPerson)
Dim oAccount As IBankAccount
For Each oAccount In oPerson.Account s
Debug.WriteLine (oAccount)
Next
Next
End Sub

Public Sub SearchForPerson ByName(oName As String)
Dim oResults As IPersonCollecti on
oResults = oBank.PersonSea rch.FindByName( oName)
Dim oPerson As IPerson
For Each oPerson In oResults
Debug.WriteLine (oPerson)
Next
End Sub
-end example.vb-


As you can see from the example, there is no need for SQL in the UI.
You may, however, wish to expose the ability for WHERE and ORDER BY
clauses on a *Search object. Although it is bad practice, it can be
used to assist RAD. The implementation of the *Search should record
such requests to a log file during development, and you should work to
remove them later on. Why?

Firstly, consider the fact that you might wish to move from one
database to another (SQL to Oracle, for example). The functions in the
SQL are different, therefore you would have to go though every
application and change the code.

Secondly, consider if you want to change the datastructure. i.e. table
names or field names. With the model outlined above, you only need
change the DAL and all applications will continue to work.

Thirdly, security. Consider a naughty programmer who sends a WHERE
clause like: 'tblPerson.Name ="Fred" delete tblPerson'
If you want a generic search, implement just that! On the PersonSearch
object, for example, you would provide the following functionality:

Dim oSearch As IGenericPersonS earch
oSearch = oBank.CreateGen ericPersonSearc h()
oSearch.Name = "Fred"
oSearch.Age = 45
Dim oPerson As IPerson
For Each oPerson In oSearch.Results
...
Next

The SqlServerPerson Search object would handle the assembly of the
WHERE clause by detecting which properties the programmer has set. You
can add specifics for "Age less than" etc. if needs be. But if the SQL
required is any more complicated than that, you should implement your
own function.

A couple of other points:

1) Although it is sometimes possible to make use of overloading, the
absense of TypeDef means that a "Surname" and a "Forename" are the
same type ("String"). Therefore, a FindBySurname or FindByForename
approach tends to be better. One of the more imporant points here is
the fact that the interface can demonstrate the business logic. e.g.
FindPeopleWithN oAccount() might be exposed on the IPersonSearch.

2) Trusting a UI programmer (or anyone else) to use your library as
you expect is very, very bad practice. Not only will it introduce
security holes, but you'll also get the blame for his bugs.

Finally, there are other ways of implementing the separated interface
pattern (SEP), but that defined above is the way I prefer - I don't
like an object knowing how it was created (by having a IPersonFactory
as a member of a Person object). Also, the usage of Interfaces in the
DAL could be replaced with inheritance (where each object in the
Interfaces library would be declared as MustInherit).
I hope that somewhere there you get some ideas.

Rgds,
Nov 20 '05 #8
Andy,
Thanks for the comments.
Very well thought out.

A bit beyond my OOP experience right now, but I now know there are other
approaches to this problem.
As I continue to learn I will keep this in mind.

Thanks!
--
Joe Fallon

"_Andy_" <wi******@nospa mthanks.gov> wrote in message
news:8e******** *************** *********@4ax.c om...
On Sat, 1 Nov 2003 12:30:40 -0500, "Joe Fallon"
<jf******@nospa mtwcny.rr.com> wrote:
My co-worker and I are debating Flexibility vs. Reliability.

For example this method is highly flexible:
Public Shared Function GetList(ByVal whereClause As String) As String
strSQL = "SELECT field1,field2,f ield3,field4,fi eld5 FROM table1 "
If whereClause <> String.Empty Then
strSQL &= "WHERE " & whereClause
End If
Return strSQL
End Function

My co-worker agress that it is flexible but would prefer to use more
reliable techniques like Overloading and Strong Typing of parameters.

Public Shared Function GetList(ByVal field1Value As String, ByVal
field2Value As Integer) As String
strSQL = "SELECT field1,field2,f ield3,field4,fi eld5 FROM table1 "
strSQL &= "WHERE field1='" & field1Value & "' AND field2=" &
field2Value
Return strSQL
End Function

My argument is that you can't predict every possible combination of field
values and arguments so the number of where clauses and overloaded methodswould grow to ridiculous proportions.


Implement a GenericSearchOb ject that has properties of all the fields
that could possibly be used. Inside the Set of each of those
properties, set a flag to indicate the developer has set it. When it
builds the SQL, you can Append just the fields and values that were
set.

Have other people faced this issue? If so what did you think about it?

Appreciate any insights and experiences.

Thanks!


I hold the opinion that there should be absolutely no SQL in the a UI
module. In fact, there should be no SQL outside of a DAL. It is the
job of the DAL to disguise the implementation of the objects (i.e.
tables in a database, or the type of the database itself). For this
reason, I abhor data-bound controls (but agree they are useful as a
short-cut) when used directly with generic constructs such as DataSet
or DataTable.

Here's an example of how I model a database application, and you'll
see how I fit in the generic search object:

We are building a system to hold people their bank accounts. We first
list the interfaces we will require to be exposed by a DAL - these are
located in one library.

-begin namespace/library Banking-
Interface IBankDatabase

Interface IPerson
Interface IPersonCollecti on
Interface IPersonSearch

Interface IBankAccount
Interface IBankAccountCol lection
Interface IBankAccountSea rch
-end namespace/library Banking-

If we have a SQL Server back-end, we would implement the following
objects in a library:

-begin namespace/library Banking.DataAcc ess.SqlServer-
Public Class SqlServerBankDa tabase Implements IBankDatabase

Public Class SqlServerPerson Implements IPerson
Public Class SqlServerPerson Collection Implements IPersonCollecti on
Public Class SqlServerPerson Search Implements IPersonSearch

Public Class SqlServerBankAc count Implements IBankAccount
Public Class SqlServerBankAc countCollection Implements
IBankAccountCol ection
Public Class SqlServerBankAc countSearch Implements IBankAccountSea rch
-end namespace/library Banking.DataAcc ess.SqlServer-

We also want to allow objects to be stored in a local MDB. Therefore,
we implement the following objects in another library:

-begin namespace/library Banking.DataAcc ess.Access-
Public Class AccessBankDatab ase Implements IBankDatabase

Public Class AccessPerson Implements IPerson
Public Class AccessPersonCol lection Implements IPersonCollecti on
Public Class AccessPersonSea rch Implements IPersonSearch

Public Class AccessBankAccou nt Implements IBankAccount
Public Class AccessBankAccou ntCollection Implements
IBankAccountCol ection
Public Class AccessBankAccou ntSearch Implements IBankAccountSea rch
-end namespace/library Banking.DataAcc ess.Access-

In our "Banking" library, we also need an object to create the
relevant database objects:

-begin namespace/library Banking-
Public Class BankingDatabase Factory
Public Shared Function CreateSqlServer ( _
hostName,userID ,password) As _
IBankDatabase
Return New SqlServerBankDa tabase(hostName ,userID, _
password)
End Function

Public Shared Function CreateAccess( _
filename) As
IBankDatabase
Return New AccessBankDatab ase(filename)
End Function
End Class
-end namespace/library Banking-

Now we can reference just the interfaces library ("Banking") in our UI
(or BL or whatever) without needing to know how to write SQL. In the
UI code, we could have a routine something like this:

-begin example.vb-
IBankDatabase oBank = BankingDatabase Factory.CreateS qlServer( _
"localhost","fr ed","password ")

Public Sub ShowEverything( )
Dim oPerson As IPerson
For Each oPerson In oBank.People
Debug.WriteLine (oPerson)
Dim oAccount As IBankAccount
For Each oAccount In oPerson.Account s
Debug.WriteLine (oAccount)
Next
Next
End Sub

Public Sub SearchForPerson ByName(oName As String)
Dim oResults As IPersonCollecti on
oResults = oBank.PersonSea rch.FindByName( oName)
Dim oPerson As IPerson
For Each oPerson In oResults
Debug.WriteLine (oPerson)
Next
End Sub
-end example.vb-


As you can see from the example, there is no need for SQL in the UI.
You may, however, wish to expose the ability for WHERE and ORDER BY
clauses on a *Search object. Although it is bad practice, it can be
used to assist RAD. The implementation of the *Search should record
such requests to a log file during development, and you should work to
remove them later on. Why?

Firstly, consider the fact that you might wish to move from one
database to another (SQL to Oracle, for example). The functions in the
SQL are different, therefore you would have to go though every
application and change the code.

Secondly, consider if you want to change the datastructure. i.e. table
names or field names. With the model outlined above, you only need
change the DAL and all applications will continue to work.

Thirdly, security. Consider a naughty programmer who sends a WHERE
clause like: 'tblPerson.Name ="Fred" delete tblPerson'
If you want a generic search, implement just that! On the PersonSearch
object, for example, you would provide the following functionality:

Dim oSearch As IGenericPersonS earch
oSearch = oBank.CreateGen ericPersonSearc h()
oSearch.Name = "Fred"
oSearch.Age = 45
Dim oPerson As IPerson
For Each oPerson In oSearch.Results
...
Next

The SqlServerPerson Search object would handle the assembly of the
WHERE clause by detecting which properties the programmer has set. You
can add specifics for "Age less than" etc. if needs be. But if the SQL
required is any more complicated than that, you should implement your
own function.

A couple of other points:

1) Although it is sometimes possible to make use of overloading, the
absense of TypeDef means that a "Surname" and a "Forename" are the
same type ("String"). Therefore, a FindBySurname or FindByForename
approach tends to be better. One of the more imporant points here is
the fact that the interface can demonstrate the business logic. e.g.
FindPeopleWithN oAccount() might be exposed on the IPersonSearch.

2) Trusting a UI programmer (or anyone else) to use your library as
you expect is very, very bad practice. Not only will it introduce
security holes, but you'll also get the blame for his bugs.

Finally, there are other ways of implementing the separated interface
pattern (SEP), but that defined above is the way I prefer - I don't
like an object knowing how it was created (by having a IPersonFactory
as a member of a Person object). Also, the usage of Interfaces in the
DAL could be replaced with inheritance (where each object in the
Interfaces library would be declared as MustInherit).
I hope that somewhere there you get some ideas.

Rgds,

Nov 20 '05 #9
;-)

FYI:
My config file determines the database type (Oracle or SQL Server) and based
on that I return the appropriate SQL String.

--
Joe Fallon

"Tom Leylan" <ge*@iamtiredof spam.com> wrote in message
news:nr******** ************@tw ister.nyc.rr.co m...
"Joe Fallon" <jf******@nospa mtwcny.rr.com> wrote...
A bit beyond my OOP experience right now, but I now know there are other
approaches to this problem.
Hi Joe... I couldn't grasp it all but in case I can be of some assistance
let me offer the following:

I avoid direct references to SQL within the UI also. It immediately ties
the UI to SQL and (depending upon the example) possibly to SQL-Server.

I'll suppose that Andy's key concept is to simply abstract SQL such that the UI
can form a query that "any" back-end can interpret.

That said I don't believe we actually know where "GetList" resides. That it returns a SQL string might not be an issue at all. It could of course
return a sort of query object that includes the query string but it is hard to know exactly where to stop abstracting and to get on with software
development. :-)

Bottom line "stick with flexibility."

Nov 20 '05 #10

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

Similar topics

77
5665
by: nospam | last post by:
Reasons for a 3-tier achitecture for the WEB? (NOTE: I said, WEB, NOT WINDOWS. DON'T shoot your mouth off if you don't understand the difference.) I hear only one reason and that's to switch a database from SQL Server to Oracle or DB2 or vice versa... and that's it.... And a lot of these enterprises don't need it as they already know what database they are going to use and they don't plan on switching in and out database in the first...
4
1441
by: Biz | last post by:
I have an ASP form which collects data into a MS Access database. This is used to collect information from 120 of my students. Last year I used a free host but this was utterly unreliable. This year I am using a paid host. It's much more reliable but I'm still finding about 20% of students do not get their data saved to the database at all. What could I do to improve reliability ?
3
1448
by: Ares Lagae | last post by:
Suppose one is writing a library behavior the classes A1, A2 and A3, which share some common behavior. Some applications will only use one of the classes, while other applications will use all of them. Because the classes share common behavior, the library designed has to choose wether to introduce an abstract base class A, or not. Introducing an ABC has the advantage of run time flexibility, but efficiency suffers (virtual functions &...
34
6403
by: Ville Voipio | last post by:
I would need to make some high-reliability software running on Linux in an embedded system. Performance (or lack of it) is not an issue, reliability is. The piece of software is rather simple, probably a few hundred lines of code in Python. There is a need to interact with network using the socket module, and then probably a need to do something hardware- related which will get its own driver written in C.
4
1303
by: John Pote | last post by:
Hello, help/advice appreciated. Background: I am writing some web scripts in python to receive small amounts of data from remote sensors and store the data in a file. 50 to 100 bytes every 5 or 10 minutes. A new file for each day is anticipated. Of considerable importance is the long term availability of this data and it's gathering and storage without gaps. As the remote sensors have little on board storage it is important that a
0
8262
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
8196
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
8637
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8364
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8502
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
7192
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
6122
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...
1
2623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1807
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.