A Funny Java Flavoured Look at the World

Monday, October 23, 2006

I just found I have been using a Data Access Interface

I learnt a new term today and I talked about this a little bit during my rant on my blog entry

The benefits of writing modular code


The new term I have learnt is Data Access Interface. It is basically the idea of hiding any data you are accessing which could be a config file, an xml file, a database and hide this data away behind an interface. The article I read also stated you can have service interfaces which do things where as Data Access interfaces are used to access a data source.

One example that popped into my mind when I read this was the interfaces used for databases. By putting all the database connection, reading code behind an interface(s) (like hibernate) you can then swap the database later, write some more code using the interfaces and then the rest of your code won't even know about this.

I also use this technique when I am testing. I often create an interface and then write some hard coded class with the data in that extends the interface. I can then use my hard coded class to test my code and when it is passing the tests, I write the code that actually gets the data from somewhere, which is usually some kind of file the user can change. The rest of the code is just working to an interface so it effected by the change of data source. This is also useful because if the source changes later on then it won't result in much code being changed because you have the interface protecting against code changes.

I suppose that also the opposite is true in the circumstances you want to change the output of some code, which might be making a report. If you have an interface for the result you can then easily put in some code which creates a different output, xml, html, csv. As long as the different reports are being encapsulated by an interface the rest of the code won't be affected.

I find it interesting when someone has labeled a practice that you have been doing but didn't really have a name for. Finding the label makes it easier to talk explain to other members of your development team what code you are writing or what practices you find useful.

0 Comments:

Post a Comment

<< Home