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 753571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:54:47+00:00 2026-05-14T14:54:47+00:00

What do you think about putting Get-logic in the getters of a ViewModel? Something

  • 0

What do you think about putting Get-logic in the getters of a ViewModel? Something like:

public class DummyViewModel
{
    public int Id { get; set; }

    private DummyObject myObject;

    public DummyObject MyObject
    {
        get
        {
            if (MyObject == null)
            {
                DummyRepository repo = new DummyRepository();
                myObject = repo.Get(Id);
            }
            return myObject;
        }
    }

}

Is this bad practice, or totally fine? I find my controllers getting really bloated by doing all the get-logic there, but I’m really torn as to where I should put it…

My reason for doing it this way, is that I can pass the ViewModel to different types of view, and only the neccessary DB-lookup will be performed based on what property is requested.

  • 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-14T14:54:47+00:00Added an answer on May 14, 2026 at 2:54 pm

    There is nothing wrong with putting logic in getters in VMs – the role of a VM is to present data to a view, and it should be as ready to “view” as possible (the View should not be having to do too much (if any) work to shape the data before showing it).

    As an example, I use properties called GetAvailableClients in my VM, and that will be one of the properties that the View binds to. The job of that particular getter is to filter data – IOW present a reduced set of data selected from a complete list (which is also held in the VM), that data will usually be filtered using LINQ, which means I have placed some custom logic in the getter.

    What I am not a fan of though is the rest of your approach, where if the property hasn’t been filled it goes off to the repository and gets the data itself. To me that is a no-no, the property is totally violating the principle of single responsibility by making the property responsible for too much. Not to mention that is not a good practice to follow once you start binding that property to the UI – suddenly your app will start hanging when the user performs an action because your property getter has been triggered and it has decided to make a call to the database or a webservice, and to make it worse that call has been done on the UI thread…. it just gets ugly.

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

Sidebar

Related Questions

How do you think about data access code like this: public void AddCusotmer(Cusotmer customer)
I'd like to know what you think about this part of our program is
I'm trying to get my team to think about only asking the database to
Without thinking about it too much, I've been doing something like the following: 1)
Something I don't get about the classical algorithm for the Producer-Consumer problem (from Wikipedia:)
Think about doing this: import matplotlib.pyplot as plt plt.plot(x_A,y_A,'g--') plt.plot(x_B,y_B,'r-o') plt.show() How would you
Think about the following: Your ISP offers you a dynamic ip-address (for example 123.123.123.123).
I cannot think about a proper question title to describe the problem. Hopefully the
What do you think about the concept of using ORO instead of traditional Garbage
I've been thinking about nested try/catch statements and started to think about under which

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.