Entering logic into ViewModel get'ers

What do you think of putting get-logic in getters ViewModel? Sort of:

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 completely perfect? I find that my controllers are really bloated by doing all the get-logic, but I'm really torn as to where I should put this ...

My reason for this is that I can pass the ViewModel to different types of views, and only the required DB search will be performed depending on what property is being requested.

+3
source share
3 answers

, - , "" ( ( ) , , ).

, GetAvailableClients , , . - IOW , ( ), LINQ, , .

, , - , , , . -, , . , , , - , , , -, ... .

+9

, .

:

1) . , ?

2) . - , viewmodel . , viewmodel ?

3) . Id ( - ) setter. ? , , , . , - , SoC.

+2

- - DTO. , , VM .

+2
source

Source: https://habr.com/ru/post/1742792/


All Articles