MVVM pattern – Best design approach to manage an application

One year ago, I discovered the WPF technology and I developed a little application, as first experiment, to compare the content of two different directories. The content of each directory is shown in a different DataGrid.
At that time, I didn’t develop the application using MVVM pattern, because it was only an experiment, not so good, but it worked.

Now that I’ve much more experience with WPF and with the design patterns, I’m not so proud of that work, and I want to rewrite the application, improving it and following the recommended MVVM pattern.

Actually, I designed the application in this way:

  • the MainModel, with the main common algorithm.
  • the MainView is the unique view of the application with the datagrids where I can show the results of my algorithms.
  • a MainViewModel, that it will take care of handling the connection between other two ViewModels, the MainModel and the MainView.
  • One ViewModel for each DataGrid. In this way, I’ll separate the behavior and the data of the two DataGrids.

The MainModel contains the main algorithm used to search the files in a given directory, like this:

public class MainModel
{
    public MainModel()
    {}

    public List<Results> GetFiles(string directoryPath)
}

The MainViewModel will instantiate an object for each ViewModel (I have a ViewModel for each datagrid), using some properties to do it:

public class MainViewModel
{
    public MainViewModel()
    {
        FirstViewModel = new ViewModelDG1();
        SecondViewModel = new ViewModelDG2();
    }

    public ViewModelDG1 FirstViewModel
    {get;set;}

    public ViewModelDG2 SecondViewModel
    {get;set;}
}

I wanted to apply a design pattern to help me manage the application, like a Factory Method or Builder, or a Template Method, but I think they’re not suitable for my application for the different usages/conditions of those design patterns.

I’m not so sure of this design, I don’t know if this a is correct implementation of the MVVM pattern and if it promotes a loose coupling and high cohesion. Can you help me in the design? Is mine a correct implementation of the MVVM? If not, then what is the correct one? What are your opinions?

Thank you.

Alberto, about one and a half years ago I was where you are now, and I’ve learned a lot about MVVM, WPF and Programming Meta Skills on the way.

For the time being, I will refrain from answering your questions specifically, and like to get a few words out that are of general interest/guidance, if you will.

MVVM is a design/architectural pattern that takes some time getting used to. There is no one right or correct way, MVVM is practiced in many different flavors with strictly no code-behind on the one side, and a more “integrated view model”-like approach (with a certain amount of code-behind allowed) on the other side. As with many things in IT, this topic also borders on personal liking.

Don’t make the mistake I made by trying to push in patterns (or someone else’s design liking) for patterns’ sake. Recognize your specific problems, and try to solve them one at a time. See how other WPF/MVVM experts are solving and buildings things. By recognizing a problem and finding a way to solve it, you learn your MVVM/WPF skill by heart. Many small steps will get you in the right direction faster than any doggedly “I’m trying to get this 100% correct the first time” approach.

Now to your question:

I cannot see any problem with your approach so far. The only things I’d like to comment on are:

  1. Instantiate your VMs via lazy loading, so that they consume memory/processing time only on-demand. Also, if you’re allowing re-setting your view model objects (see your setters), please make sure that the container class implements INotifyPropertyChanged.

    public ViewModelDG2 SecondViewModel {
    get { return _viewModelDG2 ?? (_viewModelDG2 = new ViewModelDG2()); }
    }

  2. As for your “loose coupling” concern: That one is more about providing interfaces to your specific implementation (classes). If the view is created in a different assembly, it might make sense to operate via internal classes and public interfaces.

Resources I can warmly recommend to you:

  • MVVM Survival Guide for Enterprise Architectures in Silverlight and WPF
  • Developer’s Guide to Microsoft Prism 4 Building Building Modular MVVM Applications
  • Pro WPF and Silverlight MVVM

Best!

1

Hello I think it’s to late to give this question answer but I think it may be useful to others who search same content.

as per my view the best Design Approach should be .

Your Application have below mentioned folder

1.Resources

2.Models

3.ViewModel

4.View

5.ServiceAgent //Which is used for reduce the dependance injection

6.Libs

Ok, All Views of your Application must have end with View Name. e.c MainPage is your xaml
then it name should be MainPageView.xaml

All same as All Viewmodels and models.

Your Viewmodel folder contain one ViewmodelLocator which is used to initialized all Viewmodel Which are used in your application.

you have to create object of Vuewmodel locator in App.xaml file and use it all view as a staticresource

e.c

xmlns:vm:"Your ViewModel namespace"



<resource><VM:YourViewModelLocatorName X:key="Key Name" /></resource> 

Your View Datacontext Should be

