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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Classes question 1

Status
Not open for further replies.

DeanConsulting

Programmer
Jan 11, 2002
131
0
0
US
Which is best practice:

Create class that maps a piece of data for a database. This would be properties only. (i.e. fields in the datbase table).

Then create a new class and inherit the class from above (the on that contains just the properties) and add the CRUD code to the new classclass.

or...

Do it all in the same class.


---------------------------------------
Noble D. Bell
 
Generally speaking if the architecture requires that the entities are tightly coupled you would place the CRUD in the same class as static methods.

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
which is the best practice? That all depends on context.

There are 3 primary approached to accessing data.
1. transaction scripts
2. Data Table Gateways
3. Domain Mapping

depending on the complexity and needs of a context will determine which is the best approach.

transaction scripts are usually monolithic sql statements. stored procedures are the first thing that come to mind, but not the only type of scripting. This works well with simple/no logic when entering data into the database, ETL procedures and reporting.

data table gateways have a single domain object mapped to table. this object has commands which work against a single or set of rows for the table it's mapped to. If your domain objects have a 1:1 mapping with the database this can be a good approach. The most common implementation of this pattern is the ActiveRecord pattern. the .net framework also has an implementation of this in the form of DataSets and DataTables with DataAdapters.

Domain mapping is the most complex approach and is best suited when the domain is complex. there is not a 1:1 mapping of domain objects to database tables. example include accounting and logistics. the idea is your domain objects are suited to service the business problem. they don't have any knowledge of the database. To map the domain objects to the database you have another set of objects responsible for that. if your domain heavily leverages the patterns and practices of OOP then Domain Mapping is an excellent choice. the 2 biggest frameworks I have seen that solve this problem are Hibernate (java) and Nhibernate (.net)

the book Patterns of Enterprise Application Architecture details these 3 approaches to data access. I highly recommend this as part of your research.

One other note. if you are dealing with any of the major programming languages (.net, java, ruby, php) then your data access problems are already solved. There are a number of commercial and open source frameworks which implement these concepts. It would need to be an extremely edge case to require you to create a data access layer from scratch.

as to whether your classes should be 1 super class or use inheritance. I would favor composition over inheritance whenever possible. I would ask the question do you want a single object to manage the domain and database operations, or do you want to separate these concerns. Depending on the complexity of the domain and the context for which it's used will determine the best approach.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
One thing that I failed to mention is that some of the work I am doing is in a limited resource area of smart phone and pocket pc development. With limited memory resources to work with using some of these patterns can be quite taxing on the system.


---------------------------------------
Noble D. Bell
 
how can a pattern be taxing?

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
what would probably the most taxing then is how much data you store in memory and how often you need to preform remote operations (remote devices, files, systems). not the method which the data is retrieved.

depending on the language used to design the software there may be features/objects which help manage memory or drive the architectural design.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
My advice with most object oriented designs is: try to see the responsibilities of the class.

So your first class is some kind of container. its responsibility is to hold data.

Your second class is a container as well (because it inherits that), but has more responsibility in the sense of the CRUD routines.

Personally, I don't like self-storing containers. It has all kinds of nasty chicken-and-egg situations. Can a container delete itself? No. You are holding a reference to it to be able to give the delete command, so it can delete its representation in the database, but not itself. Can it create itself then? How?
For existing (in the database) objects, I would have them reflect the correct (database) state directly after construction. I mean, this is it's "identity" state, that is what defines the object. Having to construct an empty container and then command it to "go load yourself" just does not feel right. You might forget that command or give it when changes were already made.

So, in my opinion, CRUD routines should never be on the container itself.


+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Don, great insight thank you!

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Very informative insight Don. I would like to get your insight on having static/friend methods of the container class to implement the CRUD or controlling type methods? The implementation of static/friend methods would follow the objects of type class as opposed to the object itself. I realize that I may have been ill informed but it was this point that I have used in designs to implement controlling methods of the class. Any thoughts?

If you choose to battle wits with the witless be prepared to lose.

[cheers]
 
Long time ago, I wrote an article on the subject to order my thoughts. It is quite old and I implement some different ideas now, but it may still be a good read:


Note that for the "Modifying properties" section, I now give the persistent objects both the original and the actual values, so it can tell the collection which properties have actually changed, and leave the updating to the collection.


+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top