Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Catalyst: Users

Where is the DAO equivalent?

 

 

Catalyst users RSS feed   Index | Next | Previous | View Threaded


jarom at jaromsmith

May 27, 2009, 9:59 AM

Post #1 of 5 (733 views)
Permalink
Where is the DAO equivalent?

Hello Catalysters:

First of all, a thousand apologies if this is a FAQ (and if it is,
please point me in the right direction). I have been programming in
Perl for a while but I freely admit there's a lot I don't know, which is
why I am here. I have completed the Catalyst tutorial, purchased the
book (which I am in the process of reading/completing) and I have
scrutinized all sorts of web pages trying to figure this out, but have
so far been unsuccessful.

Here's the problem I'm trying to solve: I have a partially-written web
application which I am trying to port to Catalyst. My app has DBI
methods to access the DB and return results -- things like
get_vendor_by_name() or get_option_list(). In Java, which is the
language I use most often these days, I would think of them as DAO
methods. Some of these methods are fairly trivial to replace in
Catalyst; for example, get_vendor_by_name now becomes
$c->resultset('Vendor')->search({name => $vendor_name})->all; However,
some of these methods are more complicated/complex and the proper DBIC
invocation to produce an equivalent result to my DBI method is more than
I would want to try to remember or replace each time I need to perform
that query. I don't mind re-writing the methods to use DBIC instead of
DBI (in fact, I kind of look forward to it), but I would like to
continue to have complex or semi-complex queries abstracted into their
own methods which I can re-use easily. The problem is that I don't know
where to put them or how to access them.

So, my question is: In a Catalyst context, where do I put these kinds of
"helper" DB methods and how do I access them? (both from within a
Catalyst app, as well as from the command-line like in a Cron job or
whatever...) Some actual specific examples would be great. I'm sure
there is a way, and I'm guessing it involves the model somehow (possibly
by creating a new model class?) but I haven't been able to figure it out
yet. I tried adding methods to the classes created by Catalyst/DBIC in
the Schema/Result directory, but gradually it dawned on me that methods
placed here are only dealing with a single entity of that type. To go
back to Java-speak, it seems like the stuff in Schema/Result are DTO
methods; what I'm trying to figure out is how/where to put the DAO methods.

Help! :-) Thanks.

jarom smith
tech go-to guy


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


tobias.kremer at gmail

May 27, 2009, 10:52 AM

Post #2 of 5 (677 views)
Permalink
Re: Where is the DAO equivalent? [In reply to]

Hi Jarom,

On 27.05.2009, at 18:59, Jarom Smith wrote:
> $vendor_name})->all; However, some of these methods are more
> complicated/complex and the proper DBIC invocation to produce an
> equivalent result to my DBI method is more than I would want to try
> to remember or replace each time I need to perform that query. I
> don't mind re-writing the methods to

I suppose you want "Predefined searches":

http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Manual/Cookbook.pod#Predefined_searches

If you don't want to manually set your ResultSet classes with -
>resultset_class take a look at load_namespaces() which will
automatically do this based on two distinct namespaces for
ResultSources and ResultSets:

http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Schema.pm#load_namespaces

By the way, there's also a dedicated DBIC mailing list for which this
question is probably better suited :)

HTH,
--Tobias

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


plu at pqpq

May 27, 2009, 12:24 PM

Post #3 of 5 (669 views)
Permalink
Re: Where is the DAO equivalent? [In reply to]

On 27.05.2009, at 18:59, Jarom Smith wrote:

> So, my question is: In a Catalyst context, where do I put these
> kinds of "helper" DB methods and how do I access them? (both from
> within a Catalyst app, as well as from the command-line like in a
> Cron job or whatever...) Some actual specific examples would be
> great. I'm sure there is a way, and I'm guessing it involves the
> model somehow (possibly by creating a new model class?) but I
> haven't been able to figure it out yet. I tried adding methods to
> the classes created by Catalyst/DBIC in the Schema/Result directory,
> but gradually it dawned on me that methods placed here are only
> dealing with a single entity of that type. To go back to Java-
> speak, it seems like the stuff in Schema/Result are DTO methods;
> what I'm trying to figure out is how/where to put the DAO methods.

http://www.pqpq.de/mt/2009/05/catalyst-dbic-dao.html

Cheers,
plu

--
Johannes Plunien | mailto:plu [at] pqpq | http://www.pqpq.de/contact/
Attachments: smime.p7s (2.38 KB)


jarom at jaromsmith

May 27, 2009, 5:05 PM

Post #4 of 5 (667 views)
Permalink
Re: Where is the DAO equivalent? [In reply to]

Thank you Tobias! That is what I was looking for.

I note that Catalyst has automatically created a Schema.pm with
__PACKAGE__->load_namespaces(), so all I needed to do was create
ResultSet classes corresponding to my Result classes, and everything
else just worked.

jarom smith
tech go-to guy


Tobias Kremer wrote:
> Hi Jarom,
>
> On 27.05.2009, at 18:59, Jarom Smith wrote:
>> $vendor_name})->all; However, some of these methods are more
>> complicated/complex and the proper DBIC invocation to produce an
>> equivalent result to my DBI method is more than I would want to try to
>> remember or replace each time I need to perform that query. I don't
>> mind re-writing the methods to
>
> I suppose you want "Predefined searches":
>
> http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Manual/Cookbook.pod#Predefined_searches
>
>
> If you don't want to manually set your ResultSet classes with
> ->resultset_class take a look at load_namespaces() which will
> automatically do this based on two distinct namespaces for ResultSources
> and ResultSets:
>
> http://search.cpan.org/dist/DBIx-Class/lib/DBIx/Class/Schema.pm#load_namespaces
>
>
> By the way, there's also a dedicated DBIC mailing list for which this
> question is probably better suited :)
>
> HTH,
> --Tobias
>
> _______________________________________________
> List: Catalyst [at] lists
> Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
> Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
> Dev site: http://dev.catalyst.perl.org/

_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/


bobtfish at bobtfish

May 28, 2009, 5:18 AM

Post #5 of 5 (658 views)
Permalink
Re: Where is the DAO equivalent? [In reply to]

Jarom Smith wrote:
> Hello Catalysters:
>
> First of all, a thousand apologies if this is a FAQ (and if it is,
> please point me in the right direction). I have been programming in
> Perl for a while but I freely admit there's a lot I don't know, which is
> why I am here. I have completed the Catalyst tutorial, purchased the
> book (which I am in the process of reading/completing) and I have
> scrutinized all sorts of web pages trying to figure this out, but have
> so far been unsuccessful.

Right! Your question has been answered in the rest of the thread, which
is great.

Please please please submit a doc patch which contains the appropriate
pointers to the info you want, where you tried to find that info...

This is obvious to anyone who knows DBIC, but obviously not clear to
beginners. Please point out where we should document it so that the next
person doesn't struggle!

Thanks in advance.
t0m


_______________________________________________
List: Catalyst [at] lists
Listinfo: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/catalyst [at] lists/
Dev site: http://dev.catalyst.perl.org/

Catalyst users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.