Sign in to follow this  
alnite

What's the proper name for this class?

Recommended Posts

alnite    3436

I'm writing a class that inserts/updates or deletes records to a database table.  I can't come up with the proper name for this class.

 

DBInsertor? Sounds pretty stupid.

 

DBRecord?  It's ambigous as it doesn't represent any DB record or model.

 

DBRecordKeeper?  It doesn't keep or store records.

 

Any ideas?

Share this post


Link to post
Share on other sites
alnite    3436

I would have a text file with the values to be stored to the database.  For example:

 

first_name: "John"

last_name: "Smith"

 

Then it reads this text file, parses the column names, checks if the column names actually exists in the database, then stores them.  It creates a new entry if the values do not exist.  It updates the entry if it already existed.  It can also delete an entry based on a certain criteria, for example, by first_name.

 

So I would use it like this:

 

// Initialize db bla, bla bla

db_whatever.insert("datafile");

db_whatever.update("datafile");

db_whatever.delete("first_name", "John");

 

I was thinking DBRecord sounds the closest, but it doesn't represent the data.  It inserts data into database.

 

edit: It may also extract data from database.

Edited by alnite

Share this post


Link to post
Share on other sites
alnite    3436

Smells pretty anti SRP to me...

 

Single responsibility principle?  I would say it is, at method-level, rather than class-level :D

 

 

anyway, from the methods shown, DBContext, DBConnection, or just Database?

 

DBContext and DBConnection doesn't sound bad.  I was looking at C# documentation, and came across: TableAdapter or DataAdapter, which might be a good candidate too.

Share this post


Link to post
Share on other sites
Polarist    172

DBRecordInserterUpdaterAndOrDeleter

 

I vote for this.  Clear and to-the-point.

 

But honestly, if you're set on that set of functionality (file i/o AND database query), then you'll want a fairly non-standard name to denote that it carries a non-standard role.

 

I would have suggested something like DBBasicConnection because of it's similarity with a standard idea of a Connection UNTIL you mentioned that insert() and update() functions take filenames, that kind of throws standard out the window.

Share this post


Link to post
Share on other sites
Hiwas    5807

If you take this from a top down manner:

 

It relates to databases which suggests to me a "database" or "db" namespace instead of trying to work that descriptive bit into the name directly.

 

It does three things which suggests this is a management class or a supervisor or some controller type class which is top level to decide what functionality to perform.

 

I tend to use 4 variations of description based on ownership level:

1)  The "manager", owns the item in question and can perform the suggested operations directly.

2)  The "controller", doesn't own the item but tells other systems what is to be done.

3)  The "supervisor", which is simply a decision engine around the item in question.  (This is a subtle difference from controller.)

4)  The "processor", which is used to run through many items and add tags, make lists, whatever for other things to deal with.

 

So, given the original question, I would start by using a namespace to wrap the concept instead of trying to put it in the class name.  The language was not specified but the names suggest a C/C++ background so that's what I'll assume.

 

namespace db/database { class Record[xxx] }

 

Insert proper descriptive item into "xxx".  I'm not much of a person for coding standards but I do like logically organized naming conventions.  Hopefully my description of the way I name things might help.

Share this post


Link to post
Share on other sites
dmatter    4828
These kinds of classes are common in enterprise applications and web development.

If, as you say, this class is providing Create, Read, Update & Destroy (CRUD) operations on a table then you have implemented the Data Access Object (DAO) pattern and I would stick those 3 letters on the end of the class e.g. "HighscoreDao".

There is another, related pattern that operates at a slightly higher level of abstraction from a DAO, it called the Repository pattern. A repository maps between some kind of persistent storage and collections of domain objects, for example a "MonsterRepository" might have to collaborate between multiple database operations in order to provide calls such as getMonstersForLevel.

Share this post


Link to post
Share on other sites
Bregma    9202

Finding the verbs in your system and creating classes for them is the classic procedure-oriented antipattern.  Not that there's a lot wrong with procedure-oriented design, unless what you're trying for is object-oriented design so you can take advantage of many of its long-term benefits.  Using classes and object for procedure-oriented design will bring you and the people who have to maintain your code grief.

 

In good OO design, you look for the nouns (object) in your system and create classes for them, then look for the verbs that act on the nouns and create functions for them.  If you're using your noun in the subjective case, you probably have a member function, and if it's in the objective case, you probably pass it as a function argument.  Not a hard-and-fast rule.

 

Using an agent noun ("eg. a 'manager' is something that performs the action denoted by the verb 'to manage') in place of the verb itself for a class name is still procedure-oriented programming.  Classes are nouns, functions are verbs.

 

My guess is that the original requirement was to be able to insert, remove, or update rows in a table in a relational database, or maybe (key, value) pairs in a persistent lookup table (what the folk who learned all their computer science off marketing whitepapers call a nosql database).  I image there are various additional verbs for those nouns for performing metaoperations (like connecting to the RDBMS, fetching a row, committing a transaction).

 

Just remember: nouns are classes, verbs are functions.

Share this post


Link to post
Share on other sites
LorenzoGatti    4443

I usually find this sort of class that encapsulates all SQL operations called a DAO; they cover the whole spectrum from dumb and generic (often simple CRUD operations, with the extreme of supporting query builders that write WHERE clauses on the fly) to complex and specialized (e.g. methods executing multiple or different SQL statements depending on data). The common trait is avoiding, or at least abstracting and minimizing, "business" logic that goes beyond accessing data and treating SQL errors.

 

Typical naming patterns refer to the database tables, under the assumption that different tables have different purposes and DAO changes follow table chages.  For example a DAO that operates on the Widget table only can be called "WidgetDAO", while one that combines access to the Widget, Gadget and Screw tables (either because they are considered a logical grouping or because they are actually used together) could be called "MechanicalWorkshopDAO".

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this