Michael Flanakin's Web Log

Comments and complaints on software and technology in general

  Home  |   Contact  |   Syndication    |   Login
  159 Posts | 18 Stories | 123 Comments | 497 Trackbacks

News

This weblog is no longer being maintained. For the latest, check out www.michaelflanakin.com!

Article Categories

Archives

Post Categories

Image Galleries

Miscellaneous

So, I'm about to create my data access layer... *flashback sequence begins* ...and I reach for my Patterns of Enterprise Application Architecture book. *flips the cabinet door open to see an empty space where the book used to be* Damn...I left it at home. Well, what am I to do? Let me just move over a couple of books and grab my Core J2EE Patterns book (admittedly, still in my to-read stack). I figured this was as good of a time as any to get to know the DAO pattern and how it compares to the TDG pattern. So, I read through the pattern description and I get a little excited. I realize that the only difference in the two patterns is the implementation. While I don't like the idea of tacking on “DAO” to an object name, I figure I'd make a hybrid of the two. *flashback sequence ends*

The fundamental differences between these two patterns are pretty much null and void when you consider that pattern implementation is usually different than the prescribed means. Here are a few of the pros (in my opinion)...

  • String constants for insert, select, update, and delete statements
    • Pro: One place to update table structure changes
    • Con: In methods, you don't know the order of values that need to be inserted (i.e. in .NET, using the String.Format() method to insert values)
  • Method names that match common data access terminology (create, read, update, delete - aka CRUD) and are not specific to any data source
    • Pro: Sets a naming standard for the pattern
    • Con: ???

Some of the things I didn't like about the TDG pattern were that the method names weren't consistent (where did "find" come from?), methods seemed to be based on SQL (use of "insert" method), and the method signatures didn't suggest the use of objects.*

* The use of objects is very beneficial when passing data through tiers because it allows you to abstract away from the exact data that needs to be transferred. For instance, based on the TDG pattern description, consider the insert method: PersonGateway.insert(lastName, firstName, numberOfDependants). These are the three properties of the Person class, but I don't know why a class instance wasn't passed instead - for instance, PersonGateway.insert(person). Now, consider adding an SSN. With the implementation as-is, you would have to add another PersonGateway.insert() method allowing for the SSN to be passed. And, what if the SSN is required? Then you would have to deprecate the old insert method. However, if you use a class instance, we only have to change the internals of the insert method - no new or deprecated methods. Obviously, this would be a much cleaner implementation.

Also, let me say that the TDG pattern seems to be a bit higher level. It represents the idea of having an object to act as a gateway between business and data. There isn't a lot of guidance for naming conventions or implementation ideas, which I think is important for identifying/using patterns in existing software. As such, you could say that the DAO pattern is just a lower level TDG pattern.

You'd think that this would solve all of my problems, but it doesn't. I like the idea of the Gateway pattern (not the Table Data Gateway pattern) because it is abstract and identifies a way to interface with any type of back-end. Because of this, I think it's important to look at intermediaries as gateways. Now, all of my development is usually in .NET, but I sometimes dabble in Java. I would like to have one standard set of patterns to use, but these two conflict. In my mind, the gateway pattern works in all situations; however, it is pretty much standard practice to have a PersonDAO class as opposed to a PersonGateway class. I don't like deviating from the standard, but at the same time, I don't like having two standards for the same practice.

I've talked to Dan Malks, one of the co-authors of Core J2EE Patterns, and he said that they talked with Martin Fowler when making the book. My question is: Why are there conflicting patterns? And, can we identify the right (read: best) solution so there's only one? I'd like to see the DAO pattern reference the TDG pattern (or maybe just the Gateway pattern). This would identify the relationship and developers would be able to identify each implementation as it was used in projects. This doesn't necessarily get rid of one or the other, which could cause naming conflicts, but at least its a step in the right direction. In my experience, most Java developers don't even know about the TDG or Gateway patterns.

posted on Monday, March 08, 2004 6:22 AM

