Tuesday, October 24, 2006

One indirection layer too much?

It is a common practice to have a model completely outside of Catalyst classes (let's call it MyApp::RealModel). This way you can conveniently use the code in batch jobs and command line tools outside of the web environment. But still you need to create a nearly empty MyApp::Model::EmptyModel class just to connect the outside model into the catalyst framework. This might not seem as too much work - but it requires one more unique name and complicates the documentation. And since this EmptyModel does not introduce much new functionality over the MyApp::RealModel, the programmer easily forgets about all differences between them - and eventually falls into confusion.

This is one indirection layer too much in my opinion - what I would like instead is to be able to specify in the configuration file (myapp.yml) something like:

model: MyApp::RealModel

Is that change feasible? Don't ask me.


Anonymous said...

Reasons for the additional level of indirection:
* you can use existing model classes (integrating with other apps, porting old code to catalyst)
* you are able to use multiple models in a single application. In the real world, you often have to connect to multiple databases, and catalyst lets you do that easily.
* catalyst is doing additional parsing/preparation on your model object that DBIx::Class/etc don't provide. Having the extra layer of indirection allows more magic to go on in the background, leaving you to write more useful code

zby said...

Hmm, I cannot agree with most of those points.

* using existing model classes - you just specify what existing model you want to use (model: MyApp::RealModel in the config) - you don't need any indirection here

* Multiple models - this only requires some more complicated config, it's about specifying the models not about adding code to them. It could be something like:
- name: FirstModel
class: MyApp::RealModel
- name: SecondModel
class: MyApp::SpecialModel
I agree it gets more complicated - but the main point is that you don't need any code here - it is just a configuration data structure.

* Additional parsing/preparation - sure - but is it worth it? In my opinion the different ways of getting the model only introduces confusion for the beginners and spares relatively little typing. And I've seen recommendation to specify the full model name anyway - because it can get misleading when you have multiple models. Are there any other preparations? I don't know - but I guess that if there are then you can just add them as an extension of DBIC.

At least this "direct model" should be an option for those that do think it would simplify the matters for them.