none
MVC pattern with 3 layer pattern RRS feed

  • Question

  • Hi People,

    I'm new to MVC and just wanted to understand how it should fit into my application. I have been using the 3 layer design previously with asp.net webforms.

    eg: WebApp (UI) <-> Business <-> Data
                 |                                         |
                 |-------> Biz Entities <--------| 

    now looking at MVC I guess you would do somthing like this:

    WebApp:                       Application Core:
    View <-> Controller <-> Business     <->    Data
       |                    |               |                         | 
       | -> Model <-- |               |-> Biz Entities <-|

    Problem I have is the duplication of the model and the biz entities more often than not you would have classes that are the same in both, or is the idea to use the Biz entities as both the model and the biz entites?

    The reason I have the application core is because I will have a winform application that will use some of the components as well. 

    Anyone have any guidence on this?

    Thanks!
    Maxus

     

    Friday, August 20, 2010 6:20 AM

Answers

  • Hi Maxus,

    Views often contain just a few properties of your entities or they contain properties of several entities. By creating a ViewModel you create POCO's that map exactly to the View. The controller can fill the ViewModel by getting anonymous types with Linq queries from e.g. Entity Framework 4. This way no excess data is sent to the View so less logic needed in the View.

    Ofcourse, some Views map exactly to a business entity. MVC allows you to inherit from a generic ViewPage. The generic Type can be a business entity. This means that business entities can be mapped to the View directly if you want and if the View is more complex, you can build a custom ViewModel and map the field with e.g. AutoMapper.


    Regards, Patriek
    • Marked as answer by Maxus01 Friday, August 20, 2010 11:45 AM
    Friday, August 20, 2010 9:34 AM

All replies

  • Hi Maxus,

    Views often contain just a few properties of your entities or they contain properties of several entities. By creating a ViewModel you create POCO's that map exactly to the View. The controller can fill the ViewModel by getting anonymous types with Linq queries from e.g. Entity Framework 4. This way no excess data is sent to the View so less logic needed in the View.

    Ofcourse, some Views map exactly to a business entity. MVC allows you to inherit from a generic ViewPage. The generic Type can be a business entity. This means that business entities can be mapped to the View directly if you want and if the View is more complex, you can build a custom ViewModel and map the field with e.g. AutoMapper.


    Regards, Patriek
    • Marked as answer by Maxus01 Friday, August 20, 2010 11:45 AM
    Friday, August 20, 2010 9:34 AM
  • Hi Patriek,

    Thanks for you reply, so basically it is fine to use the business enties and or the model entiies in within views.

    Are you aware of any best practises documentation for using MVC in similar scenarios?

    Thanks,
    Maxus

    Friday, August 20, 2010 11:51 AM
  • Hi Maxus,

    I've read MVVM is a progression to MVC. I watched a video on MVVM and I combined the two ideas. MVVM is ideal for building WPF/Silverlight applications where databinding is built in the framework making it very easy to bind ViewModels to pages.

    CodeClimber provides 12 best practises (with elaboration) for using ASP.NET MVC. My answer to your question is also explained by him.


    Regards, Patriek
    Friday, August 20, 2010 12:21 PM