asp.net mvc design pattern

Post on 11-May-2015

20.966 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

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

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

Jaffal HasanNovember 2009

AGENDA

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

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

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

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

..)

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.

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

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

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

MVC

Model

MVC FLOW

MVC FLOW

ControllerControllerRequest

Step 1Step 1Incoming request directed to Controller

MVC FLOW

ControllerControllerModelModelModelModel

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

MVC FLOW

ControllerController

ViViViewViewStep 3Step 3Model is passed to View

MVC FLOW

ControllerController

ViewView

Step 4Step 4View transforms Model into appropriate output format

MVC FLOW

ControllerController

ViewViewResponseResponse

ViewView

Step 5Step 5Response is rendered

MVC : MODEL – VIEW - CONTROLLER

R FR FREQUEST FLOW –DETAILEDREQUEST FLOW –DETAILED

Request Request

HTTPHTTPRoutingRouting

HttpHttpHandlerHandler ControllerController ResponseResponse

RouteRoute RouteRouteHandlerHandler

ViewViewEngineEngine ViewView

MVC VS. “CLASSIC” ASP.NET

Classic ASP.NET MVC Framework

Request DataRequest Data

REST MVCPostback Code behind

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

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

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.

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

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

ROUTING ENGINE

URLs -> application -> Controller ActionConstruct outgoing URLs

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

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

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

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.

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.

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

DEMO

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: hjaffal@arabiagis.comLinkedIn profile: http://www linkedin com/in/jaffalLinkedIn profile: http://www.linkedin.com/in/jaffalBlog: www.solutionsarchitecture.wordpress.com

top related