Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB business object design query

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi,

I have a query regarding a business object I am designing. Its the simple scenario whereby I have a database holding products - a simple table - nothing special here.
I want to create a component used by client apps to add or modify to this table. The question is, what is the best design to encapsulate these objects - ie, should one class be the database handling stuff, and another be the product info. Or should i also include a collection class to handle them all.
If having 2 or maybe 3 classes, would you make only one of them creatable, the others being handled by it.
Further, the database is Oracle and requires username/password. The question with this is; if i handle the database connection through one class, then i cannot have the classes initialisation event establish the connection, because i need to pass it this username/password, therefore do I have to have it so that i have another method, say an "initDBconnection" method which the client application must call before doing anything...

I hope that all makes sense, it is more a design question, so ideas as to the best design would be much appreciated...I am new to COM & OO programming so any help is needed.
 
Good Question!

In general when I do this type of thing, I let the object know how to save itself. Then there are several things you can do from there. So your Product object would know how to save itself. Now weather it uses another object to do this is up to you. One thing to keep in mind is the number of conenctions that your DLL will open. (I'm assuming that you're placing all these objects in a DLL for reuse) Since you have to pay for connections on Oracle, it's best to keep it to one connection and have the objects use another object for the saving. This does a few things for you. First it limits your connections to the database to one connection. Secondly you don't have the connect and disconnect speed hits for each object.

Typically for a scenrio like this I would create an interface class called iDatabase and then implement this interface in the Product class. This gives you a standard to adhear to when implementing the base interface class. Since all properties and methods have to be implemented in the implementing class, you can do some nice psuedo-polymorhism with this approach in your application.

For an example of the iDatabase class, it might look like this:

.Add
.Delete
.Update
.RecordCount

These public subs/functions don't have any code in them. They are basiclly templates for the implementing class. Once you implement the base interface in the Product class, you write the code in the .Add of the Product class that tells the Product how to save itself to the database or wherever.

Multiple implementation works really slick. I had a project one time that dealt with objects that could be saved to a database or places on a map. I had two base interface classes.

iMap and iDatabase

iMap had
.X
.Y
.Z
.FileName

iDatabase had
.Add
.Delete
.Update

In my objects, anything that would be placed on the map had to adhear to the iMap interface, so that object would implement the iMap interface. If the object had to save itself to the database, it would implement iDatabase. If it could be placed on a map and saved to the database, it implemented both interfaces.

For your application you would custom code the .Add, .Delete and .Update method for each object. The nice approach to this is that the entire Add, Delete and Update is encapsulated in the object itself. If you decide to change the save method to a file or to communicate on a socket or something, the application that used your objects doesn't have to change!

There was an EXCELLENT Thread that was started almost a year ago on this topic that I highly recommend that you read.

Click below to follow the discussion.

thread195-1421 Snaggs
tribesaddict@swbell.net
 
beauty...thanks for that...that old thread is interesting reading too...

ta
 

Snaggs,

As I started developing n-tier applications almost 2 years ago, I couldn't quite understand the reason for implementing an interface that had nothing but empty subroutine -- I still don't :-0

In your example, the iDatabase object has a few methods (Add, Delete, Update, etc.) and by implementing that object in the "Products" class, we automatically have access to those methods except they're empty and we have to put our own code in those methods within the Products class. So regardless of whether we implement iDatabase or not, we still have to code those methods in the Products class.

My question is: What do we gain by having the iDatabase object?

Tarek

The more I learn, the more I need to learn!
 
VB400, That's a great question. Basically what it does is makes your object adhear to a specific set of rules. You are correct that the interface class typically will have no data in it. When I first saw it, it didn't make any sense to me either. Once I started working with it, it made more sense. I have a project that I found out on the net somewhere that shows how this is done. However I made some major changes to it to show how it can be implemented in a clearer fashion. The orginal author is: Lone Bach Christensen, dated back on July 25th, 1998. Click on my email address below and I'll send you a copy of the project and the classes. Be sure you let me know why you're emailing me. Request the "VB Poly Project". There are comments in the code to explain what is going on and how it all works. It's a demo in Polymorphism using Viusal BASIC that impletements a base class.
Snaggs
tribesaddict@swbell.net
I don't have a solution but I admire the problem.
 
Simon,

Your email address didn't translate correctly in the header of the message you sent me. Could you please include your email address in the body of the message?

Thanks, Snaggs
tribesaddict@swbell.net
I don't have a solution but I admire the problem.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top