Hacker News new | ask | show | jobs
by Jestar342 1952 days ago
It can be extended upon.. I see a lot of ORM based solutions that pull a full record just to check a single property sometimes. E.g.:

    var user = repository.GetUserById(userId);
    return user.IsDisabled;
That's not even a facetious example, I have seen it multiple times. In some cases that query is pulling multiple columns, and a few joins.. just to pull a single bit value.
3 comments

This is not necessarally terrible in some scenarios: namely, when the repository caches enties for the duration of the unit of work, and the user in question is used elsewhere in the request (or at least is used elsewhere in the common case).

In that scenario, this can be strictly more efficient than doing a specialized query here, and a more general fetch of the user later, because it becomes just one sql command instead of two.

Obviously though there are ORMs that don't offer such caching, or cases where the value will not be used again elsewhere in the request, and in those cases this is clearly undesirable. It is generally quicker and easier to do this than adding a new custom method to the repository to get exactly the desired data which is why it remains common even in those scenarios.

I see that, too. But I don't think it's fair to blame on ORM. I also see it happening in non-ORM code that uses the repository pattern, for example.

For example, there's nothing about the code example you give that strictly implies the use of an ORM, just the use of some sort of layered design.

I didn't meant to blame ORM, only highlight the (mis)use of them in this type of scenario - but yes, you are correct. It is an abuse of the repository - ORM or other. :)
Laravel Eloquent permits sparse loading of a model; but the way ORM is used in general, this might be a bug factory. Nevertheless I do use the technique where the objects exist only within the scope of a given subroutine.
On the other hand, if the user object was already recently used somewhere else it will be cached and this code will produce no database/network traffic at all.