From: Graham K. <gr...@da...> - 2004-04-01 08:47:27
|
Nat, Steve, I've been thinking this through and I'm afraid I'm still in the dark. Let's say I have a requirement to create users (say we're writing an admin tool for some other application). The username and password are filled in, some controller gets those, does 'User user = new User(username, password)' and then it must save it to the database. So 'user.save()'. In that save method I used to have JDBC, which for testing I mocked using the mock jdbc classes from mockobjects.com. But if I also have lots of other classes it gets tedious to write all that setup code for the mock objects, and this is why I'm trying to put my own interface between me and jdbc, so I can mock that interface instead of jdbc. What should user.save() do ? Thanks in advance, Graham. Steve Freeman wrote: > It sounds like you're trying to write this bottom-up, that is > implement a generic db layer and then apply it. You might want to > think about writing your persisitence services from the viewpoint of > its clients, one case at a time, and then refactoring. > > Take a case where you need some kind of persistence. Write an > interface that provides that persistence /in terms of the client/, > using mocks to get you through the test. Now that interface becomes > the client of the next persistence layer, gradually becoming more > general until you hit JDBC. Combined with Remove Duplication, and > Tell, Don't Ask, this should generate a persistence library. > > Either that, or use something like Hibernate... > > S. > > Graham King wrote: > >> I have been trying to abstract away database (JDBC) access using >> these two principles: >> http://www.mockobjects.com/wiki/OnlyMockYourOwnInterfaces >> http://www.mockobjects.com/wiki/MockableAdapter >> >> Not being able to find a practical example to plagiarise I tried >> writing my own. A database basically allows the 'put'ing of data and >> the 'get'ing of data (save / load or executeUpdate / executeQuery). >> So first cut is (with verbosity removed): >> >> public interface Database { // Or should it be DatabaseAdapter ? >> ReturnType get(String sql); >> int put(String sql); // Returns number of rows affected >> } > > > [...] > > > |