Your SlideShare is downloading. ×
0
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
The ViewModel pattern
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

The ViewModel pattern

3,169

Published 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

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

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

×