On Oct 11, 2010, at 5:22 PM, Luca Lesinigo wrote:

> I'm using Elixir for the data model part of an MVC app I'm designing.
> I often come accross similar 'queries' I want to run against my data
> from various parts of the app, and I wonder what would be the 'ideal'
> place to put them? (ideal as in, best programming practice).
> 
> Let's take the usual movie db as an example and suppose I often want
> to get the set of Movies of a given Genre, made by a given Director,
> after some given year.
> 
> Should I add a simple function in my model, like
> getMoviesByDirectorGenreYear(director, genre, starting_year) ?
> It would run a query as usual in the form
> Movie.query...filtering...all()
> 
> Or maybe a @classmethod Movie.getByDirectorGenreYear(cls, director,
> genre, starting_year)?
> Would run a query like before but gets the class as a paramenter,
> could use that to be generic and adaptable to different classes /
> inherited classes.
> 
> Or an instance method like Director.getMoviesByGenreAndYear(self,
> genre, starting_year) ?
> And if I do that, it would be equally good to also write a
> Genre.getMoviesByDirectorAndYear(self, director, starting_year)
> method...
> 
> To me, the classmethod approach seems more logical, since if I ever
> subclass Movie I've already got my ready-made query. Also because it
> would actually return a list of Movies. What do you think?
> 
> Also, how would the same idea change if I'm not getting a set of
> instances for one of my model classes but building a whole class of
> data? (eg. resulting from some joins between my actual raw data) - the
> equivalent of some CREATE VIEW ... AS SELECT ... JOIN ... etc...

I don't think there is a general answer to this. I personally think that less 
class/instancemethods are better, but it's a question of taste and "balance".

If you for example ask a *lot* about the movies of a director for a given year 
,that might justify the case for a method.

But in general, IMHO queries should stay queries. Because then it's immediately 
clear what something does.

Diez

-- 
You received this message because you are subscribed to the Google Groups 
"SQLElixir" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlelixir?hl=en.

Reply via email to