the viewmodel pattern

18
The ViewModel Pattern Simone Chiaretta Architect, Council of the EU http://codeclimber.net.nz Twitter: @simonech June 23 rd , 2010

Upload: simone-chiaretta

Post on 06-Dec-2014

3.923 views

Category:

Technology


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: The ViewModel pattern

The ViewModel Pattern

Simone ChiarettaArchitect, Council of the EUhttp://codeclimber.net.nzTwitter: @simonech

June 23rd, 2010

Page 2: The ViewModel pattern

Who the hell am I?

► Simone Chiaretta► Microsoft MVP ASP.NET► ASP Insider► Blogger – http://codeclimber.net.nz ► Italian ALT.NET UG Founder► OpenSource developer► Climber► All Around Nice Guy

Disclaimer:"The views expressed are purely those of the speaker and may not in any circumstances be regarded as stating an official position of the Council"

Page 3: The ViewModel pattern

What is ViewModel

Page 4: The ViewModel pattern

Workflow of a MVC Application

4

Model

View

Controller

1

5

2

4

3

Browser

The request gets to the Controller

Controller asks for the data to the Model

Model returns the data back to the

Controller

Controller formats data and sends them to the View

View builds the page that is sent back to the Browser

Page 5: The ViewModel pattern

Workflow of a MVC Application

5

BLL

View

Controller

1

5

2

4

3

Browser

The request gets to the Controller

Controller asks for the DomainModel to the BLL

BLL returns the DomainModel to the Controller

Controller formats DomainModel into ViewModel and sends it to the View

View builds the page that is sent back to the Browser

Page 6: The ViewModel pattern

DomainModel != ViewModel

► DomainModel– Data + Behaviours– Hierarchical, complex types

► ViewModel– Only Data– Flat, only strings

Page 7: The ViewModel pattern

Why DomainModel is not good?

► Views should not know how to traverse the DM

► Views usually need less properties► Using ORMs you might start a SQL query by mistake

Page 8: The ViewModel pattern

How to do it?

► Copy the properties needed from DM to VM► Possibly flatten data

Page 9: The ViewModel pattern

DomainModel != ViewModel

► How to avoid getting bored writing tedious mapping code?

Page 10: The ViewModel pattern

Introducing AutoMapper

Page 11: The ViewModel pattern

Automapper

► Developed by Jimmy Bogard► Latest release 1.1► Active Mailing list► Downloadable from: http://automapper.codeplex.com/

Page 12: The ViewModel pattern

Features

► Flattening► Projection► FluentAPI configuration► Mapping of List and Collections► Mapping of Nested Objects► Custom Type Converter► Custom Value Resolver► Custom Formatters► Null Substitution

Page 13: The ViewModel pattern

Basic Usage

► Define Mapping– Mapper.CreateMap<Post, ShowPostModel>();

► Use Mapping– Mapper.Map<ListModel, ListViewModel>(viewModel)

Page 14: The ViewModel pattern

Projection

Mapper.CreateMap<EditPageViewModel, Post>().ForMember(

p => p.Id,opt => opt.MapFrom(src =>

src.Post.Id))

Page 15: The ViewModel pattern

Custom Value Resolver

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);}

}

Page 16: The ViewModel pattern

Null Substitution

Mapper.CreateMap<Post, ShowPostModel>() .ForMember(

p => p.CategoryName,opt => opt.NullSubstitute("No

Category"));

Page 17: The ViewModel pattern

Contacts – Simone Chiaretta

► MSN: [email protected]► Blog:

– English: http://codeclimber.net.nz/– Italian: http://blogs.ugidotnet.org/piyo/

► Twitter: @simonech

17

Page 18: The ViewModel pattern

Rating

If you liked this talk, please consider rating it:

http://speakerrate.com/talks/3670-the-viewmodel-pattern

18 Disclaimer:"The views expressed are purely those of the speaker and may not in any circumstances be regarded as stating an official position of the Council"