the viewmodel pattern

Post on 06-Dec-2014

3.923 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

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

The ViewModel Pattern

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

June 23rd, 2010

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"

What is ViewModel

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

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

DomainModel != ViewModel

► DomainModel– Data + Behaviours– Hierarchical, complex types

► ViewModel– Only Data– Flat, only strings

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

How to do it?

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

DomainModel != ViewModel

► How to avoid getting bored writing tedious mapping code?

Introducing AutoMapper

Automapper

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

Features

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

Basic Usage

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

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

Projection

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

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

src.Post.Id))

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

}

Null Substitution

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

17

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"

top related