Asp.Net Mvc Design Pattern

32
I O C O O D SG A INTRODUCTIONTO DESIGNPATTERN ASP.NET MVC Jaffal Hasan November 2009

description

ASP.NET MVC design pattern, and the importance of using such pattern in order to enhance the asp.net web application structure and make easier their maintainability and testability, and to show the importance of putting rules and guidelines in place of letting the developer decides how to implement the separation of concerns.

Transcript of Asp.Net Mvc Design Pattern

Page 1: Asp.Net Mvc Design Pattern

I O C O O D S G AINTRODUCTION TO DESIGN PATTERNASP.NET MVC

Jaffal HasanNovember 2009

Page 2: Asp.Net Mvc Design Pattern

AGENDA

Design Patterns OverviewMVC Design PatternMVC vs. ASP.NETLayered Application ExampleMVC vs. Layered Application

Page 3: Asp.Net Mvc Design Pattern

DESIGN PATTERN OVERVIEW

Set of guidelinesProvide solutions to common software design

blproblemsConsists of one or several software design elements such as elements such as modules, interfaces, classes, objects, methods, functions, processes, threads, etc.,

Relationships among the elements, and a behavioral descriptionExample design patterns: Model/View/Controller

Page 4: Asp.Net Mvc Design Pattern

DESIGN PATTERN OVERVIEW

Advantages:Improve the structure of softwareSi lif i tSimplify maintenanceShared language for communicatingSeparation of concernsSeparation of concernsMinimize logic needed in viewsEnhance testabilityReduce development timeEasy to customize applications

Disadvantages:Design pattern can be overkill in Simple UI

Page 5: Asp.Net Mvc Design Pattern

GOALS• Separation of concerns

• Decoupling the layers and componentsR d i d l i ( l i i )• Reducing development time ( multi processes at time)

• TestabilityFl ibilit• Flexibility• Minimal Code in UI• Changes in layers: DBMS • Changes in layers: DBMS • Change or use multiple platform to present data (mobile – web

..)

Page 6: Asp.Net Mvc Design Pattern

WHAT IS ASP.NET MVC?A new Web Application Project typeSimply an option

N l f W bFNot a replacement for WebFormsBuilds on top ASP.NETManual vs. Automatic Transmission

Existing ASP.NET features such as master pages, web controls, validation, membership integrated.

Page 7: Asp.Net Mvc Design Pattern

WHAT MVC IS NOT ?Not the new Web Forms 4.0Not replacing Web Forms, but Adds to itNot a whole new engine but sits on ASP.NET engine

Page 8: Asp.Net Mvc Design Pattern

WHAT MVC IS?Maintain Clean Separation of ConcernsExtensible and PluggableGreat integration within ASP.NET

Page 9: Asp.Net Mvc Design Pattern

MVC

Model

All b i All b i

View

UIUI

Controller

H dl d H dl d • All business logic

• ADO.NET,

• All business logic

• ADO.NET,

• UI• UI • Handles and response user interaction

• Handles and response user interaction

Linq, Linq to SQLLinq, Linq to SQL

• Work with model to retreive

• Work with model to retreiveinformation and select view to render

information and select view to render with model data

• Input logic

with model data

• Input logicp og cp og c

Page 10: Asp.Net Mvc Design Pattern

MVC

Model

Page 11: Asp.Net Mvc Design Pattern

MVC FLOW

Page 12: Asp.Net Mvc Design Pattern

MVC FLOW

ControllerControllerRequest

Step 1Step 1Incoming request directed to Controller

Page 13: Asp.Net Mvc Design Pattern

MVC FLOW

ControllerControllerModelModelModelModel

Step 2Step 2Controller processes request and forms a data Model

Page 14: Asp.Net Mvc Design Pattern

MVC FLOW

ControllerController

ViViViewViewStep 3Step 3Model is passed to View

Page 15: Asp.Net Mvc Design Pattern

MVC FLOW

ControllerController

ViewView

Step 4Step 4View transforms Model into appropriate output format

Page 16: Asp.Net Mvc Design Pattern

MVC FLOW

ControllerController

ViewViewResponseResponse

ViewView

Step 5Step 5Response is rendered

Page 17: Asp.Net Mvc Design Pattern

MVC : MODEL – VIEW - CONTROLLER

Page 18: Asp.Net Mvc Design Pattern

R FR FREQUEST FLOW –DETAILEDREQUEST FLOW –DETAILED

Request Request

HTTPHTTPRoutingRouting

HttpHttpHandlerHandler ControllerController ResponseResponse

RouteRoute RouteRouteHandlerHandler

ViewViewEngineEngine ViewView

Page 19: Asp.Net Mvc Design Pattern

