MVC The Good, The Bad and the Evil Hadi Hariri Developer & Technical Evangelist JetBrains http://hadihariri.com http://twitter.com/hhariri
<inpu nput type pe=" ="hid hidden den" " name=" ="__VI VIEW EWST STAT ATE" E" id="_ ="__VI VIEW EWST STAT ATE" E" value lue=" ="B6w B6wx5mi4gVh i4gVhW2+ 2+73 73t44EG 44EGLv LvhH hH6G 6GoM oM0g 0gz+lg +lg712/ 12/i1 i1Bz Bz/s1kL8w L8wqD qDTPb Pbs2j4J j4JJG0Yt 0Yt4w 4wHld ldE5jD E5jDQ1Q Q1QMue ueIk17 5MDIHxwsdFnk nkkcAiN AiNQ0 Q05SPJ 5SPJDyoR oRiBrd iBrdze58N 58NqG/ G/TSJ SJdgG gGkvAJ AJYr YrQL QL6t 6tImJqCdL3F dL3FDMh7F 7FyOX6 OX6TKx Kx2ZuH uHUUQQja QQja dUPy PyzMecyWre re35y 35yQ/ Q/gRkOD OD5Kq 5KqnqKib nqKibxTAa Aa9o 9o2QBZ 2QBZV1 V1iOx iOxJku+ u+kZEOGq EOGqBW BWgW gWgD gDEz EzXagC XagCId5 d5fo/21YPC 1YPCfkpix pixmZX 2h25 25NiBG iBGCqE qEHcWvniF niFCYc Yc03XPv 03XPvnQ nQYa2SAC Ya2SAC7z 7zfaBbSAt aBbSAtgM gMM+R5R 5R90t 90tvdU dUmPX PXCGiO GiOuO uOG9PJ G9PJNI9EU EUMI9XF 9XFn0f 0fSw Sw RkGcG GcGxH8jU 8jUUh/ h/nj8 nj8sPM PMySpj Spjiy iy6+ 6+Rpc pc4HK0z K0zfI12S 12ScvAJ2 AJ2LoE LoE3db dbl5I l5I2ag 2agSG SGTLNXt Xt26 26GlB GlBaF aFJR2R 2RlR lRwRkqXeR XeR/2KhG KhGK D/wkCOQk OQkDBs BsK6E6h K6E6h2p 2piPqJ iPqJUMl1 l1zMlc lcK51 K51WaF aFiglT iglTdP+ P+bSC bSC77 77CVlp Vlp+u +uDpx pxCfPw PwBlf BlfFzsnh nhow owZ6SEJ 6SEJgs gsAy AyVs VsTeDzm BYWUMLqs qsk/Rn8J n8JP4E0 P4E001M 01MoaKd oaKdlhu lhu0v 0vECVD VDhQVKW hQVKWq1 q1cjw jwEhZ EhZR5I 5I/ID5D 5D1mlQGYg lQGYg4Qi8 4Qi8p/ p/Lf LfTJE0YdPXd E0YdPXdMQu Qu91I 91I+ x5t 5t22 22Z8XSN 8XSNppAL ppALsz30 30Gc Gc02 02Sm SmXv XvxGM GMG36 36OK/ OK/6x 6xh1 h16Qp 6QpaD aDzt4s9CflI lIFJYW YWCH12nJ 12nJ8BSF SFTvbK6y K6ykPp Pphur hurscJtDg3X g3X mGKx KxNVT VTRlO lOBhI BhIxV6 V6i4v i4vy6ds dssmfXL XLA1H A1HUTnCOU OUuL+ uL+05r 5rrJ rJf8N 8NH8pPBGB4 8pPBGB4jX+ X+6ojn 6ojn7T 7TzSx SxTrM rMNkWIiom iomCeMv1Xm XmLl Ll 0fr6v r6vs/rcfoG oG6q 6qy8mCxyRq6C q6CtxU5/ 5/7t 7tblr blrs2gt gtR9w 9wQSN QSNyfNFv4Ps PsCspv pvay ay31 31Er+a Er+anQN nQNqcsNh3O h3OMXKR XKRHEM EM0H 0HIR8jE23 8jE23 M9QK QKam amfXeL XeLXfYe XfYeDFGe0 e0C1M 1MXC XCdeq deqb8a b8a5x 5xZKPa KPaxMI36 36Mo6p o6poXE oXEiC iCxj6 j6dGN dGNC9R 9R34 34HK4 K4/E+ E+m6VU 6VUR6W 6WE+ E+3XR XR8Xg 8Xg7 mpvO3N 3NVA VADYKZ YKZ/8iioc 8iiocIp0 p0WbH bHh/ h/1oH oHJUeL eLSuN SuN2jU 2jUw4p7 4p7Gnp GnphVt hVtr/ r/aM aMTNCYXX5gf YXX5gfU4SW 4SWTyds dsys6VO 6VOSQel6 SQel6k7c7e0 e05 q+ev evtyqXSz Sz65 65LI LIklhy lhy3I 3If0QN QNIc9wiVU iVUiuR iuRRoLz oLznL nLfCWPZ PZPlW PlWFkAYQ7c AYQ7cn2eKT 2eKTNMBJ BJLYM LYMr6I 6I5q 5qwrs rsGUeF eFdN dNi43 i43mjT jTj7B j7B LXr1u r1uEXG XGg2 g29U 9UJmeB2ay B2ayqjC jCc5q 5q5/ 5/az azue ueeT eTi4 i47u6 7u61Vk 1Vkk9Qy 9QyTNxo3XU 3XU8g 8gnI nItEAiz EAizlH lH97U 97UT7L 7LqKn qKntCIjC jCKoju KojuzPhT PhTY2Q Y2QKW KW q1Rs9U+4 +4DWSR SRZV/ V/rt+j9v j9vE9SO0 E9SO04z 4zXy Xy2rm rmvr5OL 5OLLYoa LYoaiXbt iXbtIlQs lQsL5P6 L5P65v 5vj8N j8NuL uLOF OF8jb8+ jb8+YBv BvMDscSVBD SVBD0iC 0iCeD eDgh/ gh/Hxj npNstdI dIzf77O 7OX/ X/xnH nHPW PWQE QE/yWge1 ge1ajz ajzRCQW QWUU44E6h 44E6hvueiu eiuoH oHu5 u5Ewhp hpeQ eQ2oF 2oFHDAk Akv7Yt Yt/G0 G0URLS2 LS2L85 L85uL uLWaX3t aX3tH 5pRHlf lf97 97dnK dnKVT VT1HZ6hr 6hrgS5 gS5BYm BYmlz lzWxBn Bnvjr jr6u5 6u5pQ pQPpR PpRHEh EhcHpBW pBWaYR aYR3ejX 3ejXdmjgw jgwvRdn8g n8gr9 r9SAT SAT7Eo 7EonXKu XKu/Bw Bw2g 2gVo Vo 3nlAiW lAiW4p+ 4p+viPU iPU4jBAU 4jBAUXt Xtkat8I 8Ihn hnrQv rQvNulJ ulJ6s 6skDbI bIznO nOLD LDg1 g1NRqr qrzDHqt qt+w +w1H 1HJUI6s 6slu8 lu8BY5 BY5tfD0lKM 0lKMRTwBKu BKuac acny nyTwnF nF Uf4e7 4e7z1iBR iBRo5+ o5+X1 X1dH dHSGH SGHd70 d70zpQED QEDR0S4VJ 0S4VJXoYp+ XoYp+A+ A+nArv nArvj3k j3kLy LyQAH QAH2p 2plqx lqxa6 a63+ 3+qxG8G 8G8GF 8GF58Se 58SeSpv Spvp3 p38s 8sgs gsRMc qwPy PyO9/ O9/KqG KqGT8M 8M8KJ KJkMxPVy PVyBy ByAPx APxTueq eq+S +S7Vh 7VhC4k 4kQ944 944+d +dTzhkgaz azd6 d6Mrs rsWEgP75 EgP75l4lw l4lwBt BtQuSgAp uSgApik ikoC5v 5vLNz0U 0Uj XHx31iU 31iU38Ev 38EvZMk8ET 8ETqW+b +b3p 3pWgy gyZpc pc++ ++Gdr drzJe3 e3m6g 6g+P +PY+ Y+8wwnU nUHHCZG3 G3QU QUhGq hGq6SN 6SNY1iU Y1iUxdwe9C e9Cax axhie hieps pslz lzpg ghpn pn7r 7rrhu rhuPnlYm PnlYmLa4o a4ovCXx XxfPT PT0KQ1t KQ1tHdN dNe6 e6nm nmjOM jOMjM jMm75 75i9eN i9eN/YAid7y YAid7yMS9C S9C3a 3a6w 6wpw pwPc Pcbru rugf gfBjv BjvZGlpZ GlpZiw iwo0 o07I 7IoZ oZS +HlA4L A4LPN1bU bUvYm YmCJAo Ao3W 3WLVb LVbxCY7V Y7VSM SMuD uDf2a 2a7J 7JQbQ QbQpR pR4w 4wOd OdEeP EePmyEgB EgBo6 o6z+qVp qVpGe0L e0LgXF gXFTXCda dakDqEvVN qEvVNQ MQ3elEc 3elEcfKdF KdFV1b V1bfNXt Xtf2aa aaF5g 5gtB0 B02P98 2P98UT7H8dlt 8dltCIHjV6J jV6JF+h +h7a 7atS5 S54VnQ 4VnQZm3+ 3+Nphe pheCohW ohWnH nHz/E4F E4FMN5D 5D56x 56xnlI nlIy G8B1I B1IiH iHp5t p5tPg7 Pg7dgi/ dgi/fGo GoV3dS3 V3dS3Ijf jfhYh hYh3q 3qvMBg Bgnz nzY7nYe Y7nYerf rfktuMOM OMzqD qDtzg1+ 1+w4HT1O3 O3uF uFGE7 GE7yLVM VM7x 7xIveFXU XU7Z 7ZLABf LABf5 inJEf EfmMaBg aBguPh4 uPh4cJd9 d9AlLOM AlLOM0x 0xfJ5D 5D9jCZTD58 58v5f 5fGdG GdGRsMs9T 9TFVM VMLu LuvKpE+ KpE+lQ0O lQ0Ojbw jbwFrXv rXvILM LMFlN lN2J 2JTs9KD 9KD+8 +8/9qv qv 9rKw KwUDL6o L6ouQ uQ7/ 7/tQB7y QB7ynYt nYtAgSpF AgSpFj1z j1zDlq lq4R 4Ret etwEw EwrKh rKhm61 61DpR pR8T 8TJGEdid GEdidEi5 i5lD lDOI OIc6JVw VwzQrYZ rYZjh jhEGw EGwqt qt24q 24qXeZ XeZYH YHo MgjT jTV1M V1MDjpm jpm2oj9w oj9wQa Qa6Xu 6Xu28G 28GtpZGw Gwoh ohM+z +zBr4 Br41F 1FcZS6BR S6BRYeW YeWyh2B2 2B2tWkmCSc ScyVw Vwy15f 15fRvqYrv YrvO3 O3BR BRrnk rnkVPp1d VPp1d HWLdv dvg0 g0Df9Z 9ZGqJ qJgBL gBLoH oHoC oCuM uMZKd Kd0e 0eBbI BbI=" />
Fallacy of Visual Productivity 12 Maintainability Factor 10 8 6 4 2 0 0 5 10 15 Drag’n’Drop Features
Model View Controller
Separation of Concerns…
You’d think we learned…
…one of many anti -patterns
Single Responsibility Principle
Write thy controllers so as tests shall not be needed
Controller Responsibilities Data conduit between View/Model
Things to avoid Everything that’s not data passing Avoid repetitive tasks Validations Checks
DRY up Controllers Aspect Orientated Programming Filters in MVC Attribute Filter Provider Global Filters
Filters OnActionExecuting OnActionExecuted Action OnResultExecuting OnResultExecuted
Unknown Record DEMO
Http Authentication DEMO
Tight Coupling
IoC DEMO
Startup Tasks - Cleaning up Global.asax DEMO
Startup Tasks - Containers DEMO
NuGet Create isolated infrastructure packages Pluggable and Discoverable via IoC MEF
The Model
Model Options Domain Model Dynamic Model View Model
Domain Model
How to perform Partial Updates?
Where to place Extra Data?
Dynamic Model
Dynamic Model
View Model
Benefits of View Model Clear Intentions Ability for Extra Data Ability for Partial Updates Scaffolding Support
Downside of View Model Extra Mapping Somewhat unnecessary Class explosion
When we map we DRY
AutoMapper DEMO
Conventions and Model Lookups DEMO
Overall: Convention / Compromise
Coping with Complex Sites
RenderAction – RenderPartial DEMO
RESTifying your application
ASP.NET MVC 4
The “API” Controller MVC for UI Offers “REST” extensibility out of the box Web API for API Controller Offers “REST” features out of the box
MVC 4 (not Web API) Via extensibility Content Negotiation Caching Routing Hypermedia (not in Web API either)
EasyMVC: Conventions for routing and more DEMO
Other things to watch out for Better Validation FluentValidation Script Organization Bundling in MVC 4 Async Controllers Controllerless Actions
Any framework / language can and will allow bad code
Thank you!
Recommend
More recommend