Feedback

# re: Table Data Gateway vs. Data Access Object 3/16/2004 6:18 PM Ray Tayek (rtayek@freightgate.ne
hi, trying to figure out a domain later architecture myself. i'm mostly a java type (but not egb) currently reversing a bunch of similar databases using middlegen into hibernat. my boss just told me that he wants the data source to be able to be a web service, so i figure i need a data access layer. so i am puzzling over these poeaa patterns also.

you say: "... the TDG pattern seems to be a bit higher level ..." (than a dao).

seems like it's the other way around since a tdg knows the table structure for the db. so it would seem less abstract and have more implementation details exposed than a dao. sun says this about a dao at: http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html where they say: "... Solution use a Data Access Object (DAO) to abstract and encapsulate *all* access to the data source. ..". but of course .net is going to be very different. iirc, they have their on dao or doa or something like that.

naict, in poeaa terms, hibernate gives me a dao (active record), data mapper, lazy load, and query object. so i am looking around for stuff on data access layer.

thanks



# re: Table Data Gateway vs. Data Access Object 3/17/2004 5:36 PM Ray Tayek
> ...Fowler also provides a base Gateway Pattern
> (http://www.martinfowler.com/eaaCatalog/gateway.html),

yes

> is a bit higher level.... You can have a PersonGateway that talks to
> a database, web service, or ...

sure
>
> Beyond the specific pattern you decide to implement, you
> should know that these aren't intended to be language- or
> platform-specific.

Good

> ... like the general idea of the Gateway
> Pattern Family more than anything I've seen from Sun or...

It seems sorta like a mini- or part-of-a dao pattern

> however, that the detailed implementation of the DAO Pattern
> was better than the Gateway Pattern Family implementations.
> This is why I have chosen a Gateway Pattern implementation
> that simply uses examples from the DAO (i.e. method names and
> string constants for SQL statements).

got it.

> ...I'm hoping that, as I work with
> others, people will give me their opinions and I can try to
> hone better pattern implementations. ...
> I'll probably post on these issues at some point in the future.

please do.

i am a newbie to biz stuff and db's (and an oo pattern bigot :). sems like the objects that hibernate gives us are just table pojo's (like the dto's that the dao/ejb uses). And with the hibernate session the become something halfway between a gateway (to a hidden rdbms) and a dao. I need to add a web service as data source. this seems like it cuts through the middle of what hibernate provides, so i am puzzling how to wrap this up as a dao. going to read the hibernate forum on application arhitecture now.

Good luck with your project and thanks for your assistanc.



# re: Table Data Gateway vs. Data Access Object 3/29/2004 3:45 PM Ray Tayek
.> .. When I was looking at these two, it was only for database access, which is why I only referenced the Table Data Gateway Pattern. Fowler also provides a ... Gateway Pattern ..., which is a bit higher level. Essentially, they will all work the same way, ...

been reading more comp.object and hinernate forums (so now i am really condused :). re-reading you post was helpful. seems like the tgw is "higher layer" and gateway is "more abstract" with dao somewhere in the middle.

>Beyond the specific pattern you decide to implement, you should know that these aren't intended to be language- or platform-specific.

yes

>You suggested that .NET might "have their own DAO," but that's not the case. ... I like the general idea of the Gateway Pattern Family more than anything ... I do think, however, that the detailed implementation of the DAO Pattern was better than the Gateway Pattern Family implementations. ...

where can one find these implementations (other than sun's and the ociweb thingie and .not's dao)?

thanks

# re: Table Data Gateway vs. Data Access Object 12/18/2004 6:21 PM anonymous
>These are the three properties of the Person class, but I don't know why a class instance wasn't passed instead - for instance, PersonGateway.insert(person)

If you are thinking that way, you should check out Domain Model with Data Mapper in Fowler's book.

# re: Table Data Gateway vs. Data Access Object 1/2/2005 6:41 AM Ray Tayek (rtayek)
michael - i have fowlers book and will re look at this.
anonymous - ditto.

i recently got back on this project and looked at the caveat emptor sample and checked out the dao code that hibernate synchronizer generates. these both turned out to be similar to what i was roling up so i refactored mine to use a hacked up HibernatUtils class and all my tests still run. joes code has more layers than caveat and was a real eye opener. as far as how to structure the inheritance (i was close, but confused about it)/

naict, with hibernate: pojo=dto=domain object (more or less). so i am thinking up at the next layer as most of the hibernate stuff makes sense now.

i have fine grained dao's (one per table) and the phb wants *one* domain object per table. but some of our db's have slight variations of tables (extra field, different colimn name , type or length).. so maybe extracting some interfaces, making some subclasses and factories for dao's and pojo's is a way to go.

thanks

# re: Table Data Gateway vs. Data Access Object 8/31/2005 5:52 AM Hasan..to ..anonymous - ditto.
I m newbie to hibernate but i found hibernatesynch really very gd but i am still facing difficulty how to figure out the use of those daos with in my web application as i am using struts, so instead going thru pojo class with getter setter and a config file i want to use this code can u tell me plzz, where should i put my business logic into those codes, and use them in action.

thankx
Hasan

# re: Table Data Gateway vs. Data Access Object 1/24/2006 12:58 AM elguaro
There are two decisions to make here. One is TDG vs DAO and the other is TDG/DAO vs Service Gateway (I'm calling Fowler's "Generic" Gateway a Service Gateway).

Lets start with the first one: TDG vs DAO.

I sometimes feel a DAO is overkill because a DAO is usually accompanied by a Factory. This combination is useful when you want to be able to switch between different data sources. i.e. you are accessing a legacy system that is getting replaced. DAO's and Factories will let you transition between the two data sources transparently.

One of the forces for DAO's is: "You want to provide a uniform data access API for a persistent mechanism to various types of data sources, such as RDBMS, LDAP, OODB, XML repositories, flat files, and so on." (from the Core J2EE Patterns book).

So if your data source is staying where it is now then you don't need DAO's. In this case TDG might be better suited. You can never guarantee with 100% certeinty that your data will not be moved somewhere else, but in many situations this will be a safe bet. i.e. an application using it's own SQLServer 2005 database in a Microsoft shop.

Now decision number 2: DAO/TDG vs Service Gateway.

I find that if I'm dealing with a domain object I use DAO's/TDG's. i.e. for a Customer domain object I would have a CustomerDAO/ CustomerGateway depending on the decision made before.

But sometimes you go to the Data Source to perform operations on more abstract concepts. i.e. a Workflow. A Workflow might very well be a domain object, but the way I'm looking at it here it's just a bunch of operations that let me move from one state to the next. The workflow itself doesn't exit, it's not an Entity. But I still want to encapsulate database calls to transition from one state to the next or find out who can do what on a given state, etc.

In this case I go with a Workflow Service Gateway.

This is how I'm looking at things anyway ... I might realise tomorrow I got it all wrong ...





# re: Table Data Gateway vs. Data Access Object 3/20/2007 3:24 PM philmee95
Don't play in Java much, mostly php and c# but as was mentioned, they are patterns for enterprise architecture, and not a specific platform. Anyways, I probably just wasted a lot of time and effort thinking about the best solution, so I just implement both styles GatewayDAO.update(x, x, x, x, x) and GatewayDAO.Update(Object). Either on of the methods just calls the the fully implemented one (whichever I fealt like building that day). They are ovverloads in .net, and just 2 methods in PHP (no overloads). I usually try to make the update methods static as a habit. I also have save() methods on instance objects that just checks if the object is new, modified or deleted and calls one of the static CRUD, well CUD methods. I got that IsDirty and Save() ideas from expert C# business objects (Lhotka). I never implmented the whole clsa framework, but it did help my object oreintednessessess, as well as confuse me further.

Post Feedback

Title:
Name:
Email: (never displayed)
Url:
Comments: