SlideShare une entreprise Scribd logo
1  sur  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"

Contenu connexe

Similaire à The ViewModel Pattern with AutoMapper for Flattening and Mapping Data

ASP.NET MVC Introduction
ASP.NET MVC IntroductionASP.NET MVC Introduction
ASP.NET MVC Introductionkishanzunjare
 
Serverless Orchestration with Azure Durable Functions
Serverless Orchestration with Azure Durable FunctionsServerless Orchestration with Azure Durable Functions
Serverless Orchestration with Azure Durable FunctionsCallon Campbell
 
Ppt of Basic MVC Structure
Ppt of Basic MVC StructurePpt of Basic MVC Structure
Ppt of Basic MVC StructureDipika Wadhvani
 
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 2013Thomas Robbins
 
Introduction to mvc architecture
Introduction to mvc architectureIntroduction to mvc architecture
Introduction to mvc architectureravindraquicsolv
 
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...
SoftServe - "ASP.NET MVC як наступний крок у розвитку технології розробки Web...SoftServe
 
Joe Ulyatt MVC Lecture
Joe Ulyatt MVC LectureJoe Ulyatt MVC Lecture
Joe Ulyatt MVC Lecturessuser7e32f9
 
Model-View-Controller: Tips&Tricks
Model-View-Controller: Tips&TricksModel-View-Controller: Tips&Tricks
Model-View-Controller: Tips&TricksCiklum Ukraine
 
Mvc 130330091359-phpapp01
Mvc 130330091359-phpapp01Mvc 130330091359-phpapp01
Mvc 130330091359-phpapp01Jennie Gajjar
 
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 functionsCallon Campbell
 
Overview of CSharp MVC3 and EF4
Overview of CSharp MVC3 and EF4Overview of CSharp MVC3 and EF4
Overview of CSharp MVC3 and EF4Rich Helton
 
OpenB Web Engine - Conceptual overview
OpenB Web Engine - Conceptual overviewOpenB Web Engine - Conceptual overview
OpenB Web Engine - Conceptual overviewWilko van der Veen
 

Similaire à The ViewModel Pattern with AutoMapper for Flattening and Mapping Data (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
 

Plus de Simone Chiaretta

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 ...Simone Chiaretta
 
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 oceanSimone Chiaretta
 
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 4Simone Chiaretta
 
FeedTso, History of a WP7 FeedReader
FeedTso, History of a WP7 FeedReaderFeedTso, History of a WP7 FeedReader
FeedTso, History of a WP7 FeedReaderSimone Chiaretta
 
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 MVCSimone Chiaretta
 
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)Simone Chiaretta
 
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.comSimone Chiaretta
 

Plus de 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
 

Dernier

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
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 Servicegiselly40
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
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...Drew Madelung
 
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 interpreternaman860154
 
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 DevelopmentsTrustArc
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 

Dernier (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
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
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
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...
 
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
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 

The ViewModel Pattern with AutoMapper for Flattening and Mapping Data

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