[Catalyst] Passing connection info to an custom model/external lib

Byron Young Byron.Young at riverbed.com
Fri Apr 10 19:42:29 GMT 2009


Eric Wright wrote on 2009-04-10:
> I have what I hope is not a dumb question. I'm trying to follow best
> practices here and am writing a piece of business logic that I feel
> should be Catalyst agnostic and therefore am creating a custom model.
> (However, at the moment Catalyst is the primary consumer of this lib.)
> I'd like to use my DBIC::Schema goodness for DB access and happily have
> abstracted that via Catalyst::Model::DBIC::Schema. The thing is I
> realized that if I connect to this schema outside of the scope of
> Catalyst via DBIx::Class::Schema->connect I have to pass in the
> connection info separately which really bugs me because I've already
> defined it via a configuration file for the Catalyst app (using
> Config::General). I suppose I could just pass that info into the model
> but that seems messy and redundant to me. If I subclass Catalyst::Model
> well then I'm tightly coupled with Catalyst again. I'd like to just be
> able to glue the model to Catalyst and do a:
> 
> $c->model('MyCustomModel')->foo(...)
> 
> and it just work.
> 
> It seems to me there should be a simple way for this schema to know its
> connection info based on context and I figured there's like a slick
> Catalyst ninja kind of method for doing this that my brain is just not
> grasping at the moment. How do you do this sort of thing? Is the magic
> in the Catalyst glue?
> 
> Thanks in advance for any advice/thoughts/comments...
> 
> -Eric
> 

I'm not sure if there's a special Catalyst way of doing this, but I use my schema classes from my various places, so I moved my db connection data into a separate config file that all apps can access.  I have a separate class I wrote that looks for the config file in a few specific places (depending on whether you're in dev mode or production mode) and reads it.  This class provides the connection info to the schema class.  This way I can easily move my database, update the info in one file, and all apps automatically get the new connection info.

Hope that helps.

Byron



More information about the Catalyst mailing list