locked
Difference between n-layer software architecture and MVVM design pattern? RRS feed

  • Question

  • hello everyone,

    I am not clear with this concepts of Software architecture pattern and design pattern.

    I am developing an wpf application using MVVM. Now, my question is, does MVVM come under n-layered architecture? 

    or n-layer architecture and MVVM both are different? 

    Regards,

    Rakesh.N


    Rakesh murthy

    Friday, April 24, 2015 10:49 AM

Answers

  • Why don't you close your previous threads by marking helpful posts as answer before you start a new one?

    MVVM is a user interface desing pattern and has nothing to do with N-layered architecture really.

    N-tier or N-layer architecture means that the overall architecture of the application consists of several different logical or physical layers that are clearly separated from each other, for example a UI/presentation layer, a service layer, a business/domain logic layer and a data access layer.

    The MVVM pattern lives in the UI/presentation layer only.  The view encapsulates the UI and any UI logic, the view model encapsulates presentation logic and state and the model that encapsulates business logic and data is represented by the N-tier layers above (service, business, data). A view model may for example reference a service that connects to a remote server where the business logic and the data access layer are implemented and hidden from the client application.

    So MVVM is a design pattern that lets you maintain a clean separation between application logic and UI in the UI/presentation layer independant of whether your overall application architecture is N-layered or not.


    Hope that helps.


    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by JayChase Friday, April 24, 2015 1:19 PM
    • Marked as answer by Barry Wang Wednesday, May 6, 2015 11:16 AM
    Friday, April 24, 2015 11:18 AM
  • >>my understanding: ...

    Correct.

    >>Now my question is, i want to process the data which i get from database. where should i write that processing logic after getting the data from database? in model classes? or in view model classes?

    Generally speaking in the model. The model handles the retrieval of the data from the database and process any custom logic on it. The view model simply exposes this data to the view. As a general rule of thumb the view model should not contain any business logic, but only application logic, e.g. what happens when you click on a button.

    >>in this context , in which layer the viewmodel and model classes are put under in n-tier architecture?

    The view model classes belongs to the UI/presentation layer and the model classes belong to any of the service, business or data access layer. You should be able to use the model classes from any client application such as for example an ASP.NET web application and a WPF application. These classes should not contain any framework specific code.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Xavier Xie-MSFT Saturday, April 25, 2015 7:03 AM
    • Marked as answer by Barry Wang Wednesday, May 6, 2015 11:16 AM
    Friday, April 24, 2015 12:01 PM

All replies

  • Why don't you close your previous threads by marking helpful posts as answer before you start a new one?

    MVVM is a user interface desing pattern and has nothing to do with N-layered architecture really.

    N-tier or N-layer architecture means that the overall architecture of the application consists of several different logical or physical layers that are clearly separated from each other, for example a UI/presentation layer, a service layer, a business/domain logic layer and a data access layer.

    The MVVM pattern lives in the UI/presentation layer only.  The view encapsulates the UI and any UI logic, the view model encapsulates presentation logic and state and the model that encapsulates business logic and data is represented by the N-tier layers above (service, business, data). A view model may for example reference a service that connects to a remote server where the business logic and the data access layer are implemented and hidden from the client application.

    So MVVM is a design pattern that lets you maintain a clean separation between application logic and UI in the UI/presentation layer independant of whether your overall application architecture is N-layered or not.


    Hope that helps.


    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by JayChase Friday, April 24, 2015 1:19 PM
    • Marked as answer by Barry Wang Wednesday, May 6, 2015 11:16 AM
    Friday, April 24, 2015 11:18 AM
  • MVVM involves 3 layers.

    View

    ViewModel

    Model

    Although it's a front end pattern rather than end to end pattern, there are still 3 layers there.

    At least 3.

    EG Often the database would be considered another layer.

    It would depend on your exact definition of what an n-layered architecture is.

    Personally I would say 3 is a number and n represents number.

    I'd go with yes.  

    You might think that those 3 layers are all in one thing, so that would disqualify them.

    It's not unusual for multiple layers to be logically separated though.

    .

    It is just a labelling thing though.

    You say tomato, I say tomayto.

    Same thing.

    Quite why one would care, I dunno.


    Hope that helps.

    Technet articles: Uneventful MVVM; All my Technet Articles


    • Edited by Andy ONeill Friday, April 24, 2015 11:41 AM
    Friday, April 24, 2015 11:40 AM
  • So in wpf what does the model classes have?

    my understanding:

    1. view : will have xaml files which is seen by user on screen.

    2. view-model: are the files which communicate with model classes and get required data from model classes to display it on view.

    3. model: In model classes, there will be code to get the data from the data base.

    Now my question is, i want to process the data which i get from database. where should i write that processing logic after getting the data from database? in model classes? or in view model classes?

    in this context , in which layer the viewmodel and model classes are put under in n-tier architecture?


    Rakesh murthy

    Friday, April 24, 2015 11:54 AM
  • >>my understanding: ...

    Correct.

    >>Now my question is, i want to process the data which i get from database. where should i write that processing logic after getting the data from database? in model classes? or in view model classes?

    Generally speaking in the model. The model handles the retrieval of the data from the database and process any custom logic on it. The view model simply exposes this data to the view. As a general rule of thumb the view model should not contain any business logic, but only application logic, e.g. what happens when you click on a button.

    >>in this context , in which layer the viewmodel and model classes are put under in n-tier architecture?

    The view model classes belongs to the UI/presentation layer and the model classes belong to any of the service, business or data access layer. You should be able to use the model classes from any client application such as for example an ASP.NET web application and a WPF application. These classes should not contain any framework specific code.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    • Proposed as answer by Xavier Xie-MSFT Saturday, April 25, 2015 7:03 AM
    • Marked as answer by Barry Wang Wednesday, May 6, 2015 11:16 AM
    Friday, April 24, 2015 12:01 PM
  • "

    Now my question is, i want to process the data which i get from database. where should i write that processing logic after getting the data from database? in model classes? or in view model classes?

    "

    The model is however the data comes from wherever it comes from.

    If there is any processing after you get it then that would be in the viewmodel.

    The viewmodel adapts your data to the view.

    If it needs any adapting then the viewmodel does that, not the model.

    EG here

            protected async override void GetData()
            {
                ThrobberVisible = Visibility.Visible;
                ObservableCollection<CustomerVM> _customers = new ObservableCollection<CustomerVM>();
                var customers = await (from c in db.Customers
                                        orderby c.CustomerName
                                        select c).ToListAsync();
                foreach (Customer cust in customers)
                {
                    _customers.Add(new CustomerVM { IsNew = false, TheEntity = cust });
                }
                Customers = _customers;
                RaisePropertyChanged("Customers");
                ThrobberVisible = Visibility.Collapsed;
            }

    That logic is in a viewmodel.


    "

    in this context , in which layer the viewmodel and model classes are put under in n-tier architecture?

    "

    They're in two of the n layers/tiers.

    Or they're in one.

    Depending on how someone decides to classify a layer or a tier.

    There is no absolute definition.

    Why would you care?


    Hope that helps.

    Technet articles: Uneventful MVVM; All my Technet Articles


    Friday, April 24, 2015 1:24 PM