SlideShare ist ein Scribd-Unternehmen logo
1 von 18
The ViewModel Pattern Simone ChiarettaArchitect, Council of the EU http://codeclimber.net.nz Twitter: @simonech June 23rd, 2010
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"
WhatisViewModel
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
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
DomainModel != ViewModel DomainModel Data + Behaviours Hierarchical, complex types ViewModel Only Data Flat, only strings
WhyDomainModelisnotgood? Viewsshouldnotknowhowto traverse the DM Viewsusuallyneedlessproperties UsingORMsyoumight start a SQL querybymistake
Howto do it? Copy the propertiesneededfrom DM to VM Possiblyflatten data
DomainModel != ViewModel How to avoid getting bored writing tedious mapping code?
IntroducingAutoMapper
Automapper DevelopedbyJimmyBogard Latestrelease 1.1 Active Mailing list Downloadablefrom: http://automapper.codeplex.com/
Features Flattening Projection FluentAPIconfiguration Mapping of List and Collections Mapping of NestedObjects CustomTypeConverter CustomValueResolver CustomFormatters NullSubstitution
Basic Usage DefineMapping Mapper.CreateMap<Post, ShowPostModel>(); UseMapping Mapper.Map<ListModel, ListViewModel>(viewModel)
Projection Mapper.CreateMap<EditPageViewModel, Post>() 	.ForMember( 			p => p.Id, opt => opt.MapFrom(src => src.Post.Id))
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); 	} }
NullSubstitution Mapper.CreateMap<Post, ShowPostModel>()         .ForMember( 			p => p.CategoryName, opt => opt.NullSubstitute("No Category") 		);
Contacts – Simone Chiaretta MSN: simone_ch@hotmail.com Blog: English: http://codeclimber.net.nz/ Italian: http://blogs.ugidotnet.org/piyo/ Twitter: @simonech 16
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"

Weitere ähnliche Inhalte

Ähnlich wie The ViewModel pattern

SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe
 
Mvc 130330091359-phpapp01
Mvc 130330091359-phpapp01Mvc 130330091359-phpapp01
Mvc 130330091359-phpapp01
Jennie Gajjar
 

Ähnlich wie The ViewModel pattern (20)

ASP.NET MVC Introduction
ASP.NET MVC IntroductionASP.NET MVC Introduction
ASP.NET MVC Introduction
 
Mvc part 1
Mvc part 1Mvc part 1
Mvc part 1
 
Mvc
MvcMvc
Mvc
 
Serverless Orchestration with Azure Durable Functions
Serverless Orchestration with Azure Durable FunctionsServerless Orchestration with Azure Durable Functions
Serverless Orchestration with Azure Durable Functions
 
Spring Framework-II
Spring Framework-IISpring Framework-II
Spring Framework-II
 
Ppt of Basic MVC Structure
Ppt of Basic MVC StructurePpt of Basic MVC Structure
Ppt of Basic MVC Structure
 
L10 Web Programming
L10 Web ProgrammingL10 Web Programming
L10 Web Programming
 
Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013
 
Introduction to mvc architecture
Introduction to mvc architectureIntroduction to mvc architecture
Introduction to mvc architecture
 
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
 
Joe Ulyatt MVC Lecture
Joe Ulyatt MVC LectureJoe Ulyatt MVC Lecture
Joe Ulyatt MVC Lecture
 
Model-View-Controller: Tips&Tricks
Model-View-Controller: Tips&TricksModel-View-Controller: Tips&Tricks
Model-View-Controller: Tips&Tricks
 
ASP.NET MVC
ASP.NET MVCASP.NET MVC
ASP.NET MVC
 
No brainer
No brainerNo brainer
No brainer
 
Mvc 130330091359-phpapp01
Mvc 130330091359-phpapp01Mvc 130330091359-phpapp01
Mvc 130330091359-phpapp01
 
Introduction to serverless compute with azure functions
Introduction to serverless compute with azure functionsIntroduction to serverless compute with azure functions
Introduction to serverless compute with azure functions
 
Overview of CSharp MVC3 and EF4
Overview of CSharp MVC3 and EF4Overview of CSharp MVC3 and EF4
Overview of CSharp MVC3 and EF4
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 
OpenB Web Engine - Conceptual overview
OpenB Web Engine - Conceptual overviewOpenB Web Engine - Conceptual overview
OpenB Web Engine - Conceptual overview
 
Asp.net mvc
Asp.net mvcAsp.net mvc
Asp.net mvc
 

Mehr von Simone Chiaretta

Mehr von Simone Chiaretta (10)

Fast and furious(ly) multilingual: Publishing of EU politics in 24 languages ...
Fast and furious(ly) multilingual: Publishing of EU politics in 24 languages ...Fast and furious(ly) multilingual: Publishing of EU politics in 24 languages ...
Fast and furious(ly) multilingual: Publishing of EU politics in 24 languages ...
 
OpenROV: Node.js takes a dive into the ocean
OpenROV: Node.js takes a dive into the oceanOpenROV: Node.js takes a dive into the ocean
OpenROV: Node.js takes a dive into the ocean
 
La UX delle cose
La UX delle coseLa UX delle cose
La UX delle cose
 
UGIALT.net Keynote
UGIALT.net KeynoteUGIALT.net Keynote
UGIALT.net Keynote
 
What's new in asp.net mvc 4
What's new in asp.net mvc 4What's new in asp.net mvc 4
What's new in asp.net mvc 4
 
FeedTso, History of a WP7 FeedReader
FeedTso, History of a WP7 FeedReaderFeedTso, History of a WP7 FeedReader
FeedTso, History of a WP7 FeedReader
 
Ruby on Rails vs ASP.NET MVC
Ruby on Rails vs ASP.NET MVCRuby on Rails vs ASP.NET MVC
Ruby on Rails vs ASP.NET MVC
 
Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)Design for testability as a way to good coding (SOLID and IoC)
Design for testability as a way to good coding (SOLID and IoC)
 
ASP.NET MVC Extensibility
ASP.NET MVC ExtensibilityASP.NET MVC Extensibility
ASP.NET MVC Extensibility
 
Lavorare con applicazioni Brownfield: il caso di 39x27.com
Lavorare con applicazioni Brownfield: il caso di 39x27.comLavorare con applicazioni Brownfield: il caso di 39x27.com
Lavorare con applicazioni Brownfield: il caso di 39x27.com
 

Kürzlich hochgeladen

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Kürzlich hochgeladen (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 

The ViewModel pattern

  • 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"
  • 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?
  • 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"