1. 首页
  2. 编程语言
  3. Web开发
  4. TestDrive ASP.NET MVC

TestDrive ASP.NET MVC

上传者: 2019-09-28 21:39:00上传 PDF文件 2.14MB 热度 35次
1 Getting Started with ASP.NET MVC 1.1 How ASP.NET MVC Works 1.2 Installing MVC 1.3 MVC in Five Minutes: Building Quote-O-Matic 2 Test-Driven Development 2.1 TDD Explained 2.2 Test-Driving “Hello World” II Building an Application 3 Getting Organized with McontentsAcknowledgmentsPreface10What Makes AsP. NET MvC Special?Why Test-Driven Development?02Who Should read This book?13What's in This Book?13What's new in ASP. NET MVc 2.0?14Online resources16I Fundamentals171 Getting Started with ASP. NET MVC181.1 How ASP NET MVC Works181. 2 Installing Mvc211.3 MVC in Five Minutes: Building Quote-O-Matic242 Test-Driven Development312.1 TDD Explained312.2 Test-Driving“ Hello world”36II Building an Application423 Getting Organized with Mvc433.1 Time Management with Getorganized433.2 Reading Data453.3 Creating a To-Do563.4 Deleting: Creating an Action Without a view623.5 Updating: Marking a To-Do as Complete66CONTENTS↓64 Working with Controllers714.1 Creating Topics724.2 Using the Form Collection and TempData Objects4.3 Adding a Little Color with jQuery804.4 Controllers talking to Controllers875 Managing State and Files with Controllers935.1 Enabling Filters and Results with Controllers935.2 Logging In1005.3 Testing Routes in MVC1095. 4 Storing Information in Memory1095.5 Manipulating Files1196 Enhancing Views with HTML Helpers and Master Pages 1276.1 Making Our Site Presentable with HTML Helpers .. 1286.2 Building a Custom HTML Helper1356.3 Simplifying Page Layouts with Master Pages1396.4 Adding Validations Using ModelStateDictionary1436.5 Replacing Web Controls with Advanced HTML Helpers 1467 Composing Views with Ajax and Partials1517.1 Working with Ajax1527.2 Finding It in a Snap with Autocomplete1577.3 Using Partials to Reduce Duplication161III Integrating with Other Frameworks1718 Persisting Your Models1728.1 MVCs Next Top Model: NHibernate1738.2 Using the Repository Pattern1748.3 Mapping with Fluent NHibernate1778.4 Creating and Reading Records1798.5 Editing Models1838.6 DeletingRecords1858.7 Additional ORM Data Relationships1869 Integrating Repositories with Controllers1889. 1 Fixing the NHibernate Session Inside MvC1899. 2 USing Inversion of Control with the IControllerFactory 1929.3 Injecting Repositories into Controllers1979.4 Creating a Custom Action Filter200this copy is(P1. 0 printing, June 2010)CONTENTS↓79.5 Linking NHibernate and MVC Validations2039.6 Preventing Performance Problems with Profiling20610 Building RESTful Web Services21010.1 Use soaP or Take a rest Instead?21010.2 Creating a Web Service21310.3 Publishing to Blogger220IV Security and Deployment22811 Security, Error Handling, and Logging22911.1 Applying Additional Security23011.2 Using an Action Filter to Handle errors23811.3 Using Logging to See What Went Wrong24111.4 Checking for a Pulse with ASP. NET Health Monitoring 24512 Build and Deployment24712.1 Automating Builds24712.2 Using MSBuild to Automate the Build24912.3 Deploying to Production259A Bibliography268Index270this copy is(P1. 0 printing, June 2010)acknowledgmentsJust like a movie, a book couldnt happen without the support of manyothers who don,t appear on the front coverId like to thank my publishers, Dave and andy, who not only providedthe opportunity for me to write this book but who also have publishedand written some of my favorite technical books. They also assignedme a talented and dedicated editor, Susannah Pfalzer. She's been myguide throughout this journey, and without her encouragement andconstructive feedback, the text would not be where it is today ThanksSusannah!Thanks to Clinton Begin and Mike Mason for providing role models ofhow a developer at heart can turn into an author. I'd like to thankthe crew of ThoughtWorks University XII-Sumeet Moghe, KrishnanNair, Deepthi Chandramouli, Michael Aguilar, Deepali Pawar, and rixtWiersma--who all helped me get started on writing this book. Alsothanks to all the men and women at Thought Works Canada who provide me with the opportunity every day to work alongside such passionate software professionalsI also had some in-depth reviewers who helped shape the code andtutorials of this book. These included David Cameron, my long-timefriend who also taught me how do debug Pascal back in the sixth gradeand worked through the code in this book line by line; Scott Muc,adeveloper whose tenacity helped give more form to Part Ill of the bookJohn Finlay, a programmer who reviewed this book while simultaneously explaining to me why the Hadron Collider will not cause Earthto be sucked into a massive black hole: Radu muresan the romanian who taught me English grammar; and Jennifer Smith, a fellowThoughtWorker whose detailed comments gave me a ton of ideas forimprovementACKNOWLEDGMENTs 9Several other reviewers also gave their feedback at different parts of thisproject. Id like to thank Puneet Goyal, Ted Neward, Siva Pinnaka, PaulReimer, Ravi Kumar Pasumarthy, Xingrui Pei, Jeff Cohen, Joe PoonEllen flookes and sharan Karantha huge thanks go to my family for supporting me through this endeavorTo my wife, Niki Rickhi, who cheered me on at every step. Niki, you arethe most amazing person I know. To my dad, Jock McCracken, whohimself became an author a few years ago and has always supportedme in following my own dreams. Id also like to say a special thanks toDK Sing, for all your guidance and wisdomMy final thanks is to you, the reader, who i hope enjoys the book asmuch as I enjoyed writing it. May it help you along your adventures inASP.NET MVC, TDD, and beyondJonathan McCracken, April 2010jon@lexicon.cathis copy is(P1. 0 printing, June 2010)if at first the idea is not absurd, then there is no hope for it.Albert einsteinPrefaceIt's testable. It's lightweight. It's open source. It's... Microsoft? YesASP. NET MVC is an open source web application framework created byMicrosoft to cater to the needs of agile software developers. Since itsofficial release in early 2009, it has been downloaded by almost l million developers, and it is rapidly being adopted by many organizationsbecause of its efficient development model. Simply put, it's C# on theWeb done rightWith this books test-driven approach to ASP.NET MVC, youll gain thecutting-edge skills to build your next web application and become amore agile developer in the processWhat Makes AsP. NET MVc special?Microsoft offers two web presentation frameworks: ASP. NET Web Formsand asP. net mvc. asp. net itself is the common set of libraries andfeatures that both AsP. NeT Web Forms and aSP. NET MvC work on topof. This supports customers'existing needs with the older ASP. NETWeb Forms and their future needs with ASP. NET MVC. AlthoughASP. NET MVC shares many of the same underpinnings of AsP. NET, itovercomes its brother's weaknesses. ASP. NET MVC was designed usingthe latest innovations and lessons learned on how to build web applications. This adds up to big productivity improvements for your teamsHere's what asP net mvc offers that asp. net Web forms doesntFull Control Over MarkupIf you' ve ever developed an asP. net Web Forms website, youll knowwhat a struggle it is to build a site for anything other than InternetExplorer. This is partly because ASP. NET Web Forms was designedfor intranet applications where a single browser could be more easilymandated. For most companies, supporting only one browser isnt anWHAT MAKES ASP NET MVC SPECIAL?<11option anymore. Many companies are focusing on enabling their partners and customers to perform their work through web applications, sothey need to support multiple browsersThe Achilles heel of AsP. NET Web Forms is its bloated HTML. It generates complex markup through a string of embedded web and usercontrols. ASP. NET MVC comes to the rescue with a much simpler solution. Its default view engine, which is confusingly named the web Formsview engine, gives you full control over your markup. No more strangeid tags with S and underscores in them This pays off when dealing withclient-side scripting such as JavaScript. You'll find out more about theWeb Forms view engine in Chapter 7, Composing Views with Ajax andPartials, on page 151Testabilitya web application framework that has out-of-the-box testing saves youa lot of time. Most developers building ASP. NET Web Forms applicationshad to use their own design patterns, such as Model-View-Presenter(MVP to accomplish this. For developers who dont know much aboutunit testing, it's less obvious how to approach testing. ASP. NET MVCsolves this with a clear way to test your code. I'll be focusing on thispoint heavily throughout the book to walk you through how to write awell-tested ASP.NET MVC applicationConvention Over ConfigurationFollowing convention saves time. ASP. NET MVCs timesaving conventions keep you out of configuration files, and some conventions giveyou added benefits, such as search engine optimization. For example, in ASP.NET MVC, URLs to your site become more readable byenginesInsteadofhttp://yourblog.com/blog/entry.aspx?id=108inAsp.NetWebFormsAsp.NetMvccandomuchbettersuchashttp://yourblogcom/Blog/Entry/108/MVC-Makes-Search-Engines-Happy. You can achievethe same thing with AsP. NET Web Forms, but it's less straightforwardWith ASP.NET MVC, you get it for free. You'll see more of these conventions throughout Part Il, Building an ApplicationExtensible architectureStriking a balance between conventions and extensibility is tricky forweb frameworks. If too many conventions are prescribed, they can1.http://weblogs.aspnet/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspxthis copy is(P1. 0 printing, June 2010)WHY TEST-DRIVEN DEVELOPMENT <12restrict you from extending the framework when you need to do soThe opposite is also true: if no conventions are set, then your team hasto continue to reinvent the wheelASP.NET MVC strikes a pretty good balance. It comes with a powerfudefault view engine but makes it easy to extend or create your ownYoull learn about this in Section 6.2, Building a Custom hTML Helper,on page 135. AsP. NET MvC has a feature called action filters that youcan extend to provide helpful features such as transaction supportYoull tackle this in Section 9. 4, Creating a Custom Action Filter, onpage 200. Because ASP. NET MVC's architecture has a single point ofcreation for all the controllers, you can extend it with dependency injection. dependency injection decouples object behaviors, or, more specif-ically, the implementation of those behaviors. We pass the behavior tothe constructor, effectively"injecting"it into the object. You'll see howto do this in Section 5. 1, IControllerFactory: Where Controllers are bornon page 98Finally, AsP. NET MvC isn't tied to any single persistence framework(see the Joe Asks... on page 19 for more on persistence frameworks)In fact. it doesnt come bundled with one at all. This leaves room foryou to choose the right tool for the job. In this book, you'll be usingNHibernate, one of the most popular open source persistence frame-works. You'll see how to use NHibernate in Chapter 8, Persisting YourModels, on page 172Why Test-Driven Development?Test-driven development (TDD) is a simple programming technique thatdrives your development by starting with a failing unit test. It's quicklybecoming a standard practice on projects because TDD helps you feelmore confident about your code. If you' ve never used TDD before, thenChapter 2, Test-Driven Development, on page 3l will show you howWith TDD, you'll spend much more time coding and much less timefiddling around with the debuggerThe other key advantage to this method is that it helps you learn aframework faster. Tests, when they pass, confirm that youve writtena bit of code correctly, and you can even dig into the tests that theframework offers. Because ASP.NET MVC is open source, you're free tobrowse all of its unit tests to help you gain an even better understanding of itthis copy is(P1. 0 printing, June 2010)
用户评论