Hi Jan, Having to pass "db => shift->model->db” in every model helper doesn’t seem like invisible way ;) It would be really invisible if you had “use DB” inside a model package and then “my $db = $DB::dbh” :) but then you’d have to somehow pass app->config to DB. Also if you kept something in app->config that model needs - would you pass it as another parameter to every model you had? What if then you had app->cache (redis/memcached) ? Another parameter to pass to every model again? I feel it doesn’t seem good but can’t find any other way in Mojo to do it so thought someone had found the solution...
Cheers Eugene > On Apr 20, 2015, at 11:18 AM, Jan Henning Thorsen <jan.henn...@thorsen.pm> > wrote: > > I pass on $db to every instance of a model in MCT: > https://github.com/mojoconf/MCT/blob/master/lib/MCT.pm#L33 > > Just make a helper where the passing of $db (or any other common argument) is > "invisible". > > The nice thing is that the model will need to know less. It will then be > easier to use for "other things" outside of your Mojo app. To flip the > question around. Which version ofof the code below makes most sense? > > my $app = Mojolicious->new; > $db = Mojo::Pg->new(app => $app); > $db = Mojo::Pg->new(app => $app->config->{db}{dsn}); > > (I hope the version where $app is passed to new() looks weird...) > > > On Monday, April 20, 2015 at 9:17:27 AM UTC+2, Eugene Toropov wrote: > Hi Jan, > > Thanks for your reply. Don’t you think that passing app->db to every model is > not a good idea neither because model must know itself where data are and how > to fetch it and pass to controller? Also it’s simply inconvenient to always > have one (or even 2 - app->db and app->redis for example) arguments passed to > every model, no? > > Cheers > Eugene > > On 20 Apr 2015, at 10:07, Jan Henning Thorsen <jan.henn...@thorsen.pm > <mailto:jan.henn...@thorsen.pm>> wrote: > >> You don't. Passing $app or $c to a model is not a good idea. Reason for this >> is that it makes it hard to reuse the models elsewhere. What you can do, is >> passing data from $app when you construct your models, but I would strongly >> advice against passing $app. >> >> >> On Friday, April 17, 2015 at 4:38:06 PM UTC+2, Eugene Toropov wrote: >> Greetings, >> >> In the following example how will you make MyApp::Model::Users have access >> to app object (which is basically $self) that is necessary to use app->db >> and app->config? >> >> http://mojolicio.us/perldoc/Mojolicious/Guides/Growing#WELL-STRUCTURED-APPLICATION >> >> <http://mojolicio.us/perldoc/Mojolicious/Guides/Growing#WELL-STRUCTURED-APPLICATION> >> >> package MyApp; >> use Mojo::Base 'Mojolicious'; >> >> use MyApp::Model::Users; >> >> sub startup { >> my $self = shift; >> >> $self->secrets(['Mojolicious rocks']); >> $self->helper(users => sub { state $users = MyApp::Model::Users->new }); >> >> my $r = $self->routes; >> >> $r->any('/' => sub { >> my $c = shift; >> >> my $user = $c->param('user') || ''; >> my $pass = $c->param('pass') || ''; >> return $c->render unless $c->users->check($user, $pass); >> >> $c->session(user => $user); >> $c->flash(message => 'Thanks for logging in.'); >> $c->redirect_to('protected'); >> } => 'index'); >> >> my $logged_in = $r->under(sub { >> my $c = shift; >> return 1 if $c->session('user'); >> $c->redirect_to('index'); >> return undef; >> }); >> $logged_in->get('/protected'); >> >> $r->get('/logout' => sub { >> my $c = shift; >> $c->session(expires => 1); >> $c->redirect_to('index'); >> }); >> } >> >> 1; >> Cheers >> Eugene >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Mojolicious" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to mojolicious+unsubscr...@googlegroups.com >> <mailto:mojolicious+unsubscr...@googlegroups.com>. >> To post to this group, send email to mojolicious@googlegroups.com >> <mailto:mojolicious@googlegroups.com>. >> Visit this group at http://groups.google.com/group/mojolicious >> <http://groups.google.com/group/mojolicious>. >> For more options, visit https://groups.google.com/d/optout >> <https://groups.google.com/d/optout>. > > > -- > You received this message because you are subscribed to the Google Groups > "Mojolicious" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to mojolicious+unsubscr...@googlegroups.com > <mailto:mojolicious+unsubscr...@googlegroups.com>. > To post to this group, send email to mojolicious@googlegroups.com > <mailto:mojolicious@googlegroups.com>. > Visit this group at http://groups.google.com/group/mojolicious > <http://groups.google.com/group/mojolicious>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. -- You received this message because you are subscribed to the Google Groups "Mojolicious" group. To unsubscribe from this group and stop receiving emails from it, send an email to mojolicious+unsubscr...@googlegroups.com. To post to this group, send email to mojolicious@googlegroups.com. Visit this group at http://groups.google.com/group/mojolicious. For more options, visit https://groups.google.com/d/optout.