0

I don't think I have seen an example of this, but I also haven't read anywhere that explicitly states that it shouldn't be done. For example, let's say I have some user model with the usual stuff like first name last name etc:

public class UserModel
{
    private int userID;
    public int UserID
    {
        get { return userID; }
    }

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public string MiddleInitial { get; set; }

    ...

}

If I were to strictly follow the MVVM pattern, would it be allowed to have, for example, a list of some other model

public class UserModel
{
    ...
    public List<SomeOtherModel> SomeList { get; set; }

}

or should models only have simple types?

  • Yes it's fine, it would be a hassle otherwise – TheGeneral Mar 26 '18 at 04:41
  • It is perfectly fine, and quite common, to have a viewmodel contain collections. – Cory Nelson Mar 26 '18 at 04:44
  • Yes. Probably what you are confused with is whether a VM should contain another VM? Composite VMs makes no sense when exposing a child VM that the immediate V is not concerned with. Plus it's a bit like putting excessive business logic in a VM –  Mar 28 '18 at 00:05

1 Answers1

4

Quite simply, yes.

When doing MVVM you need to change your perspective a little. The definition of 'model' isn't the same as MVC, instead it encompasses everything that isn't the view and viewmodel. This means the 'model' is a data entity (i.e. POCO), it includes any services or controllers, it includes business logic, etc:

Model

The model in MVVM is an implementation of the application's domain model that includes a data model along with business and validation logic. Examples of model objects include repositories, business objects, data transfer objects (DTOs), Plain Old CLR Objects (POCOs), and generated entity and proxy objects.

Quoted from the MSDN article The MVVM Pattern

So now that you've changed your perspective, if your question becomes Should POCOs (DTOs) contain other POCOs?, then the answer is still yes.

Community
  • 1
  • 1
slugster
  • 49,403
  • 14
  • 95
  • 145
  • Contrary to MS's definition of a _best practice_, it is a best practice that a POCO is just that - _a class devoid of application layer; that contains **no business logic**_. BL shouldn't go into VMs either. Regardless of MVVM religion, there is always SOLID principles to go by –  Mar 28 '18 at 00:07
  • @MickyD Agreed, once a POCO has business logic in it then it becomes a business object. – slugster Mar 28 '18 at 02:38