The ViewModel pattern

  • 3,051 views
Uploaded on

Even with ASP.NET MVC you can do things in the wrong way: it depends on how you do it. …

Even with ASP.NET MVC you can do things in the wrong way: it depends on how you do it.
The ViewModel pattern helps keeping a clear separation between the view and your domain model.
In the presentation I explain why you need a separate model for your view, and how to make it easier to adopt this approach using AutoMapper

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,051
On Slideshare
0
From Embeds
0
Number of Embeds
6

Actions

Shares
Downloads
57
Comments
0
Likes
2

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. The ViewModel Pattern
    Simone ChiarettaArchitect, Council of the EU
    http://codeclimber.net.nz
    Twitter: @simonech
    June 23rd, 2010
  • 2. Who the hell am I?
    Simone Chiaretta
    Microsoft MVP ASP.NET
    ASP Insider
    Blogger – http://codeclimber.net.nz
    ItalianALT.NET UG Founder
    OpenSource developer
    Climber
    All Around Nice Guy
    Disclaimer:"The viewsexpressed are purelythose of the speaker and may not in anycircumstancesberegarded as stating an official position of the Council"
  • 3. WhatisViewModel
  • 4. Workflow of a MVC Application
    Controller asks for the data to the Model
    The request gets to the Controller
    Model
    2
    1
    3
    Controller
    Browser
    Model returns the data back to the Controller
    Controller formats data and sends them to the View
    View
    4
    5
    View builds the page that is sent back to the Browser
    3
  • 5. Workflow of a MVC Application
    Controller asks for the DomainModel to the BLL
    The request gets to the Controller
    BLL
    2
    1
    3
    Controller
    Browser
    BLL returns the DomainModel to the Controller
    Controller formats DomainModelinto ViewModeland sends it to the View
    View
    4
    5
    View builds the page that is sent back to the Browser
    4
  • 6. DomainModel != ViewModel
    DomainModel
    Data + Behaviours
    Hierarchical, complex types
    ViewModel
    Only Data
    Flat, only strings
  • 7. WhyDomainModelisnotgood?
    Viewsshouldnotknowhowto traverse the DM
    Viewsusuallyneedlessproperties
    UsingORMsyoumight start a SQL querybymistake
  • 8. Howto do it?
    Copy the propertiesneededfrom DM to VM
    Possiblyflatten data
  • 9. DomainModel != ViewModel
    How to avoid getting bored writing tedious mapping code?
  • 10. IntroducingAutoMapper
  • 11. Automapper
    DevelopedbyJimmyBogard
    Latestrelease 1.1
    Active Mailing list
    Downloadablefrom: http://automapper.codeplex.com/
  • 12. Features
    Flattening
    Projection
    FluentAPIconfiguration
    Mapping of List and Collections
    Mapping of NestedObjects
    CustomTypeConverter
    CustomValueResolver
    CustomFormatters
    NullSubstitution
  • 13. Basic Usage
    DefineMapping
    Mapper.CreateMap<Post, ShowPostModel>();
    UseMapping
    Mapper.Map<ListModel, ListViewModel>(viewModel)
  • 14. Projection
    Mapper.CreateMap<EditPageViewModel, Post>()
    .ForMember(
    p => p.Id,
    opt => opt.MapFrom(src => src.Post.Id))
  • 15. CustomValueResolver
    Mapper.CreateMap<EditPageViewModel, Post>()
    .ForMember(
    p => p.Category,
    opt => opt.ResolveUsing<CustomResolver>()
    )
    public class CustomResolver : ValueResolver<EditPageViewModel, Category>
    {
    protected override Category ResolveCore(
    EditPageViewModel source)
    {
    return new InMemoryPostModel()
    .GetCategory(source.Post.CategoryId);
    }
    }
  • 16. NullSubstitution
    Mapper.CreateMap<Post, ShowPostModel>()
    .ForMember(
    p => p.CategoryName,
    opt => opt.NullSubstitute("No Category")
    );
  • 17. Contacts – Simone Chiaretta
    MSN: simone_ch@hotmail.com
    Blog:
    English: http://codeclimber.net.nz/
    Italian: http://blogs.ugidotnet.org/piyo/
    Twitter: @simonech
    16
  • 18. Rating
    If you liked this talk, please consider rating it:
    http://speakerrate.com/talks/3670-the-viewmodel-pattern
    17
    Disclaimer:"The viewsexpressed are purelythose of the speaker and may not in anycircumstancesberegarded as stating an official position of the Council"