views:

53

answers:

1

Hello, I have a DB and a mapping framework which was written by the company Im working for. So I have a class for each table in the DB and those classes allow to call the DB and get various information in DataSets or DataTables for example. Now I am supposed to write that information to a TXT file in a certain format (I have the specs how it should look like). Its about estates, there will be many estates in a file and for each estates there are about 40 lines in the file. I know how to write to a file and stuff, what I am looking for is a good approach how to build this functionality in general.

This might be too general to give good advice, but is there a proven way to do such things?

Thanks :-)

A: 

Let's call the classes that give you table info TableInfo objects

I'd create an interface IDBInfoWriter, with a method WriteDBInfo(TableInfo ti)

Then an implementation of IDBInfoWriter, say DBInfoFileWriter, with a FileWriter as a private member. Each call to WriteDBInfo would dump whatever in the file writer

Finally a DBInfoWalker object, which would take a list of instantiated TableInfo and a IDbInfoWriter

class DBInfoWalker
function new(TableInfo[] tis, IDBInfoWriter idbiw)
{...}

function process()
{
    for each TableInfo ti in tis
    {
        idbiw.WriteDBInfo(ti);
    }
}

This way you can

  • Work on any subset of TableInfo you want (lets say you want just a list of TableInfo beginning with "S", pass only this list in the constructor of DBInfoWalker
  • Create as many output styles for your tableInfo, just create the correct implementation of IDBInfoWriter (network, unique file, multiple files, etc)

Of course, that's just one possibility :)

Good luck

samy
I'd just make a DBInfoWriter class and give it a `TextWriter` to write to. The stream abstraction is already there, no need to duplicate it into your own classes.
tdammers
Indeed, but let's put aside YAGNI for one second :) the way i see it is that if i were asked to log DB Info, i'd still use an interface to benefit from future and potential changes in the implementation. If i'm asked to change the logging to another DB, i'd swap my instantiation of a DBInfoFileWriter to a DBInfoDBWriter. DBInfoWalker wouldn't have to worry about it. I know YAGNI, but since it's such a little change to make, i'd run with it
samy