[Catalyst] The model -is- where your business logic lives.

Ian Docherty catalyst at iandocherty.com
Fri Sep 28 08:55:09 GMT 2007


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) =3D @_;
>>
>>    if (<some complicated business logic>) {
>>        $self->state('pending');
>>        $self->update;
>>    }
>> }   =

>>
>>
>> #### somewhere in my application
>>
>> $demo =3D $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::De=
mo =

>> 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 mo=
del
> logic itself should live.
>
> I usually these days have MyApp::Web for the catalyst app instead of MyAp=
p 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} =3D =

MyCompany::MyApp::Schema::Result::User->constraints;

with the following in my User ORM

my $constraints =3D  {
    usernamename =3D> {
        min_length      =3D> 0,
        max_length      =3D> 15,
    },
# etc.
};

sub constraints {
    my ($self) =3D @_;
    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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20070928/6a242=
ae6/attachment-0001.htm


More information about the Catalyst mailing list