Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7601779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T23:14:48+00:00 2026-05-30T23:14:48+00:00

I’m posting this question as language and framework agnostic since I think it may

  • 0

I’m posting this question as language and framework agnostic since I think it may be general to ORM Frameworks.

I’m wondering if anyone can tell me if there is a design pattern for the following use case:

The Use Case

The system is a Model-View-Controller web application running with an ORM Framework.

There are two entities in play Poll and Vote.

One Poll has a one-to-many relationship with Vote and votes are in a collection which is Lazy Loaded.

Each user in the system can only vote on a poll once.

For the sake of user experience it would be nice for the View to adapt by showing controls only for polls on which the current user has not yet voted therefore Poll has a method (in pseudocode):

public boolean hasVoteFor(user) {
    for each vote
        if vote.user == user
            return true

    return false
}

So that when the View is passed a Poll by the Controller it can render the correct controls by doing an if poll.hasVoteFor(user).

The Problem

The problem with the above method is that, since the collection is Lazy Loaded, iterating over it could become very memory & db intensive.

Using SQL/DQL/SQL you can obviously use the database to directly query the Poll entities on which a user has voted, however any such query should occur in a repository which I’m assuming that the View should not have access to.

Solutions?

So I’m wondering if there is a design pattern to solve this.

  • Should the Controller have a method such as hasVoteFor(poll, user)? This is problematic if the view renders multiple polls. (A query with many criteria for each poll would be faster rather than doing many selects as the View iterates Polls that it is given.)

  • Should the Controller use a repository to query all the Polls that a user has voted on up front and pass it to the View along with the Poll (or array of Poll). This kind of breaks the neat abstraction of a Poll having a hasVoteFor method – isn’t ORM supposed to let you only think about your objects? 😉

  • Some other solution commonly used in practice that I’m overlooking?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T23:14:49+00:00Added an answer on May 30, 2026 at 11:14 pm

    An ORM is just a tool to help you with your database. But the database is still the best tool you have to efficiently query for the data you want to display. And using an ORM without understanding and caring about the database and the queries that are generated is a recipe for disaster.

    Your second solution is better than the first one, but it’s still not as logical and efficient as it could be.

    Instead of querying for the poll or polls to display, and then querying for the polls that a user has voted on, and passing these two collections to the view, the controller could execute a query which returns a list of PollWithVoteFlag instances, containing each poll data, along with a flag indicating if the poll has been voted on by the user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have this code to decode numeric html entities to the UTF8 equivalent character.
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.