Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

 »  Home  »  Data Programming  »  Encapsulation in the Databases World
Encapsulation in the Databases World
by John Bailo | Published  03/01/2002 | Data Programming | Rating:
John Bailo

John Bailo is a Web Developer for Vestcom NW and has been developing n-tiered Internet workflow and e-commerce applications for several years.

 

View all articles by John Bailo...
Encapsulation in the Databases World

What is encapsulation?

One of the four properties of the object-oriented paradigm is encapsulation. While inheritance, abstraction and polymorphism are widely used in Object Oriented Programming (OOP), especially in Visual Basic, can we say that encapsulation is ever used?

Looking throughout the MSDN Library gives me this definition of encapsulation (from an article entitled the One Minute Terminologist):

Objects are encapsulated - that is, they contain both their code and their data, making them more easier to maintain than traditional ways of writing code.

The MSDN article Object Oriented Programming in Visual Basic.NET redefines encapsulation (incorrectly) as

Encapsulation hides the internal implementation of an abstraction within the particular object.

I believe the first definition is the true, and valid, pillar of true OOP, yet current design practice rarely builds encapsulated objects in favor of heterogeneous data structures on traditional relational dbms server systems.

Do we encapsulate?

One of the first programming languages I learned was FORTRAN (using JCL on punch cards), and until we learned file I/O we added input via the DATA command at the end of the program. I guess you could say my FORTRAN batch programs were encapsulated. In fact, they were more encapsulated than most of today's object oriented programs, which are rarely, if ever - contain data. My career in the last four years has been far from batch programming. As an n-tiered developer, I have worked in creating web applications and multi-tier client/server network applications that include the use of middle tier objects, component oriented clients in script and compiled code, the extensive use of external data sources.

The last part is what causes me some dissonance. Rather than embodying encapsulation, the progress in the last few years has been toward marrying external objects to traditional relational databases with various piping software (ODBC, JDBC and so on). These objects take the form of COM+ objects (originally MTS), which definitely assist in defining business logic. Certainly, one of Visual Basic's strengths is its readability as a programming language.

Modeling business logic or other systems in VB is infinitely superior to using machine like languages such as C/C++, or trying to use any non-OO scripting language (such as HTML). XML, the godsend of data markup languages, clearly create encapsulation and embody ideas of inheritance - but they have no procedural syntax; and while handy for passing information between machines, are of little use for data architectures of any kind.

Why data encapsulation?

Ok, so that's my criticism: encapsulation is rarely used, if ever, in Visual Basic n-tiered information systems. The next question is more positive. Why should it be used? Let's think about samples…lets say we are building a customer relationship management system, but we want maximum flexibility. We really want to know our customers. In fact, we are so hands on, touchy-feely (think Nordstrom) that we know each customer has different characteristics. The typical relational database response might be to include a "Comments " field in addition to name, address, the usual. So we get records that look like:

NameCityStateComments
John BailoKentWALikes color green

Well, that's not a very instructive thing, because it is hardly useable in code and presents problems for select statements, indexing, and so on. More than likely, the 'Comments' field will be input, but never retrieved or used. So what many DBMS tools then do is offer the database designer to add 'custom fields' by relating a table to the customer. For example. The table above becomes.

IDNameCityState
1John BailoKentWA

The custom fields table becomes

IDFieldValue
200Favorite ColorGreen
200Favorite Ice CreamVanilla

So at least, someone who takes the time to look at the custom fields table gets some idea of the types of information that have meaning to this customer. The problem here is the opposite of instance one, every time we have a customer with a special feature, we apply this field to all customers! Now, for my pitch for encapsulation.

Suppose that we keep the database for what it does well - modeling, searching and sorting homogeneous data structures. That is - it covers those things that we all have in common - names, addresses, cities, email addresses. Databases, however, get hung up in the details. My suggestion is that VB.NET COM+ components, for instance, are very good places to store the details - as the properties and methods of individual classes that can be applied down to the individual person (in the case of CRM).

A 'mixed' encapsulated and ado aware class in vb.net that might represent a customer might look like this:

Imports System
Imports Microsoft.VisualBasic

Public Class JohnBailo

    Sub getBasics()

        Dim connSql As SqlConnection
        Dim cmSql As SqlCommand

        Dim dremp As SqlDataReader
        connSql = New SqlConnection_
            "server=webserver;uid=sa;pwd=;database=license")
        cmSql = New SqlCommand("s_getAllCustInfo"connSql)
        cmSql.commandtype = CommandType.StoredProcedure
        Try
            connSql.Open()
            dremp = cmSql.ExecuteReader()
            Do While dremp.Read()
                Console.WriteLine(dremp("ename").ToString())
            Loop
        Catch err As SqlException
            Console.WriteLine(err.ToString())
        Finally
            connSql.Close()
        End Try

    End Sub

    
    Public Function getFavoriteColor() As String
        Return favoriteColor = "blue"
    End Function


End Class
Generated using PrettyCode.Encoder

