Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this correct way to implement MVVM?

I'm creating an application that enables a user to insert, update and delete data that has been entered and then shown in a data-grid (CRUD operations).

In my View Model, it contains properties which are bound to the xaml (Firstname for example). It also contains a navigation property as well as validation attributes.

      [Required(ErrorMessage = "First Name is a required field")]
      [RegularExpression(@"^[a-zA-Z''-'\s]{1,20}$", ErrorMessage = "First Name must       contain no more then 20 characters and contain no digits.")]
      public string FirstName
      {
        get { return _FirstName; }
        set
        {
            if (_FirstName == value)
                return;

            _FirstName = value;
            OnPropertyChanged("FirstName");
        }
    }

Furthermore, it contains commands for the xaml to execute, which creates an instance of the CRUD operation;

    private void UpdateFormExecute()
    {
        var org = new OrganisationTypeDetail();
        UpdateOrganisationTypeDetail(org);
    }

And lastly, it contains the CRUD operations as well. Such as the Insert, Update and Delete.

Which leads me to my question. if I want to implement the correct MVVM way, is all this code too much for the view model to contain?

Should I use the model and create a collection within my View-model and bound that to my xaml? Would this be the correct way of doing it?

Should I use a Repository system for the CRUD operations? If so, how would I pass the data from the text fields through to the model to get updated?

Im new to WPF, MVVM and finding it hard to adapt without proper guidance.

like image 392
greg Avatar asked Nov 23 '12 09:11

greg


People also ask

What design pattern does MVVM implement?

Model-View-ViewModel (MVVM) is a structural design pattern that separates objects into three distinct groups: Models hold application data. They're usually structs or simple classes. Views display visual elements and controls on the screen.

Is MVVM a design pattern or architecture?

Model–view–viewmodel (MVVM) is a software architectural pattern that facilitates the separation of the development of the graphical user interface (the view) – be it via a markup language or GUI code – from the development of the business logic or back-end logic (the model) so that the view is not dependent on any ...

Which is better MVVM or MVC?

MVVM is better than MVC/MVP because of its unidirectional data and dependency flow. Dependency is one way, thus it is a lot easier to decouple it when we need to. It is also easier for testing.


1 Answers

I would say that this is a correct way to implement MVVM, but not the correct way to implement MVVM.

What I mean by this is that there is no one correct way to implement this pattern. if you have created a ViewModel that can be bound to your View, without having any extra logic within your View (i.e. code-behind) then you have captured the essence of MVVM.

Whether or not you add more patterns and structure to your code is entirely up to you. If this is a simple application, I would keep the patterns light. Go ahead and have your ViewModel talk directly with a repository. You current code looks just fine to me in that respect.

If this is a large application, you might want to add further layers, like a service layer, data access layer. You might want to think about dependency injection.

But don't just adopt a pattern, or add an extra layer just because you think you should. Dependency Injection sounds cool, but in many cases it is more hassle than it is worth!

like image 165
ColinE Avatar answered Sep 18 '22 08:09

ColinE