Saturday, June 23, 2018

android - Is my MVC and MVVM software design pattern knowledge correct?



I want to check my knowledge as I have seen a project in my company in different way.



What I know is, if we want to use MVC software design pattern in our project, we separate our project like this:



View part is our design UI, which means ui.xaml file in WPF, ui.designer.cs in WinForms, ui.xml in Android, ui.qml in QtQuick and ui.ui in XCode.



Controller part is the nearest source file connected to designer file with similar names. Which is ui.xaml.cs file in WPF, ui.cs in WinForms, ui.java in Android, ui.h/ui.cpp in QtQuick and ui.swift in XCode.




Model part is any other class that does the main operations. I usually add the suffix Manager (or something like that) to them to say what is its responsibility.



and for MVVM I also use an interface or abstract class that the View is inherited from for its View Model part and consider my former Controller as a part of View.



And one Project is enough for building the whole thing.



But a project in my company has many classes! a folder of separate libraries for designers with the name of UserControl, a folder of another separate libraries with the name of View, and another set of libraries with the name of ViewModel and so is the case for Controller and Model. And they say it is a mixture of MVC and MVVM. The most surprising point is, the whole software has 38 separate projects written by a single programmer and the team is a small one.



Please tell me whether my knowledge is correct? And please tell me if the project in my company is in a right direction?



Answer



Is your knowledge about MVC and MVVM patterns correct? You had a mistake on Controller(ViewModel) and I think the same for Model.



Is your project in right direction? Doesnt seem right.



We use interfaces for all classes of Views and ViewModels (same for Controllers in MVC) just to have a better dependency management. its called IoC(inversion of Control) and the technique most of the times we use is called Dependency Injection, which makes a class, dependent to interface only (not concrete Impl.) using a Service Locator or some kind of service look-up object.



First of all, you can mix Controller with MVVM to handle the logic codes and let ViewModel handle binding and representation of Data Model for view, check out these series on Code project:
http://www.codeproject.com/Articles/173618/MVVM-sharp-Episode-1




But I'm not a big fan of MVCVM unless you will use all of its benefits.



Controllers or ViewModels are separate classes from Views, which we put our logic codes in them. so "ui.xaml.cs" is not a Controller for example. that file is just another file for ui.xaml class. they both are same class which we call View. Remember we use presentation patterns to separate logic codes from View's Code Behind codes.



Model is an object of properties grouped together. we just try to not to mix Model class with any logic codes. Models can have Validation functions (.IsNameValid() for example) or some other functions to create another representation of object (.ToString() for example) but not any logic codes (like CRUDs for example). We dont put our main operations in Model. main operations are in Controllers(ViewModels) but the result could be a Model which we bind to some of View fields.



We click "Search User" button, SearchUser command runs a function in Controller and in the result sets a User model object to a property called FoundUser (which is in Controller), and View's TextBox shows FoundUser's Name.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...