Where getAllCustInfo is a sproc that retrieves all of the information in a customer table in SQL Server. The beauty of this, is that when we write a client application, we see or expose the getFavoriteColor. This allows heterogeneous data to be part of an individual class, which is also a COM+ component, which can be called or mapped to a unique customer key - but at the same time, not all customers are required to carry that data, if the data is not relevant to the customer! This model would look thus:

In many ways, the .NET Passport idea carries some of this thinking. A user logs on and carries his 'record' with him passing those data fields to an e-commerce or other sites.

To summarize, the little used concept of object data encapsulation, might be a way to help us store and retrieve non-standard, or heterogeneous data.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3 out of 5
 6 people have rated this page
Article Score10846
Comments    Submit Comment

Comment #1  (Posted by Roy Osherove on 03/03/2002)

HUH?
How far fetched is this?
Do you really think that getting some of the data from a database and some of the data from a PASSPORT service achieves true encapsulation?
If anything, it breaks encapsulation, and makes code harder to maintain.
btw,
When you quoted MSDN, by referring to "Object Data" this means "Object State".
What's important is that the State is encapsulated, but how the state is achived is a different matter.
You claim that this can be achived through 2 (if not more) different sources.
I'd like to see the Data retieval object which is responisble to do that. having to connect to both passport and a database? even is passport was in sucj a state that one could use it in such ways, the whole concept is a pain to do..

 
Comment #2  (Posted by Wenzel on 03/11/2002)

Yea... like a large department store is going to create 100,000 classes, one for each customer, and place them in an assembly...
 
Comment #3  (Posted by john bailo on 03/11/2002)

hi -- in response to the two comments, yes, the effort is high -- however, the point of my article is that it is interesting to start thinking of customer data as tailored to the customer, not the database, and for that
it requires extraordinary effort -- which in turn gives extraordinary results. In my mind, think of a technology
which is as detailed in its relationship to the data, as Nordstrom is in its relationship to customers. Rather
than 'pegging' data we build an assembly system which allows for individuality.
 
Comment #4  (Posted by Chance Seagle on 05/17/2002)

Are you sure you have been programming since the beginning of Fortran? Damn dude!
 
Comment #5  (Posted by john bailo on 05/21/2002)

'dude' yes, i have been, which brings me to the subject of Bailo's Law -- every so often, maybe a decade or so, the interface to computers becomes twice as easy, and the people who can program them can be half as smart ... see chart below...

Theoretical Turning Machine can be understood by Atomic Physicists
ENIAC, Programmable Switches can be understood by Highly Trained Engineers
Assembly can be understood by Genius Hackers
3rd GL Languages can be understood by Computer 'Scientists'
4th GL Languages can be understood by Systems Analysts
Visual Programming can be understood by Consultants
HTLM/Javascript can be understood by Skate Punks

after that its all voice commands and grandma's a code guru !


 
Comment #6  (Posted by anonymous on 07/16/2002)

I think there is some confusion here between encapsulation and data access. Encapsulation is geared more towards "hiding" the underlying code from other objects. If we go by the idea of developing data-centric applications, you will be violating another oop concept called Polymorphism. Polymorphism is what make us "think" as developers and not DBA's. This is the driving force behind maintaining the code standardized. As far as encapsulation goes, it is usefull for other ideas in VB such as hiding private methods and variables.
 
Comment #7  (Posted by john bailo on 07/16/2002)

you may wish to read:

http://www.gnacademy.org/text/cc/Tutorial/node4.html#SECTION00421000000000000000

this article at least focuses on the 'data structure' being encapsulated within the object.

i believe also that objects can (and should) encapsulate data -- because the object (i propose) becomes the 'database' or rather the database has now been 'exploded' into objects -- in the same way that functionality has been 'exploded' from monolithic programs, the data can now be 'exploded' from records confined within a monolithic schema to individual records with their own schemas and data.


 
Comment #8  (Posted by Stephen_DuVal on 01/19/2003)

A DBMS is a huge repository of global variables. The real issue for encapsulation of DBMS data is the trade off between the convenience of query access and the maintanance nightmare of global variables.

The DBMS is a huge repository of global variables accessible not only by the entire code base but also by reports/queries written by the user community. When the DBMS is accessed directly throughout the code base, any attempt to modify the schema degenerates into a search for all locations where the code touches that portion of the schema. Usually reports/queries created by the user community are ignored. After one or more revolts by the user community, schema changes are limited to additions which eventually results in systems which become next to impossible to maintain.

OO encapsulation requires that a component (probably multiple classes) be created to hide a portion of the schema and that all access to that portion of the schema flow through the component. This pattern requires that the tables in the schema be partitioned into non overlapping sets and that a component is used to implement each set of tables. This greatly simplifies the schema maintenance issue. However, without query tools capable of executing against the component's API, this pattern is not practical.

The code base will not have a problem accessing the database via the component's interface but user written reports/queries are a different issue. Until tools such as Crystal Reports are able to access DBMS data via a schema implemented via an API, this problem remains difficult to deal with.
 
Comment #9  (Posted by an unknown user on 05/12/2005)
Rating
It is well written, and I used the information
 
Comment #10  (Posted by an unknown user on 05/31/2005)
Rating
Excellent and informative discussion.
 
Sponsored Links