Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you organize your Models/Views/ViewModels in WPF

Tags:

c#

mvvm

wpf

This has been a constant irritation of mine, so I thought I would ask for suggestions. How do you organize your Models/Views/ViewModels in WPF (Solution Explorer)? I can never seem to find a solution that I'm happy with so I'm wondering if there is someone out there that has.

like image 598
sircodesalot Avatar asked Jun 05 '13 18:06

sircodesalot


People also ask

What is model view ViewModel in WPF?

MVVM (Model-View-ViewModel) MVVM is a way of creating client applications that leverages core features of the WPF platform, allows for simple unit testing of application functionality, and helps developers and designers work together with less technical difficulties.

Can a ViewModel have multiple views?

It's possible that having two separate views is what you want, but it's just something to consider. Show activity on this post. I am using the prism framework and was also looking for a solution of using one viewmodel for many (child) views.

How do I link views and ViewModel?

The other approach to bind the View and Viewmodel in View First Approach is in the xaml itself as shown in the figure below. I am setting the datacontext in the . xaml of the View itself. To set the datacontext in this way the ViewModel class need to have a default constructor.


6 Answers

Aside from the fact that your Models should be in their own assembly (project). I tend to put Related Views and ViewModels together in a single Folder, rather than having a folder called "Views" and Another one called "ViewModels"

Say, for example:

Project MyApp.Model
    |---> Models


Project MyApp.Client
    |--> Orders
    |      |--> OrderCRUDView
    |      |--> OrderCRUDViewModel
    |      |--> OrderListView
    |      |--> OrderListViewModel
    |--> Accounts
           |--> AccountCRUDView
           |--> AccountCRUDViewModel
           |--> AccountListView
           |--> AccountListViewModel
    ...etc
like image 68
Federico Berasategui Avatar answered Oct 04 '22 10:10

Federico Berasategui


How do you organize your Models/Views/ViewModels in WPF (Solution Explorer)?

I typically have the Model in a separate project. One of the main goals in MVVM is to keep the model isolated from the View and ViewModel entirely.

The View and ViewModel depends - My personal organization style differs based on project scope.

For very small projects, I often have the View and ViewModel for each "view" side by side.

For larger projects, I will separate these into their own namespaces (and folders), or even into separate projects. Having the ViewModel in a separate project from the View is nice in that it can enforce that your ViewModel does not refer to View elements, as you can leave the required references out of that project entirely.

like image 22
Reed Copsey Avatar answered Oct 04 '22 10:10

Reed Copsey


In a larger application you might wanna throw stuff into separate assemblies but I think this would work just as well.

Project MyApp
    |--> Views
    |      |--> AccountsView
    |      |--> OrderView
    |--> Sources
           |--> CustomerData
                  |--> Data
                  |     |--> DataAccess.cs    <-- Provides database search helper methods that return ObservableCollections of "Model" data types for use in the ViewModels.
                  |--> Models
                  |     |--> Account.cs
                  |     |--> Order.cs
                  |--> ViewModels    <-- Having more then one viewmodel for the same data is possible, e.g. Master-Detail scenarios.
                        |--> AccountsViewModel.cs
                        |--> OrderViewModel.cs
like image 28
FocusedWolf Avatar answered Oct 04 '22 10:10

FocusedWolf


I separated them into different projects then broke it down from there. Basically M project, VM project, and then the View as the main project. Though eventually V & VM became more tightly coupled.

like image 26
Zach Leighton Avatar answered Oct 02 '22 10:10

Zach Leighton


I'm a 'Solution Folder' person...

I keep a given V and VM together in the same assembly and put all V/VM assemblies in a 'Solution Folder' created by Visual Studio.

Models and Utility classes are isolated by assembly and also pushed into a 'Solution Folder'.

And of course, there's a Solution Folder called 'Infrastructure' that contains the magic strings and so on...

Solution Folders are a logical designation. They do not create physical folders on your drive.

like image 30
Gayot Fow Avatar answered Oct 04 '22 10:10

Gayot Fow


This has been my starting basic setup for non trivial projects for almost a decade and it's simplicity has served me well. The terrible practice of keeping views and view models in the same project seems to be is a prism thing but the MVVM community shot that anti-pattern down a decade ago.

Client Tier:

ProjectName.Client.csproj
    --Assets
    --Images
    --Brushes
    --DataTemplates
    --Styles
    --Controls
    --Helpers
    --Views
ProjectName.Client.ViewModel.csproj
    --ModelViews
    --ViewModels
    --Helpers

Server Tier:

ProjectName.Server.Services.csproj
ProjectName.Data.csproj
ProjectName.Model.csproj

The view model layer doesn't reference a "Model" project as that exists in the server tier and is exposed to the view model via a proxy of the data service reference.

like image 27
Este Vato Pancho Villa Cruz Avatar answered Oct 02 '22 10:10

Este Vato Pancho Villa Cruz