Matt S Trout wrote:
Put this code in MyApp::Schema::Demo.
I often call it e.g. MyApp::DataStore or MyApp::Domain to remind me that
it's the business layer and the DBIC-ness is merely incidental.
sub do_some_business_logic_stuff {
my ($self) = @_;
if (<some complicated business logic>) {
$self->state('pending');
$self->update;
}
}
#### somewhere in my application
$demo = $c->model('DBIC::Demo')->find($index);
$demo->do_some_business_logic_stuff;
-------------
Is this a standard/typical/best-practice way to do this sort of thing?
It seems to me that if I want to use the business logic in an external
application (cronjob) then I am having to use the MyApp::Model::DBIC::Demo
namespace as decreed by Catalyst (not that this is a big issue).
Having moved the logic out of MyApp::Model:: this ceases to be an issue.
Don't confuse class -names- with the nature of classes. MyApp::Model:: is
*adapters* that make a model available to MyApp, not where your domain model
logic itself should live.
I usually these days have MyApp::Web for the catalyst app instead of MyApp so
I can deploy things like MyApp::DataStore from a separate dir tree.
Yes I did briefly have that confusion, but I think I am mostly straight
with it now and I (largely) have the layout you suggest. Just to confirm
I currently have the following
MyCompany::MyApp::Controller
MyCompany::MyApp::View
MyCompany::MyApp::Model
MyCompany::MyApp::Schema (where my ORM goes)
MyCompany::MyApp::Logic (where my other business logic goes)
I originally created the 'Logic' namespace because I was using
load_classes and by default my Schema directory had all my ORM
result_source files.
However I am just in the process of changing to using load_namespaces so
I now have
MyCompany::MyApp::Schema::Result (where I have moved all my
result_sources to)
MyCompany::MyApp::Schema::ResultSet
leaving MyCompany::MyApp::Schema empty of classes.
So I presume I can consider the MyCompany::MyApp::Schema namespace to be
for my business logic that does not directly map onto a ORM? (I can't
think of a good example off-hand.)
One final point that I am not sure I have right.
In my business logic I have certain constraints (for example the maximum
length of a web-form field, e.g. username) and most of the time this
constraint is defined by the database (the field length of the
user.username). Now the template needs access to these values to display
the appropriate error message so typically I do the following in my
controller.
$c->stash->{constraints}{user} =
MyCompany::MyApp::Schema::Result::User->constraints;
with the following in my User ORM
my $constraints = {
usernamename => {
min_length => 0,
max_length => 15,
},
# etc.
};
sub constraints {
my ($self) = @_;
return $constraints;
}
Which leaves me feeling a little ill-at-ease since it seems wrong to me
to have a class method to do this but I don't understand why!
Regards
Ian
_______________________________________________
List: [email protected]
Listinfo: http://lists.rawmode.org/mailman/listinfo/catalyst
Searchable archive: http://www.mail-archive.com/[email protected]/
Dev site: http://dev.catalyst.perl.org/