Putting logic in ViewModel get'ers

Posted by Yngvebn on Stack Overflow See other posts from Stack Overflow or by Yngvebn
Published on 2010-04-26T09:43:18Z Indexed on 2010/04/26 10:13 UTC
Read the original article Hit count: 155

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about c#