ASP.NET MVC Practice and Guideline Refresh!
Zubzib Black Coffee #7.5 กิ�น กิาแฟเขียนเว็�บ !
About me
Chalermpon Areepong (Nine)
Microsoft MVP ASP.NET
Work @
Session Level 300Audience Skill HTML/JS/CSS JavaScript C# OOP LINQ
Agenda Basic ASP.NET MVC Programming NuGet Package Manager Entity Framework 4.x Code First Introduce EF Migration Deep in Model Deep in Controller Deep in View UI JavaScript Framework Application Layer Architecture Repository Pattern Dependency Injection Security ASP.NET MVC 4 Preview
Basic of ASP.NET MVC Programing
Model : o Class Modelo Entity Modelo Domain Transfer Object (DTO)o View Model
View : User Interfaceo Engine : ASPX ViewEngine, Razor ViewEngine, Etc……o Output: HTML, CSS, JavaScript, JSON, Text
Controllero Control logic, o Data access, o Prepare parameters for view rendero call view to render output
Basic of ASP.NET MVC Programing
DEMO
Basic of ASP.NET MVC Programing
Routeso Catch URL to parameter by pattern
• Default “{controller}/{action}/{id}”http://www.zz.com/blog/get/1
• Fix route value
new { controller = “blog”, action = “get”, id = 1 }
• Custom Routes• blog/Archive/12-25-2009
“{controller}/{action}/{date}”http://www.zz.com/blog/Archive/12-25-2009
Public ActionResult Archive(DateTime date)
• blog/Archive/12-25-2009_12-17-2011“{controller}/{action}/{dateFrom}_{dateTo}”http://www.zz.com/blog/Archive/12-25-2009_12-17-2011 public ActionResult Archive(DateTime dateFrom, DateTime DateTo)
Basic of ASP.NET MVC Programing
Routes DEMO
NuGet Package Manager
Manage Library for o Searcho Install / Update / Uninstallo Execute library command (PowerShell)
VS - > Menu -> Tools -> Extension Manager -> Find NuGet and install
How to useo Solution->Project -> right click on +Reference -> nuget
packageo Package Manager Console
• Install-Package <package name>• Uninstall-Package <package name>• Get-Package• Update-Package //all available
NuGet Package Manager
DEMO
EF 4.x Code First
Object Relational Mapping POCO Convention Over Configuration LINQ Support Working with various database (3rd party driver) Fluent API to fully customize the persistence
mapping Database Installation Database Migration
EF 4.x Code First
DEMO
Deep in Model
Model?o Rich Domain? o Model Entity?o DTO?
Validation?o Server Side
• DataAnotation Attribute• IValidationObject• FluentValidate
o Client Side• ClientValidation• IClientValidatable
Demo
Deep in Controller
Controller ActionResult
Demo
Deep in View
ViewEngineo Razor ViewEngine (WebPage)o Aspx ViewEngineo 3rd Party ViewEngine
JavaScript
Demo
UI JavaScript Framework
All will render to HTML/ CSS / JavaScript JQuery UI Telerik MVC Extension (strong type helper) Knockout (MVVM) Kendo UI (HTML5)
UI JavaScript Framework
DEMO
Software Design: Repository Pattern
Encapsulate data access Simple collection like interface Should only return aggregate roots Provide for adding and removing entities Provide methods to select objects based on some
criteria Allow easy substitution
Software Design: Repository Pattern
DEMO
Software Design: Core Layer Architecture
Motivation – Logical layers separate the major concerns of the application: 1. Modularity, low-coupling – easier maintenance. 2. Business-logic is separated from presentation –
reuse. 3. General technical services, e.g., database, are
separated from the business-logic – reused, replaced.
4. Low coupling, separation of concerns – evolving functionality, system scaling-up, adapt to new technologies.
Software Design: Core Layer Architecture
Software Design: Core Layer Architecture
DEMO
Dependency Injection
Dependency Injection - one example of IoC design principle.
Also known as the Hollywood Principleo Don’t call us, we’ll call you!
Good OO Systems – organised as web of interacting objects
Goal – High cohesion, low coupling Advantages of low coupling
o Extensibilityo Testabilityo Reusability
PS>Not so easy to achieve!
Dependency Injection
AutoFac AutoFac MVC Integration
Demo
Security
Windows Authentication Form Authentication Asp.net Membership Provider Controller and Action
o Authorize Attributeo FluentSecurity (robust, fluent syntax)
ASP.NET MVC 4 Preview
New Default project templates New mobile project template Recipes to customize code generation Asynchronous methods
Summary
Bye…