I want to develop my wpf project by using MVVM pattern. Should I create new folder to separate Model, View, ViewModel or create new project for each? Which one is good practice? I downloaded sample project, some of project have separate folder for it and some of have separate project for each. I referred this link. But this isn't clear my doubt. I am asking whether creating new projects for Model, view and viewmodel is good practice or creating new folder?
4 Answers
This is a duplicate question, as you can find the answer here by Sheridan to be most helpful.
Basically you need to determine what the overall size of your project is going to be. If you plan on reusing pieces, you might want to to multiple projects and different levels of folders. This also goes for if you plan on providing parts of your application as an API, then you will probably want to use multiple projects.
For beginners, I would suggest you start small and put everything in one project. To learn MVVM, don't focus on the project structure rather how WPF and MVVM work. Solution structure can always be revisited after you've understood how MVVM is working.
EDIT
The answer is no, you never need to create folders or separate projects for your project. Just realize that creating new class
es under a different folder will also use that path as a namespace. So if you create a ViewModels
folder and add a class
to that folder you will see namespace WpfApplication1.ViewModels
at the top. This also means that you will have to pay attention to locations of namespaces in your XAML files. If you wanted to reference a type in your ViewModels
folder in a XAML file, you will need to do something like <xmlns:vm="clr-namespace:WpfApplication1.ViewModels">
. The downside is if you have folders in ViewModels
you need to reference that namespace in your XAML also. So if there was ViewModels > Basics, you would need both namespaces in XAML since you can't reference all sub-dirs in one xmlns declaration.
<xmlns:vm="clr-namespace:WpfApplication1.ViewModels">
<xmlns:vmBasics="clr-namespace:WpfApplication1.ViewModels.Basics">
The alternative is using folders and renaming the class namespaces to just plain old namespace WpfApplication1
then you would just need a single xmlns declaration and can access all types in your application.
<xmlns:app="clr-namespace:WpfApplication1">
So, don't complicate things if they aren't needed; and typically nothing in the learning stages will be so complex it needs multiple projects. Folders are optional, but understand the side effects/workaround if you use them.
-
Yeah that link is helpful but my question is bit different. I am asking whether I need to create different project for model, view and viewmodel or create new folder for each. – Mohini Mhetre May 28 '14 at 06:08
-
Sub folder doesn't necessarily require to change namespace. You can always suppress this convention and have folders to organizing purpose only and have your classes still compiled under one namespace. Yes it's more work. – voddy May 28 '14 at 06:28
-
Yes but how do you suppress this convention? Do you mean by adding a new class at the root folder and dragging it into the sub directory? That's changing the namespace. I'm not sure of another way to do that. – Kcvin May 28 '14 at 06:30
-
You can specifically set the namespace of every class, so when you create it just give it whatever namespace you desire. – Mashton May 28 '14 at 09:47
-
That's what I mentioned in my answer. `renaming the class namespaces` – Kcvin May 28 '14 at 09:49
This will really depend on your use case. Do you have a scenario in which your views, view models and models may be separately reused in some other project? What i mean by that, for instance, do you have a scenario where you would use only your models in some other project. If yes, then it makes sense to create separate projects so they may be reused. If that is not the case however, it is better to keep them all in one project.

- 179
- 11
Folders are only for creating the Structure and for easy Identification. It is really not necessary to have Folders for View and ViewModel.

- 216,225
- 63
- 350
- 396
I would normally create folders each for view, ViewModel etc. Then I would give descriptive names to views and viewModels classes. If you have several pages I would suggest having sub folders. All goes in one project. But this is my preference.
eg:
View>Sales> WelcometoSalesPageView.xaml
ViewModel>Sales> WelcometoSalesPageViewModel.vb

- 950
- 1
- 11
- 21