Reputation: 709
Lets suppose that I have two classes Foo
and Bar
with each respective DAO
.
Lets say that a foo
has many bars
.
So the method that returns the bars
of a foo
where should it go?
class FooDAO
def bars_from_foo(foo)
# return the bars of that foo
end
end
class BarDAO
def bars_from_foo(foo)
# returns the bars of that foo
end
end
My doubt is, does FooDAO
only return foo
objects? Or does FooDAO
only include the methods that a foo
will need?
In my opinion, I choose the first one: FooDAO
only returns foo
objects. Because, the method could be used from another class, that has nothing in common with the other DAO
. I can't think in an example right now.
I would like to read some opinions from experts.
Upvotes: 0
Views: 36
Reputation: 183514
There are two approaches:
foo
has many bars
can actually be modeled in your Foo
class: foo.get_bars()
would return the bars
. FooDAO
would then be responsible for making sure that this works properly, either by fetching the bars
at instantiation time or by using lazy-loading.bar
has a corresponding foo
can be viewed as a search criterion for bar
. BarDAO
would then have a find_bars_by_foo_id(foo_id)
or find_bars_by_foo(foo)
. (This is pretty similar to your second possibility; the key is to think about it the right way.) Alternatively, it would have a find_bars(criteria)
, where criteria
can have settings such as the foo_id
to use.Which approach is most appropriate will depend on the semantics of the two classes and of the relationship between them.
Upvotes: 1