Should I put functionality to retrieve data related to a bean in the bean or a DAO?

37 views
Skip to first unread message

Thomas Messier

unread,
Jun 12, 2012, 9:41:54 AM6/12/12
to coldfu...@googlegroups.com
Say I have a scenario where I have users and products. I need functionality to recommend products to a user and that functionality is gonna run a complicated query to determine what products to recommend. I'm using CF's ORM functionality and the query can't just be a simple mapped relationships, it's going to need a custom query. My question is, do I put that query in my user DAO or do I put it in my bean as a method? Or is it largely down to preference/use case? I figured if I think I'll only ever use this function when also retrieving a user, it might makes sense to just put it in the bean. On the other hand, this is data access so maybe it doesn't belong in the bean? I guess I could put it in a DAO and call the DAO from the bean, but then I'd need to inject the DAO into the bean, which gets cumbersome seeing as this is running on the built-in ORM and I don't think the dependency injection frameworks out there can inject into an ORM bean. Otherwise seems like I'm falling into anemic domain model land and will have beans doing mostly getting/setting, with the occasional calculation.

Suggestions?

Denard Springle

unread,
Jun 12, 2012, 10:40:27 AM6/12/12
to coldfu...@googlegroups.com
I would probably build a service for this. In this way you call the service which calls your query (which can be in the DAO/DAG to keep the queries together, or right in the service if you prefer) and populates your beans for the presentation (view) layer. In this way you're not generating a dependency in the user bean with a heavy query that probably only needs to run once in awhile, whereas you'll use your user bean for far more than that one query (e.g. to add, edit, etc. users, access controls, authentication, etc.) and you're not having to inject anything into the bean, which is a bit counter-intuitive - generally speaking. And for the record Coldspring and ORM do play nice, nice together ;)

-- Denny


On Tue, Jun 12, 2012 at 9:41 AM, Thomas Messier <thomas....@gmail.com> wrote:
Say I have a scenario where I have users and products. I need functionality to recommend products to a user and that functionality is gonna run a complicated query to determine what products to recommend. I'm using CF's ORM functionality and the query can't just be a simple mapped relationships, it's going to need a custom query. My question is, do I put that query in my user DAO or do I put it in my bean as a method? Or is it largely down to preference/use case? I figured if I think I'll only ever use this function when also retrieving a user, it might makes sense to just put it in the bean. On the other hand, this is data access so maybe it doesn't belong in the bean? I guess I could put it in a DAO and call the DAO from the bean, but then I'd need to inject the DAO into the bean, which gets cumbersome seeing as this is running on the built-in ORM and I don't think the dependency injection frameworks out there can inject into an ORM bean. Otherwise seems like I'm falling into anemic domain model land and will have beans doing mostly getting/setting, with the occasional calculation.

Suggestions?

--
You received this message because you are subscribed to the Google Groups "Object-Oriented Programming in ColdFusion" group.
To view this discussion on the web visit https://groups.google.com/d/msg/coldfusionoo/-/x6uqWeXe9VkJ.
To post to this group, send email to coldfu...@googlegroups.com.
To unsubscribe from this group, send email to coldfusionoo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/coldfusionoo?hl=en.

Reply all
Reply to author
Forward
0 new messages