Download - Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

Transcript
Page 1: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

Mohamed Meligy

Applying Domain Driven Design on

Asp.net MVC

Page 2: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

Agenda ASP.NET MVC, The Steps

Model, Controller, View

Why ASP.NET MVC

Domain, AKA, Model The Domain Model

The Ubiquitous Language

Building Blocks Entities / Value Objects

Aggregates / Factories

Repositories

Services / Modules

Putting it all together

Page 3: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

ASP.NET MVC – Form few thousand feet …

Model

ControllerView

Page 4: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

ASP.NET Futures Themes

Webforms

AJAX

MVC

Dynamic Data

Feature Sharing

Webforms with AJAX

Dynamic Data with MVC

jQuery

Page 5: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

M – V – C, The Steps …

RequestRequest

Routing

ForwardForward

Step Incoming request matched by Step 0Incoming request matched by Routing

Page 6: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

M – V – C, The Steps …

RequestRequest

Controller

Step Incoming request directed to Step 1Incoming request directed to Controller

Page 7: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

M – V – C, The Steps …

Controller

Model

Step ControllerStep 2Controller processes request and forms a data Model

RequestRequest

Page 8: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

M – V – C, The Steps …

Controller

View

Step ModelStep 3Model is passed to View

RequestRequest

Page 9: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

M – V – C, The Steps …

Controller

View

Step ViewStep 4View transforms Model into appropriate output format

RequestRequest

Page 10: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

M – V – C, The Steps …

Response

Controller

View

Step Step Response is renderedStep Step 55Response is rendered

RequestRequest

Page 11: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

The ASP.NET MVC Building Blocks

Simply creating a new project

Page 12: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

The model

The class that needs to be presented, or processed

Can be bound in controller

Model Binders

Can live anywhere in your solution

Page 13: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

The Controller

A set of action methods that share a common

Controller context

… which is different from HTTP context

An action:

1. Receives the request parameters

2. Creates any required model (user, etc..)

3. Calls services or repositories to execute data or get

the models to be displayed

4. Selects the view and sends the model to it, with any

additional info (like page title, …)

Page 14: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

The ASP.NET MVC Building Blocks

ROUND TWO !!

Page 15: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

The View Just renderer

No logic should go there

Page lifecycle ?

Yes and No!

Code Behind exists but not recommended

No View State, No Server Controls

Really?

HTML Helpers

Takes the view data from the controller

Can be typed or Dictionary of values

Not Just Web forms code

View Engines

Page 16: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

View Engines

Web forms

Web forms

• The default option

• Comes with VS

• Same experience:

• Syntax

• VS Support

Page 17: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

View Engines

NHAML

• engine

NHAML

• Based on Ruby On Rails HAML Engine

• http://code.google.com/p/nhaml

• http://andrewpeters.net/2007/12/19/introducing-nhaml-an-aspnet-mvc-view-engine

Page 18: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

The webforms view Engine

From ASP.NET MVC source code

Page 19: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

Why Oh, WHY !!!

Easier …

Tight control over markup

User/SEO friendly URLs

Conventions and Guidance

jQuery (Built-in)

Tight control: Not just markup

Everything is replaceable

Interface based style

Not full with sealed/internal stuff

Testability

The guys who created it are just awesome

94% Code Coverage/1051 unit tests

Page 20: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

The other side of the story

It’s not as easy as you think!

To go low level or not to go; this IS the question.

IIS 6 Quirks

Custom Controls

Page 21: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

Where to go

Official website http://asp.net/mvc

Source Code http://codeplex.com/aspnet

Blogs

The ASP.NET MVC Team

Scott Guthrie http://weblogs.asp.net/scottgu

Rob Conrey http://blog.wekeroad.com

MVC Store Front http://blog.wekeroad.com/mvc-storefront

Phil Haack http://haacked.com

Scott Hanselman http://hanselman.com

Stephen Walthler http://weblogs.asp.net/stephenwalther

http://www.chrisvandesteeg.nl

http://www.singingeels.com

Page 22: Applying Domain Driven Design on Asp.net MVC – Part 1: Asp.net MVC

http://weblogs.asp.net/meligy