MVC VS. “CLASSIC” ASP.NET

Classic ASP.NET MVC Framework

Request DataRequest Data

REST MVCPostback Code behind

Page 20: Asp.Net Mvc Design Pattern

MVC VS. “CLASSIC” ASP.NETMVC i b d ASP NET f kMVC is based on ASP.NET frameworkREST model versus the postback model of classic ASP.NET.Each page is split into two distinct components -controller and view - that operate over the same model of data. This is opposed to the classic code-behind model where no barrier is set that forces you to think in terms of separation of concerns and controllers and views. A good developer could achieve separation of concerns even without adopting MVC and its overhead. p gMVC is a model superior to a properly-done code-behind for its inherent support for test-driven development. p

Page 21: Asp.Net Mvc Design Pattern

REST ? Set of guidelines

REST is an architectural pattern that defines how network resources should be defined and addressed in order to gain shorter response addressed in order to gain shorter response times, clear separation of concerns between the front-end and back-end of a networked systemy

Page 22: Asp.Net Mvc Design Pattern

WHAT? NO MORE POSTBACKS!The MVC Framework

Doesn't support classic postbacks and viewstateD 't id URL th d i t t Doesn't consider any URL as the endpoint to a physical server file to parse and compile to a class.

In ASP.NET, you have a 1:1 correspondence between a URL and a resource.

Page 23: Asp.Net Mvc Design Pattern

WHY MVC?Very Clean separation of ConcernsUnit TestingModel that leads you down a maintainable path (prevent spaghetti code)Cl URL d HTMLClean URLs and HTML

Page 24: Asp.Net Mvc Design Pattern

CLEAN URLUrl no longer points to the view/aspx pageA Url maps to a controller classAllows more flexibility to map UrlsUrl routing engine, Route table

www.arabiagis.com/software/powermap Or

www.arabiagis.com/software.aspx?id=23

Page 25: Asp.Net Mvc Design Pattern

ROUTING ENGINE

URLs -> application -> Controller ActionConstruct outgoing URLs

Constructed URLs can be used to call back to Controllers/Actions

Page 26: Asp.Net Mvc Design Pattern

LAYERED APPLICATIONS SOUNDS GOOD

Layered application design concept sounds good, but the problem is when it's time to code it. L t’ S l Let’s See an example

PresentationLayer

PresentationLayerayeaye

BLL

DAL

Page 27: Asp.Net Mvc Design Pattern

AL THEYAB FLIGHTAVAILABILITY.ASPXRE-VISIT

Is the codeMaintainable ?T t bl ?Testable ?

Are the object serializable ?serializable ?

The presentation layer can be simply tested and changed?changed?Can we display the same data using a platform other than the webforms?other than the webforms?Where is the separation of concerns?

Presentation – Business Logicg

Page 28: Asp.Net Mvc Design Pattern

WHO’S RESPONSIBLE?The Coder?The Developer ?The Designer?All of them:

The designer should put clear and clean guidelines!The coder should follow the guidelines and rulesTh d l h ld d hi k thi k b f The developer should do his work: think before coding.

Page 29: Asp.Net Mvc Design Pattern

WHO’S RESPONSIBLE?

Many developers can argue that they are doing a layered application, but they are writing all the logic in

h d b hi d fil f f

D l t t t th b i l i d

the code behind file of a form. Or Some of

Developers try to separate the business logic and the data layers.E h diff t id d i Everyone has different ideas and experiences about doing this. So even when layers have a defined responsibility So even when layers have a defined responsibility its necessary to restrict the way the application is actually coded. And the only way to do that is y y yapplying a design that affects the code structure.

Page 30: Asp.Net Mvc Design Pattern

BEST CHOICE

1 Guidelines and Rules 1 Developer decides

MVC Layered Application

1. Guidelines and Rules2. Shared language for

communicating

1. Developer decides2. Every team member

speaks a languagecommunicating3. Easy to maintain4 Easy to understand

p g g3. Hard to maintain4. Ambiguous and

4. Easy to understand the structure

5 The code depends of

variant structure5. Application is

dependent of the 5. The code depends of the developer skills and opinions

dependent of the developer skills and opinions

Page 31: Asp.Net Mvc Design Pattern

DEMO

Page 32: Asp.Net Mvc Design Pattern

Practical

“You should be pragmatic with any pattern,it's not necessary to be applicable for every situation”

Jaffal HasanJaffal Hasan

THANKS FOR LISTENINGTHANKS FOR LISTENING.Mail: [email protected] profile: http://www linkedin com/in/jaffalLinkedIn profile: http://www.linkedin.com/in/jaffalBlog: www.solutionsarchitecture.wordpress.com