Datacontext=”{Binding ViewModelName,Source=”{StaticResource KeyName}”

Thanks

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

MVVM pattern – Best design approach to manage an application

One year ago, I discovered the WPF technology and I developed a little application, as first experiment, to compare the content of two different directories. The content of each directory is shown in a different DataGrid.
At that time, I didn’t develop the application using MVVM pattern, because it was only an experiment, not so good, but it worked.

Now that I’ve much more experience with WPF and with the design patterns, I’m not so proud of that work, and I want to rewrite the application, improving it and following the recommended MVVM pattern.

Actually, I designed the application in this way:

  • the MainModel, with the main common algorithm.
  • the MainView is the unique view of the application with the datagrids where I can show the results of my algorithms.
  • a MainViewModel, that it will take care of handling the connection between other two ViewModels, the MainModel and the MainView.
  • One ViewModel for each DataGrid. In this way, I’ll separate the behavior and the data of the two DataGrids.

The MainModel contains the main algorithm used to search the files in a given directory, like this:

public class MainModel
{
    public MainModel()
    {}

    public List<Results> GetFiles(string directoryPath)
}

The MainViewModel will instantiate an object for each ViewModel (I have a ViewModel for each datagrid), using some properties to do it:

public class MainViewModel
{
    public MainViewModel()
    {
        FirstViewModel = new ViewModelDG1();
        SecondViewModel = new ViewModelDG2();
    }

    public ViewModelDG1 FirstViewModel
    {get;set;}

    public ViewModelDG2 SecondViewModel
    {get;set;}
}

I wanted to apply a design pattern to help me manage the application, like a Factory Method or Builder, or a Template Method, but I think they’re not suitable for my application for the different usages/conditions of those design patterns.

I’m not so sure of this design, I don’t know if this a is correct implementation of the MVVM pattern and if it promotes a loose coupling and high cohesion. Can you help me in the design? Is mine a correct implementation of the MVVM? If not, then what is the correct one? What are your opinions?

Thank you.

Alberto, about one and a half years ago I was where you are now, and I’ve learned a lot about MVVM, WPF and Programming Meta Skills on the way.

For the time being, I will refrain from answering your questions specifically, and like to get a few words out that are of general interest/guidance, if you will.

MVVM is a design/architectural pattern that takes some time getting used to. There is no one right or correct way, MVVM is practiced in many different flavors with strictly no code-behind on the one side, and a more “integrated view model”-like approach (with a certain amount of code-behind allowed) on the other side. As with many things in IT, this topic also borders on personal liking.

Don’t make the mistake I made by trying to push in patterns (or someone else’s design liking) for patterns’ sake. Recognize your specific problems, and try to solve them one at a time. See how other WPF/MVVM experts are solving and buildings things. By recognizing a problem and finding a way to solve it, you learn your MVVM/WPF skill by heart. Many small steps will get you in the right direction faster than any doggedly “I’m trying to get this 100% correct the first time” approach.

Now to your question:

I cannot see any problem with your approach so far. The only things I’d like to comment on are:

  1. Instantiate your VMs via lazy loading, so that they consume memory/processing time only on-demand. Also, if you’re allowing re-setting your view model objects (see your setters), please make sure that the container class implements INotifyPropertyChanged.

    public ViewModelDG2 SecondViewModel {
    get { return _viewModelDG2 ?? (_viewModelDG2 = new ViewModelDG2()); }
    }

  2. As for your “loose coupling” concern: That one is more about providing interfaces to your specific implementation (classes). If the view is created in a different assembly, it might make sense to operate via internal classes and public interfaces.

Resources I can warmly recommend to you:

  • MVVM Survival Guide for Enterprise Architectures in Silverlight and WPF
  • Developer’s Guide to Microsoft Prism 4 Building Building Modular MVVM Applications
  • Pro WPF and Silverlight MVVM

Best!

1

Hello I think it’s to late to give this question answer but I think it may be useful to others who search same content.

as per my view the best Design Approach should be .

Your Application have below mentioned folder

1.Resources

2.Models

3.ViewModel

4.View

5.ServiceAgent //Which is used for reduce the dependance injection

6.Libs

Ok, All Views of your Application must have end with View Name. e.c MainPage is your xaml
then it name should be MainPageView.xaml

All same as All Viewmodels and models.

Your Viewmodel folder contain one ViewmodelLocator which is used to initialized all Viewmodel Which are used in your application.

you have to create object of Vuewmodel locator in App.xaml file and use it all view as a staticresource

e.c

xmlns:vm:"Your ViewModel namespace"



<resource><VM:YourViewModelLocatorName X:key="Key Name" /></resource> 

Your View Datacontext Should be

Datacontext=”{Binding ViewModelName,Source=”{StaticResource KeyName}”

Thanks

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật