6Tips of Separation - 1105 Media

62
PLUS Working with Windows Identity Foundation C#: Use Expression Trees in Your APIs Develop Web Apps with WebMatrix APRIL 2011 Volume 21, No. 4 VisualStudioMagazine.com 6 Tips of Separation Take ViewModel for a spin and unit test your Silverlight apps.

Transcript of 6Tips of Separation - 1105 Media

PLUS Working with Windows

Identity FoundationC#: Use Expression Trees

in Your APIsDevelop Web Apps

with WebMatrix

APRI

L 20

11

Volu

me

21, N

o. 4

VisualStudioMagazine.com

6 Tips of SeparationTake ViewModel for a spin and unit test your Silverlight apps.

Using Quince™, you and your team cancollaborate on the user interface usingwireframes, designs and examples.

Then use NetAdvantage® UI controls,like the map control used here, to bringthe application to life quickly & easily.

...

............................................................................................

.......................................................................

Untitled-7 2 11/10/10 10:59 AM

From start to finish, Infragistics gives you the tools to createimpressive user experiences that'll make end users happy!

SEE HOW WE USE THE TOOLS TO CREATE THIS KILLER APP AT INFRAGISTICS.COM/IMPRESS

Infragistics Sales 800 231 8588 • Infragistics Europe Sales +44 (0) 800 298 9055 • Infragistics India +91 80 4151 8042 • @infragistics

..............................................................................................................................

...

Untitled-7 3 11/10/10 10:59 AM

Untitled-2 2 3/8/11 10:27 AM

Untitled-2 3 3/8/11 10:27 AM

programmersparadise.com866-719-1528

Your best source for software development tools!

Prices subject to change. Not responsible for typographical errors.

®

programmers.com/vSphere

VMware vSphere Essentials Kit BundlevSphere Essentials provides an all-in-onesolution for small offices to virtualize threephysical servers for consolidating and managing applications to reduce hardwareand operating costs with a low up-frontinvestment. vSphere Essentials includes:

• VMware ESXi and VMware ESX (deployment-time choice)

• VMware vStorage VMFS• Four-way virtual SMP• VMware vCenter Server Agent• VMware vStorage APIs/VCB• VMware vCenter Update Manager• VMware vCenter Server for Essentials

for 3 hosts Paradise #

V55 85101C02

$446.99

programmers.com/sparxsystems

Enterprise ArchitectCorporate EditionVisualize, Document and Control Your Software Projectby Sparx SystemsEnterprise Architect is a comprehensive,integrated UML 2.1 modeling suite providing key benefits at each stage ofsystem development. Enterprise Architect7.5 supports UML, SysML, BPMN andother open standards to analyze, design,test and construct reliable, well under-stood systems. Additional plug-ins arealso available for Zachman Framework,MODAF, DoDAF and TOGAF, and to integrate with Eclipse and Visual Studio2005/2008.

1-4 Licenses Paradise #

SP6 03101A02$182.99

New Intel Visual Fortran Compilerby IntelIntel® Visual Fortran Composer XE2011 includes the latest generation of Intel®

Fortran compilers, Intel® Visual Fortran CompilerXE 12.0 for Windows. Intel® Fortran Composer XE is available for Linux and Mac OS X. Thispackage delivers advanced capabilities for development of application parallelism and winning performance for the full range of Intel®

processor-based platforms and other compatibleplatforms. It includes the compiler’s breadth ofadvanced optimization, multithreading, andprocessor support, as well as automatic proces-sor dispatch, vectorization, and loop unrolling.

for Windows Single (SSR)Paradise # I23 86101E03 $263.99

programmers.com/intel programmers.com/microsoft

Microsoft SQL ServerDeveloper Edition 2008 R2 by MicrosoftSQL Server 2008 Developer enables developers to build and test applications that run on SQL Server on 32-bit, ia64, andx64 platforms. SQL Server 2008 Developerincludes all of the functionality of EnterpriseEdition, but is licensed only for development,test, and demo use. The license for SQLServer 2008 Developer entitles one developerto use the software on as many systems as necessary. For rapid deployment into production, instances of SQL Server 2008Developer can easily be upgraded to SQLServer 2008 Enterprise without reinstallation.

2-bit/x64 IA64 DVDParadise #

M47 31101A04

$40.99

programmers.com/idm

UltraEdit The #1 Best Selling Text Editor in the World

by IDMUltraEdit is the world’s standard in texteditors. Millions use UltraEdit as the ideal text/hex/programmers editor on any platform — Windows, Mac, or Linux!

Features include syntax highlighting for nearly any programming language; powerful Find, Replace, Find in Files, and Replace in Files; FTP support, sort,column mode, hex, macros/scripting,large file handling (4+ GB), projects,templates, Unicode, and more.

Named User 1-24 Users Paradise #

I84 01201A01$59.95

NEWRELEASE!

Win an iPad!Place an Order for Software (or Hardware) with Programmer’s Paradise and You’ll be Entered for a Drawing to Win an iPad Wi-Fi 32GB.

Just Use the Offer Code TRWD04When You Place Your Order Online or with Your Programmer’s Paradise Representative.

programmers.com/textcontrol

Download a demo today.

Professional EditionParadise #

T79 12101A01$1,109.99

• .NET WinForms and WPF rich text box for VB.NET and C#

• ActiveX for VB6, Delphi, VBScript/HTML, ASP• File formats DOCX, DOC, RTF, HTML,

XML, TXT• PDF and PDF/A export, PDF text import• Tables, headers & footers, text frames,

bullets, structured numbered lists, multipleundo/redo, sections, merge fields, columns

• Ready-to-use toolbars and dialog boxes

TX Text Control 16.0Word Processing ComponentsTX Text Control is royalty-free, robust andpowerful word processing software in reusable component form. New

VersionReleased!

programmers.com/mindjet

Mindjet® MindManagerversion 9 for Windows®

Every Successful Project Starts with a Good Plan.by Mindjet®

Mindjet MindManager® is informationmapping software that gives businessprofessionals a better way to conquerinformation overload, brainstorm concepts, develop strategies, simplifyproject planning, and communicateresults. MindManager® maps provide an intuitive visual framework for planning successful projects.

1 User Paradise #

F15 17401A01

$293.98

programmers.com/embarcadero

Embarcadero RAD Studio XEby EmbarcaderoEmbarcadero® RAD Studio XE is a comprehen-sive application development suite and thefastest way to visually build GUI-intensive, data-driven applications for Windows, .NET, PHP and the Web. RAD Studio includes Delphi®,C++Builder®, Delphi Prism™, and RadPHP™. Thesuite provides powerful compiled, managed and dynamic language support, heterogeneousdatabase connectivity, rich visual componentframeworks and a vast third-party ecosystemthat enable you to deliver applications up to 5x faster across multiple Windows, Web, anddatabase platforms!

Paradise # CGI 15401A01 $1,383.99

NEWRELEASE!

ActiveReports 6by GrapeCity PowerTools

The de facto standard reporting tool for Microsoft Visual Studio.NET

• Fast and Flexible reporting engine• Flexible event-driven API to completely

control the rendering of reports• Wide range of Export and Preview formats

including Windows Forms Viewer, WebViewer, Adobe Flash and PDF

• XCopy deployment• Royalty-Free Licensing for Web and

Windows applications

Professional Ed.Paradise # D03 04301A01 $1,310.99

NEWVERSION

6!

programmers.com/grapecity

programmers.com/LEAD

LEADTOOLS Document Imaging Suite SDK v17.0by LEAD Technologies• Libraries for C/C++, .NET, Silverlight,

Windows Phone, WPF, WCF & WF• High Performance OCR, ICR, MICR & OMR• 1D & 2D Barcodes (Read/Write)• Forms Recognition/Processing• PDF, PDF/A and XPS• Document Cleanup• Advanced Compression (CCITT G3/G4,

JBIG2, MRC, ABIC, ABC)• High-Speed Scanning• Print Capture and Document Writers

Paradise # L05 03301A02 $4,018.99

programmers.com/tuneup

TuneUp Utilities™ 2011 by TuneUp DistributionThe latest version of TuneUp Utilities™ has been designed with the need of small-to-medium businesses as well as government and educational institutions in mind. TuneUp Utilities™ will allow you to reduce costly system downtime, extend system lifetime and allow your staff to work more efficiently.

With more than 30 intuitive tools, you will benefit from:• Increased performance and system response time• Solutions to a large number of PC problems• Greater system reliability• Optimal maintenance• Recovered disk space

Single UserParadise #

TU7 01201A01

$34.52

Untitled-4 1 3/2/11 4:11 PM

VisualStudioMagazine.com · April 2011 · VISUAL STUDIO MAGAZINE 5

14

April 2011 // Volume 21 // No. 4

Michael Desmond, Editor in Chief, Visual Studio Magazine

{ F R A M E W O R K S }

Expanding VisionA year ago this month Microsoft released Visual Studio 2010 and the Microsoft .NET Framework 4, the most signifi cant update to the company’s IDE and code framework since .NET was fi rst released in 2002.

Since the launch, we’ve seen still more additions. Microsoft has released a number of so-called Feature Packs that have extended support for unit testing, Team Foundation Server-based collaboration and virtual-load testing in the IDE. And last month, Microsoft released Visual Studio 2010 SP1, which includes a host of feature updates and fi xes ranging from integrated tooling for Silverlight 4 development to IntelliTrace support for SharePoint and 64-bit projects.

The expanding scope of Visual Studio 2010 is refl ected in our coverage. This month we begin rolling out several new columns. Practical .NET, authored by VSM Tools Editor Peter Vogel, provides actionable tutorials and guidance across a broad range of programming challenges, while Mobile Corner author Nick Randolph helps developers acclimate to the emerging world of Windows Phone development. In May, we’ll debut XAML Expert Mark Michaelis’ column aimed at Windows Presentation Foundation, Silverlight and other XAML-based development.

We’re also extending our reach online. While Patrick Steele will continue to write our C# Corner column in print, new arrival Eric Vogel (no relation to Peter) will be contributing online, writing twice-monthly Web columns under the C# Corner banner. Finally, it’s with regret that we bid farewell to longtime contributor Kathleen Dollard, who’s setting aside her Ask Kathleen column to focus on other endeavors. Kathleen, we miss you already.

What would you like to see covered in Visual Studio Magazine? Drop me a line at [email protected]. VSM

FEATURES14 6 Tips of Separation: Take ViewModel for a Spin and Unit Test Your Silverlight Apps

The Microsoft Model-View-ViewModel pattern is an indispensable tool for developing applications for Silverlight, Windows Phone 7 and Windows Presentation Foundation. Benjamin Day helps you avoid architectural pitfalls and create unit-testable and maintainable applications. BY BENJAMIN DAY

24 Improve Authentication with Windows Identity FoundationWindows Identity Foundation turns authentication over to token servers, reducing demands on developers while preparing the way for a service-oriented world. It also integrates with the authorization mechanisms you’re already using. BY PETER VOGEL

34 Give Your Classes a Software ContractWith the Microsoft .NET Framework 4, software contracts are available and even integrated with Visual Studio. Here are some of the benefits of a contract-first approach, such as code maintainability and ease of development. BY DINO ESPOSITO

40 LANGUAGE LAB PRACTICAL .NET PAGE 40 Why You Should Be Using LINQ

Peter Vogel introduces a new column on application development in the real world, and begins by advocating for Language Integrated Query. BY PETER VOGEL

C# CORNER PAGE 46 Using Expression Trees in Your APIs

How to translate C# code into expression trees to eliminate strings, standardize parameter validations and interact with other data structures. BY PATRICK STEELE

MOBILE CORNER PAGE 51 Intro to Windows Phone 7

New VSM columnist Nick Randolph shows how to build a YouTube search app using Visual Studio 2010 and Expression Blend. BY NICK RANDOLPH

DEPARTMENTS8 Letters to the Editor

9 DevDisastersThe Logic Behind Modern MaintenanceBY MARK BOWYTZ (THEDAILYWTF.COM)

12 DevInsight DEVELOPER NEWS, VSTOOLBOX, VSINSIDER

VSToolbox: Enable Power Users with WebMatrix PAGE 12

VSInsider: Peering Ahead to Windows 8 PAGE 13

COLUMNS5 Frameworks BY MICHAEL DESMOND

56 Redmond Review BY ANDREW J. BRUST

6 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

Get the complete picture —the latest dev news, analysis and how-to content—at VisualStudioMagazine.com and our partner sites in the Redmond Developer Network.

REDDEVNEWS.COM

Can Microsoft Build Appliances?BY ANDREW BRUSTAndrew Brust says Microsoft’s challenge has shifted from building hardware and software that makes things possible to building products and technologies that make things easy. Can Redmond shed its

“some assembly required” paradigm to give users solutions that just work, and what must the company do to achieve that end?

RedDevNews.com/Brust0411

DotNetNuke Leaves VB.NET for C#BY KATHLEEN RICHARDSThe next release of the open source DotNetNuke core content-management system framework will be moving to C#, after being written in Visual Basic .NET since its inception in 2002.

RedDevNews.com/Richards0411

Microsoft MVPs Discuss the Road AheadBY KURT MACKIEMicrosoft held its invitation-only 2011 MVP Global Summit on its Redmond campus last month. The annual gathering of developers, IT professionals and partners gives Microsoft MVPs and other technology insiders a chance to fi nd out what’s ahead, and discuss how the company shapes its software.

RedDevNews.com/Mackie0411

ADTMAG.COM

Driving Software QualityBY BOLA ROTIBIToo often organizations look for the answers in technology and tools, yet skip over the fact that the greatest impact on delivering quality software lies with people and processes, and professionalism backed with a reasonable level of experience.

ADTmag.com/Rotibi0411

Android Takes Smartphone Top SpotBY CHRIS PAOLIAccording to a comScore report released in March, the Android mobile OS has overtaken the Research In Motion BlackBerry OS to become the highest-selling smartphone OS in the United States. The report tracks overall smartphone sales over a three-month period ending in January.

ADTmag.com/Paoli0411

Democratizing the ESB MarketBY JOHN K. WATERSTalend is on a quest to “democratize the ESB market,” and it has launched a production-ready version of the open source Apache Camel project. The new Talend Integration Factory uses Camel patterns to make message-based system integration easier to implement and more scalable.

ADTmag.com/Waters0411

EASY FINDITWhat we once called FindIT codes are now easy URLs. You’ll see these embedded throughout Visual Studio Magazine so you can access any additional information quickly. Simply type in VisualStudioMagazine.com/ followed by the FindIT code into your URL address field. (Note that all URLs do not have any spaces, and they are not case-sensitive.)

Online Contents

ID Statement Visual Studio Magazine (ISSN 1537-002X) is published monthly by 1105 Media, Inc., 9201 Oakdale Avenue, Ste. 101, Chatsworth, CA 91311. Periodicals postage paid at Chatsworth, CA 91311-9998, and at additional mailing offi ces. Complimentary subscriptions are sent to qualifying subscribers. Annual subscription rates payable in U.S. funds for non-qualifi ed subscribers are: U.S. $35.00, International $60.00. Annual digital subscription rates payable in U.S. funds for non-qualifi ed subscribers are: U.S. $25.00, International $25.00. Subscription inquiries, back issue requests, and address changes: Mail to: Visual Studio Magazine, P.O. Box 2166, Skokie, IL 60076-7866, email [email protected] or call toll free (888) 768-8759, fax number 847-763-9564. Interna-tional calls 847-763-9135. POSTMASTER: Send address changes to Visual Studio Magazine, P.O. Box 2166, Skokie, IL 60076-7866. Canada Publications Mail Agreement No: 40612608. Return Undeliverable Canadian Addresses to Circulation Dept. or XPO Returns: P.O. Box 201, Richmond Hill, ON L4B 4R5, Canada.

Copyright Statement © Copyright 2011 by 1105 Media, Inc. All rights reserved. Printed in the U.S.A. Reproductions in whole or part prohibited except by written permission. Mail requests to “Permissions Editor,” c/o Visual Studio Magazine, 230 California St. Suite 302, San Francisco, CA 94111.

Legal Disclaimer The information in this magazine has not undergone any formal testing by 1105 Media, Inc. and is distributed without any warranty expressed or implied. Implementation or use of any information contained herein is the reader’s sole responsibility. While the information has been reviewed for accuracy, there is no guarantee that the same or similar results may be achieved in all environments. Technical inaccuracies may result from printing errors and/or new developments in the industry.

Corporate Address 1105 Media, 9201 Oakdale Ave. Ste 101, Chatsworth, CA 91311 www.1105media.com

Media Kits Direct your Media Kit requests to Matt Morollo, VP Publishing, 508-532-1418 (phone), 508-875-6622 (fax), [email protected]

Reprints For single article reprints (in minimum quantities of 250-500), e-prints, plaques and posters contact:PARS International Phone: 212-221-9595 E-mail: [email protected] www.magreprints.com/QuickQuote.asp

List Rental This publication’s subscriber list, as well as other lists from 1105 Media, Inc., is available for rental. For more information, please contact our list manager, Merit Direct. Phone: 914-368-1000; E-mail: [email protected]; Web: www.meritdirect.com.

VISUALSTUDIOMAGAZINE.COM

My Biggest Programming MistakesBY JOE KUNK, PATRICK STEELE AND PETER VOGELRead these fi rst-person tales of development gone wrong, from the authors of the On VB, C# Corner and Practical ASP.NET Visual Studio Magazine columns.

VisualStudioMagazine.com/Mistakes0411

Microsoft Releases Visual Studio 2010 SP1BY MICHAEL DESMONDVisual Studio 2010 SP1 adds a host of feature additions, fi xes and tweaks to the base IDE, including IntelliTrace support for 64-bit and SharePoint development projects and integrated tooling for Silverlight 4 projects.

VisualStudioMagazine.com/Desmond0411

TFS 2010 Reporting with Microsoft ExcelBY MICKEY GOUSSETTeam Foundation Server (TFS) 2010 uses SQL Server Reporting Services to provide reports, but because it uses data warehouse and analysis services to store reporting information, users can roll their own. Gousset examines how to use Microsoft Excel to access the TFS OLAP cube to create a report.

VisualStudioMagazine.com/Gousset0411

The MissionKit®, is an integrated suite of

UML, XML, and data integration tools for today’s

software architect.

Visualize

software works

of art with the

complete set of tools

from Altova®

The Altova MissionKit includes multiple tools

for software architects:

Download a 30 day free trial!

Try before you buy with a free, fully

functional, trial from www.altova.com

UModel® – UML tool for software modeling

Support for all UML diagram types,

SQL database diagrams, BPMN, SysML

Reverse engineering and code generation

in Java, C#, VB.NET

XMLSpy® – XML editor & development environment

Support for all XML-based technologies

Royalty-free Java, C#, C++ code generation

MapForce® – graphical data mapping tool

Mapping between XML, databases, EDI, flat files, XBRL,

Excel 2007+, Web services

Royalty-free Java, C#, C++ code generation

Plus up to five additional tools…

UML modeling

of SQL databases

Support for BPMN 2.0

Code generation from State

Machine diagrams

Cutting edge chart &

report creation

Direct file output for ETL projects

Enhancements for EDI

data mapping

And much more

of S

New inVersion

2011:

Untitled-4 1 3/2/11 4:09 PM

8 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

What You Want to SeeIn the March Frameworks column and a related Desmond File blog post, Editor in Chief Michael Desmond asked what readers of Visual Studio Magazine would like to see more of in the months to come. Here are some responses.

I’d like to know what features get removed in each Visual Studio version—a fi ne comparison of each version with not just additions, but removals as well. I don’t think the latest Visual Studio 2010 version constitutes progress at all because of its bloated Windows Presentation Foundation underpinnings.

Identity WithheldPosted online

Visual Studio 2010 has a lot of extensions and add-ins. It would be great to give tips on their purpose and how to use them.

Identity WithheldPosted online

It’s one thing to be able to write beautiful code. It’s another thing to properly confi gure Windows Server domains and security, .NET security, SQL Server, IIS, virtual machines, SharePoint and so on. Can we have articles that provide good instructions on how to confi gure everything properly?

Identity WithheldPosted online

.NET developers need to design interfaces similar to the movie “Minority Report.” A tour of the Kinect SDK would be great.

Identity WithheldPosted online

Redmond’s Open Source StrategyA reader responds to Mark Michaelis’ VSInsider column, “Copy and Paste: Redmond’s Open Source Strategy” (March 2011), commenting on why Microsoft delivers homegrown solutions for functionality that are already available via open source.

There’s one other reason that I can think of that Michaelis failed to mention: ownership of intellectual property. Microsoft could put itself into a serious liability situation if it were to include nominally “open source” code into the products it sells. By developing its own solutions, the company avoids this risk.

Bryan MorrisPortland, Ore.

Visual Studio Magazine wants to hear from you! Send us your thoughts about recent stories, technology updates or whatever’s on your mind. E-mail us at [email protected] and be sure to include your first and last name, city and state. Please note that letters may be edited for form, fit and style. They express the views of the individual authors, and do not necessarily reflect the views of the VSM editors or 1105 Media Inc.

Letters Check out our events at VSLive.comWe’ll be in Vegas, at Microsoft HQ and in Orlando in 2011

VisualStudioMagazine.comApril 2011 • Volume 21 • No. 4

Editorial Staff Vice President, Doug Barney Editorial Director Editor in Chief Michael Desmond Executive Editor Kathleen Richards Managing Editor Wendy Gonchar Associate Managing Editor Katrina Carrasco

Tools Editor Peter Vogel

Contributing EditorsAndrew J. Brust, Ken Cox, Kathleen Dollard,

Roger Jennings, Joe Kunk, Jeff Levinson, Bill McCarthy, Karl E. Peterson,

Patrick Steele, Bill Wagner

Art Staff Creative Director Scott Shultz Art Director Joshua Gould

Production Staff Director, Print Production Jenny Hernandez-Asandas Production Coordinator Serena Barnes

Online/Digital Media Director, Online Media Becky Nagel Executive Editor, New Media Michael Domingo Site Administrator Shane Lee Designer Rodrigo Muñoz

Advertising/Sales VP, Publishing Matt Morollo Regional Sales Manager Chris Kourtoglou National Accounts Director William Smith Microsoft Account Manager Danna Vedder

President Henry Allain Vice President, Publishing Matt Morollo Director of Marketing Michele Imgrund Online Marketing Director Tracy Cook

President & Neal Vitale Chief Executive Offi cer Senior Vice President Richard Vitale & Chief Financial Offi cer Executive Vice President Michael J. Valenti

Senior Vice President, Abraham M. Langer Audience Development & Digital Media Vice President, Christopher M. Coates Finance & Administration Vice President, Erik A. Lindgren Information Technology & Application Development Vice President, Carmel McDonagh Attendee Marketing Vice President, David F. Myers Event Operations

Chairman of the Board Jeffrey S. Klein

REACHING THE STAFFStaff may be reached via e-mail, telephone, fax, or mail. A list of editors and contact information is also available online at VisualStudioMagazine.com. E-mail: To e-mail any member of the staff, please use the following form: [email protected] Offi ce (weekdays, 9:00 a.m. - 5:00 p.m. PT)Telephone 949-265-1520; Fax 949-265-152816261 Laguna Canyon Road, Suite 130, Irvine, CA 92618Framingham Offi ce (weekdays, 9:00 a.m. - 5:00 p.m. ET)Telephone 508-875-6644; Fax 508-875-6633600 Worcester Road, Suite 204, Framingham, MA 01702Corporate Offi ce (weekdays, 8:30 a.m. - 5:30 p.m. PT)Telephone 818-814-5200; Fax 818-734-15229201 Oakdale Avenue, Suite 101, Chatsworth, CA 91311

Visual Studio MagazineThe opinions expressed within the articles and other contents herein do not necessarily express those of the publisher.

VisualStudioMagazine.com · April 2011 · VISUAL STUDIO MAGAZINE 9

</DevDisasters>

When the big merger was announced, the IT staff of both corporations was a little bit nervous, and with good reason: The day after the announcement, many redundant positions were eliminated. Miraculously, the IT staff on both sides was left almost untouched. With the integration of two disparate code bases, there was a lot of work ahead.

The Odd CoupleDuring this process, Doyle discovered that his development group inherited a widely used enterprise-level suite of Visual Basic 6 client-based apps from “the other side.” Doyle and his team were used to working in VB.NET, C# and ASP.NET; they felt a little taken aback at this news. But no matter, they thought—it was only Visual Basic.

So, after procuring enough copies of Visual Studio 6.0 from various online auctions for the entire offi ce, and applying 10-plus years worth of updates, they were ready to get their integration show on the road. They were just in time, because enhancements were rolling in by the truckload.

Doyle needed to add another country-code checkbox for Mexico to a user confi guration screen. It turned out that the program got data about what company information a user could view by reading in a registry entry (a three-bit fi eld):

If iChkBx And 100 Then chkUS.Value = 1 Else chkUS.Value = 0

If iChkBx And 10 Then chkCanada.Value = 1 Else chkCanada.Value = 0

If iChkBx And 1 Then chkOther.Value = 1 Else chkOther.Value = 0

'If not visible then uncheck in case it got checked above.

If chkUS.Visible = False Then chkUS.Value = 0

If chkCanada.Visible = False Then chkCanada.Value = 0

If chkOther.Visible = False Then chkOther.Value = 0

After some inspection, it was found that many UI input controls were controlled by this “bits in the registry” method.

To the developers who maintained the application before the merger, this was how they did things; but to those in Doyle’s department, this method—though functional—made them cringe. They knew they could do it better. After all, with all the enhance-ments coming, they would be allowed to make improvements along the way, right?

Wrong. “Hold on!” declared the project leads. Enhancements, like adding

a new checkbox, were completely valid. But changing existing base logic—no matter how fl awed—added risk to a process that, up until a few weeks prior, never needed to take into account that “other” might not suffi ce as a category for users residing in Mexico.

Hooray for Status QuoIn the end, the original caretakers of the app didn’t feel too bad about this decision. They could go ahead and keep up the programming habits that they’d honed during the past decade. Now, Doyle and his team spend their time adding and swapping bits in the registry, learning the “other” way of doing things, and biding their time until the merger train pulls out of the station. VSM

The Logic Behind Modern Maintenance

{ S O F T W A R E D E V E L O P M E N T G O N E W R O N G }

SERVED UP BY MARK BOWYTZ

Mark Bowytz is a contributor to the popular Web site The Daily WTF (thedailywtf.com). He has more than a decade of IT experience and is currently a systems analyst for PPG Industries. Have you experienced the darker side of development? E-mail your tale to Executive Editor Kathleen Richards at [email protected].

Tell Us Your Tale

Doyle discovered that his development group inherited a widely used enterprise-level suite of Visual Basic 6 client-based apps from “the other side.”

Untitled-9 2 2/15/11 1:10 PM

Untitled-9 3 2/15/11 1:11 PM

12 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

VSToolbox

BY PETER VOGELIf you belong to one of those organizations that keeps trying to stamp out software development by anyone but the IT department, then you don’t need WebMatrix. But if your organization has recognized that it’s impossible to stop power users from building applications for themselves and their departments, then WebMatrix has a role to play. A tool like WebMatrix can be useful to you by enabling others—for example, those power users—to build applications that would otherwise have to go into your backlog.

Many power-user applications eventually come under the control of a professional developer when the application hits some level of complexity. The tool must support migration to the organization’s “real” development platform. In addition, these applications must support some kind of robust deployment process that’s simple enough for power users to work with, but reliable enough to meet company standards. WebMatrix meets those goals.

When WebMatrix starts up, the user is presented with a Quick Start page that

provides four ways to initialize a site. The Web Gallery and Templates options allow the user to select from predefined sites. The Templates are surprisingly sparse (only five) but the Web Gallery has several dozen choices organized around various toolsets. The choice that’s most useful for the power-user scenario creates a site from a folder. WebMatrix can open and work with ASP.NET Web sites. By providing a few basic start points (for example, with the company’s Master Pages) you can control how users create their sites.

The WebMatrix interface bogs down a bit, primarily because it integrates both a site-monitoring tool and a site-development tool. When WebMatrix first opens, users can find themselves dealing with the site-monitoring choices rather than getting to the menus that support their real goal: building a new site. Once users get up to speed with the interface, they’ll find switching between tasks fast and easy.

Once past the initial UI, users have a product that lets them develop with the same building blocks that Visual Studio provides:

ASPX pages. The deployment process uses the new Microsoft Web Deployment package, so if you’ve migrated to Visual Studio 2010, both you and your power users will be sending your Web master the same packages. Using the included version of IIS Express, WebMatrix applications can be run on departmental servers.

The question is whether your power users can deal with the complexity of creating ASP.NET sites. Developers can use HTML pages, but they can also add ASPX pages, Master Pages and code files. This isn’t a tool with a lot of wizards that generate code for users—they’re going to have to write it themselves. By default, WebMatrix will encourage users to embed code inside their ASPX pages, but it also supports the code-behind model. The key point is that a site created in WebMatrix can be readily migrated to Visual Studio.

Installing WebMatrix is a breeze and could be reasonably left to a power user. The download not only installs WebMatrix, but also includes SQL Server Compact 4, the Web Deployment package and the new IIS Express 7.5.

Can the power users in your organization actually use WebMatrix? That’s a tougher question to answer. Some training will be required, but if properly employed, WebMatrix gives power users the tools to create Web sites that your development organization can live with. VSM

Peter Vogel ([email protected]) is the tools editor for Visual Studio Magazine and a principal in PH&V Information Services, specializing in ASP.NET development.

Mic

roso

ft

Web

Mat

rix

</DevInsight>

The UI for WebMatrix looks like Office across the top and Outlook down the left-hand side. Pieces of Visual Studio, like the Solution and Server Explorers, appear in those left-hand panels.

Microsoft WebMatrix

MicrosoftWeb: Microsoft.comPhone: 1-800-642-7676Price: FreeQuick Facts: A tool that enables power users to create, customize and deploy Web sitesPros: A simplified UI that incorporates Web development, deployment and site monitoringCons: No out-of-the-box support for generating pre-defined apps; users are expected to write their own HTML, ASPX tags and code

Enable Power Users with WebMatrixWebMatrix may not have a place in your toolbox, but it probably does have a place in your organization—and it’s one you should foster.

0411vsm_GrapeCity_Insert.indd 1 3/3/11 1:33 PM

0411vsm_GrapeCity_Insert.indd 2 3/3/11 1:34 PM

VisualStudioMagazine.com · April 2011 · VISUAL STUDIO MAGAZINE 13

By now, you may have heard rumblings of Microsoft’s next big bet, Windows 8. The follow-up OS to the widely successful Windows 7 currently has far more rumors surrounding it than sound, factual data, but we know enough to glean that Microsoft is actively targeting Windows 8 at developers. Redmond’s intentions appear to skew toward mobile developers, what with Windows 8 on ARM being demonstrated at the Consumer Electronics Show (CES) 2011 and a Windows application store being noted in public planning documentation. But what about non-mobile developers? What can you expect in the next version of Windows?

Microsoft hasn’t committed to a public delivery schedule, but Mary Jo Foley, who writes the Foley on Microsoft column for VSM sister publication Redmond maga-zine, wrote on her All About Microsoft blog that Windows 8 could enter public beta in September and ship by mid-2012.

It’s clear that Microsoft is thinking about professional developers with Windows 8. The aforementioned OS documentation includes a Venn diagram that details the developer markets Microsoft aims to address. It displays the approximate size of three developer segments: hobbyist/non-professional at 104 million developers, STEM-D (science, technology, engineering and math developers) at 39 million, and

professional at 8.6 million. The diagram shows Windows 8 embracing the entire professional market, versus only a fraction of the hobbyist and STEM-D sectors.

Public postings from Microsoft employees are revealing. One employee notes on his LinkedIn profile that he’s working on an “app-development framework for Windows 8.” Another writes that his team is “chartered with reinvigo-rating the Windows developer ecosystem by building substantial improvements into the Windows platform (APIs, tools and the underlying infrastructure).” That same employee writes that additional details are confidential, but the commitment to developer issues is apparent.

The Big PictureThere are changes to the OS that could have pervasive impact. The new UI, for one, may drastically change the user experience for those who opt to employ it. With Microsoft reportedly keen on adding touch- and speech-based UI enhance-ments, desktops, notebooks and netbooks will likely acquire new hardware such as touch-capable screens, accelerometers and other sensors. Windows 8 may be an important steppingstone for making touch, speech and other inputs a core part of the computing experience. Should your applications follow suit?

Windows 8 is also poised to introduce changes in how users get help and support. What does that have to do with you? Well, don’t be surprised if Microsoft makes help and support in Windows 8 more like a platform containing an API for devs to integrate with their applications. That may be a far-reaching conclusion, but I wouldn’t rule out the possibility—especially with the Device Stage and Device Experience Development Kit that Microsoft created for developers with Windows 7.

Anyone who’s seen the Microsoft “to the cloud” TV campaign won’t be surprised to find that Windows 8 is pushing cloud-

based thinking to new extremes. The concept of roaming profiles could make Windows more user-centric and less machine-dependent, allowing an individual to access settings, preferences and more on any Windows-based computer simply by logging into a roaming profile.

Now take that concept and apply it to an application you’ve developed. What would this do for user licensing, where a user is often restricted to one machine install per license? On the other hand, this could create the ability for a user to seamlessly migrate settings for your application from one machine to another.

Finally, Windows 8 will likely rally around Windows Presentation Foundation (WPF) and Silverlight, as Microsoft utilizes the XAML platforms to develop Windows 8-based components. We don’t have a lot of specifics yet, but it appears that WPF and Silverlight are about to become much more front-facing than they’ve been up to this point.

There will undoubtedly be marked improvement in Windows 8 compared to previous iterations of Windows, where developers are concerned. To what degree, though, is still a big question. The crux of having insight into early planning is that features and targets sometimes get changed or canceled along the way. However, I’ve heard and seen enough to feel 100 percent confident that the Windows 8 team will unveil an impressive update of the flagship Microsoft OS—perhaps even the greatest one to date, especially for developers. VSM

Stephen Chapman is an investigative blogger who writes for ZDNet and authors the Microsoft enthusiast blog Microsoft Kitchen (msftkitchen.com). He was recently named by Redmond magazine as one of the top 10 influential Microsoft pundits. His coverage of emerging Microsoft technologies affords him a unique perspective and sound insight into the future of products such as Windows, Office, Windows Phone and more.

</DevInsight>

VSInsider

Win

dow

s 8

Peering Ahead to Windows 8

BY STEPHEN CHAPMAN

VSM COVER STORY

After working on a Silverlight 4 project for the last 18 months, I’ve learned a few things about layer loyalty, code organization, unit testing and maintainable code. Not surprisingly, I’ve formed some opinions on how to implement the Microsoft Model-View-ViewModel (MVVM) pattern, and used the Repository and Adapter patterns to achieve better results. It sounds complicated but it doesn’t have to be, especially with these six tips to help you avoid common architectural pitfalls and get the results you’re after: separation of concerns, testability and easier maintenance as your application evolves.

First off, what is MVVM? It’s a code organization and testability pattern commonly used in Silverlight, Windows Phone 7 and Windows

Presentation Foundation (WPF) applications that helps you to bridge the gap between your UI code and your “business” tier or Domain Model code. The Views are your *.xaml files, the Models are your Domain Model objects and the ViewModels are logical representa-tions of your Views minus the UI-specific stuff. Your ViewModel classes won’t have any UI-specific types like TextBox or Label but will instead use types like string, int and DateTime. If you have a View named PersonDetail.xaml that allows the user to view and edit information about a person, you’ll have a ViewModel class named PersonViewModel. If you’ve got a TextBox on your View that dis-plays a Person’s FirstName, you’ll have a property on the ViewModel named FirstName that’s a string. If you have a Save button on your View, you’ll have a property on your ViewModel of type ICommand named SaveCommand. The Views and the ViewModels are connected together via databinding expressions on the UI controls.

Like its sister pattern, Model-View-Controller (MVC), MVVM helps you to unit test your application. It does this by minimizing the amount of code that lives in your codebehind (*.xaml.cs,

*.xaml.vb) files. Why is minimizing your codebehind so crucial for unit testing? One of the key problems with unit testing is figuring out how to test a running instance of the UI. It’s a challenge because unit test frameworks like NUnit and MSTest are geared toward testing ordinary classes. Sure, a XAML page is a class, but it comes along with a lot of other Windows-related stuff that’s notoriously difficult to automate.

You might be thinking, don’t Microsoft Test Manager (MTM) and Coded UI tests in Visual Studio 2010 fix this? Well, yes and no. These tools make it much easier to automate testing the UI of a running application, but it’s a different kind of test. MTM and Coded UI tests are generally aimed at integration testing of a deployed application. There’s a difference in mindset between “integration”

The Goals of Microsoft Model-View-ViewModel■ Bridge the gap between your UI implementation (*.xaml) and

your Domain Model (or “Business”) layer■ Minimize the amount of code that goes in codebehind

(*.xaml.cs, *.xaml.vb) files ■ Help you to organize your application code so it can be unit tested■ Help you to organize your application code so it can be

maintained separately from the UI —B.D.

The Microsoft Model-View-ViewModel pattern is an indispensable tool for developing applications for Silverlight, Windows Phone 7 and Windows Presentation Foundation. Benjamin Day helps you avoid architectural pitfalls and create unit-testable and maintainable applications. BY BENJAMIN DAY

Take ViewModel for a Spin and Unit Test Your Silverlight Apps

14 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

VSM COVER STORY

testing and “unit” testing. Integration tests test the complete func-tionality of a deployed application while unit tests test small pieces of functionality in isolation. Integration tests are “macro” and unit tests are “micro.” In a way, using MVVM and unit tests solves the UI testing problem by not testing the UI. The ViewModels are an abstraction representing the state and logic behind the UI, so you minimize the amount of stuff that’s difficult to test and focus on what’s easy to test—the non-Windows ViewModel classes that you write in C# or Visual Basic .NET.

Tip 1: Separate the Model from the ViewModel, no matter what.There’s a tendency to want to lump your Model and your View-Model into a single object. Let’s take the case of a screen that edits information about a person: Id, FirstName, LastName, EmailAddress. If you don’t have a heck of a lot of business logic or complex valida-tion logic, having a PersonViewModel and a Person model class is going to feel a little like overkill or code duplication. These two classes are definitely going to be pretty similar. One of the great things about Silverlight/WPF and MVVM is being able to use data binding. You don’t have to write 62 zillion assignment state-ments to get data from your ViewModel on to your View, and the codebehind for your View stays extremely clean. If you use two-way data binding, your View-Model gets refreshed from your View as soon as the user clicks off of a field.

I learned the importance of strictly separating the Model from the ViewModel when I needed to implement a Cancel button.

Let’s say you’re editing an existing Person record and you’re using two-way data binding. You change the first name, you change the last name, you change the e-mail address and then you realize that you’re completely wrong and you want to undo your changes. At this point, the ViewModel is already updated because of the two-way data binding and has the same values that are displayed on the View.

If you “cheaped out” and combined the Model and ViewModel, you don’t have anything to roll back to unless you reload the Person record from the database. But, if you have a separate ViewModel and Model, you simply don’t migrate the changes to the Model. Instead you refresh the ViewModel from the Model and your cancel functionality is complete.

This demonstrates a key point about layer loyalty. The Views and ViewModel are all about displaying stuff to the user in a par-ticular UI implementation. The Model cares nothing about how the data is displayed and is more concerned with business rules and persistence logic.

The Views and ViewModels are for displaying and collecting data from the user, but when the actual Save happens, the data

VisualStudioMagazine.com · April 2011 · VISUAL STUDIO MAGAZINE 15

16 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

ViewModel and Unit Testing in SilverlightCOVER STORY

from the ViewModel gets copied to the Model and it’s the Model that gets saved.

For the simple example of a Person editor, there’s a 1-to-1 mapping between the View, the ViewModel and the Model. When your Views become more complex, this mapping changes. There’s almost always a 1-to-1 mapping between the View and ViewModel, but when the Views (and therefore the ViewModels) get complex, the ViewModel might need to talk to two or more Models to populate and save the data on that View. This is the second case where

“cheaping out” on the separation between View, ViewModel and Model can get you into trouble.

Tip 2: The classes from “Add Service Reference” are not your Models or your ViewModels.If you’re writing a Silverlight business application, you’re probably using Windows Communication Foundation (WCF) to save and retrieve data. Silverlight can’t directly connect to a database, so you really don’t have any other options than to call up to some kind of Web service, which will connect to the database on your behalf. This means that you’re probably going to be using Add Service Reference to generate proxy classes to help your Silverlight application talk to those services. If you’re writing a WPF client to a service-oriented application, you’re probably using Add Service Reference as well.

When these proxy classes are generated, they implement INotify-PropertyChanged, and that means that they can be used for two-way data binding. However, just because they can be used doesn’t mean you should use them. It’s another example of “layer loyalty.” These classes are basically data-access classes, and they’re loyal to that service and to the WCF implementation. How you save and load data isn’t really related to a View—it’s related to the persistence implementation. Plus, if you think about it, WCF isn’t really about objects anyway; it’s about messages, so what you get back from WCF almost definitely isn’t shaped the way you want.

Sure, it can be convenient to use these proxy objects as your ViewModels, but it isn’t going to do you any favors when it comes to maintenance because all your layers are going to be tightly cou-pled. Your Views are tied directly to the operations and messages for the service. If the service contract changes, you have to change your View, and if your View changes you have to change your service (yuck).

Unless your application is really simple, these proxy objects aren’t good candidates to be your Models, either. The proxy objects are simply data-transfer objects for talking to the service. The messages that go back and forth between your Silverlight client application, and the service application should be optimized for those operations. They might have more or less data than you need for your model or, more importantly, aren’t going to be the same shape as what you need. The messages might even be shaped a certain way in order to make them serialize more compactly.

Remember, your Models are Domain Model objects. They should be object-oriented (not message-oriented) and should represent how your client application thinks about the data it manages. If you use the service proxy objects as your Models, you’re probably going to have some funky object designs. If you use the service proxy objects as your ViewModels, you not only skip having Models but also create a direct dependency between your XAML-based Views and your services. That’s not a good recipe for code maintenance.

Tip 3: Separate your ViewModel and Model from your data-access logic with the Repository Pattern.Your ViewModels represent the state of the UI and they should be experts in doing exactly that. They might have to make calls to get data saved and loaded, but they shouldn’t be experts in how that’s actually achieved. That’s where the Repository pattern comes in.

The Repository is commonly thought of as a way to wrap reads and writes to a database on behalf of Domain Model objects—but it doesn’t have to be a database. The pattern is actually focused more on hiding the details of saving and retrieving Model data to/from a persistent store than on any particular kind of persistent store.

What this means for Silverlight (or any other client to a service-oriented application), is that you can and probably should encapsulate the logic for calling the services inside of a Repository pattern object. Additionally, once you encapsulate that logic inside of the Repository pattern, you can create an interface for that Repository and code against that in your application—IPersonRepository instead of WcfPersonRepository.

Creating that Repository interface and coding against the interface type rather than the concrete type makes a big difference in how your unit tests work. Remember the goal when creating unit tests is to test small units of functionality. If you’re testing how the PersonView-Model handles the data it gets back from calls to IPersonRepository, you don’t really need to call the WcfPersonRepository. You can create a fake version of IPersonRepository that returns canned data. Now you’re testing only the logic in PersonViewModel rather than the logic for PersonViewModel, the WcfPersonRepository and the WCF service.

It can be convenient to use proxy objects as your ViewModels,

but it isn’t going to do you any favors when it comes to maintenance

because all your layers are going to be tightly coupled.

Listing 1. Asynchronous calls to a WCF service.

public void LoadById(int id){ // Create an instance of the service proxy var client = new PersonService.PersonServiceClient(); // Subscribe to the "completed" event for the service method client.LoadByIdCompleted += new EventHandler<PersonService.LoadByIdCompletedEventArgs>( client_LoadByIdCompleted); // Call the service method client.LoadByIdAsync(id);}

void client_LoadByIdCompleted(object sender, PersonService.LoadByIdCompletedEventArgs e){ // This method is called after the call to LoadByIdAsync() is finished PersonService.PersonDtopersonReturnedByService = e.Result; // ...}

Untitled-4 1 3/2/11 4:10 PM

18 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

ViewModel and Unit Testing in SilverlightCOVER STORY

This gives you a much more focused test. It makes your test and application code maintainable because your application logic is clearly separated from the data-access logic, which makes the application easier to debug.

Tip 4: Use the Adapter pattern between ViewModel and Model, and Model and Service Data Transfer Objects.As stated previously, the ViewModel is going to collect data from your UI so that you can populate and eventually save your Models. With Silverlight, you’re going to need to take the data from your Models and turn them into service proxy objects that you’ll send through WCF. You also have to populate your ViewModels from Models and populate your Models from WCF service proxy objects. That’s a fair number of gets and sets.

Rather than scattering that code throughout your classes in a bunch of different places, try using the Adapter pattern. The basic idea of the Adapter pattern is to take two different object structures and create a third object (the Adapter) to make them work together. In our case, the adapter is going to be responsible for the fairly tedious

job of taking data from one type of object and putting it into another type of object—ViewModel to/from Model and Model to/from WCF service project objects.

Having these gets and sets in your Adapter classes not only keeps your code organized but makes it easy to unit test the adaptation logic. I’m continually surprised by how error- prone this logic tends to be and, because of this, I think it’s one of the most important places to unit test in an application.

Tip 5: In Silverlight, fi gure out early on how you’re going to handle asynchronous methods.If you’re like me and you like to keep your code well-layered and organized, you’re even-

tually going to need to do battle with the asynchronous WCF methods of Silverlight. In Silverlight, all networking traffic must be asynchronous. (Sounds harmless enough so far, right?) So where you might have a WCF service method named LoadPersonById, when you try to call it from Silverlight, you’ll be calling LoadPerson-ByIdAsync and dealing with asynchronous callbacks. There are ways to force Silverlight to call WCF synchronously, but they’re tricky, error prone and probably not worth doing.

If you don’t embrace this asynchronous requirement early it can wreck your beautifully organized architecture and ViewModels. It will beat you up, laugh at you and run away with your lunch money in a matter of seconds if you aren’t careful. The reason why comes down to a simple little problem: Methods that call WCF can’t have any return values. Put another way, any method that calls an asyn-chronous method must return “void.” You’re probably thinking there’s no possible chance that that’s true—but, I assure you, it is.

In Listing 1 (p. 16) you can see some Silverlight code for calling the LoadById method for a WCF service named PersonService. Notice that you need to subscribe to the LoadByIdCompleted event followed by a call to LoadByIdAsync. LoadByIdAsync has the return type of void and when you call it, it returns immediately and you don’t get access to the return value of the WCF service method until the LoadByIdCompleted event fires.

Single-Responsibility Principle and Unit TestingRobert C. Martin authored a set of principles for object-oriented design called the SOLID Principles. One of these principles is the Single Responsibility Principle (SRP), and it states that an object should have a single reason to change.

The SRP is key to why the Microsoft Model-View-ViewModel pattern is broken out in the way that it is—Model, View and View-Model all have distinct roles. The SRP is also why I like to separate the logic that calls my WCF services (the Repository code) from the logic that populates the service data-transfer objects and handles any return values from my services (the Adapter code).

Separating the Repository and Adapter logic has a big effect on my unit tests. I typically don’t make any WCF service calls in my Silverlight unit tests. I test all the logic right up to the moment before I’d make a Windows Communication Foundation (WCF) call and all the logic that happens after the WCF call returns—but I never actually make that WCF call. I do this for three reasons: I think of my services as a completely separate application that’s tested independently; making that call says

“integration test” rather than “unit test”; and I want to minimize the dependencies and required setup for running my unit tests. I don’t want to require that my back-end services are deployed in order to unit test my application—that’s an integration test.

When you apply the SRP to your data-access logic and separate it into Repositories and Adapters, you start to see that the majority of what’s going on is really happening in the Adapters. There’s lots of code for converting between the service data-transfer objects and the models and virtually nothing related to making calls to WCF. This means you can focus your testing on the Adapter code, skip the WCF call and still test the important code while getting great code coverage. —B.D.

Listing 2. LoadByIdCompleted event handler.

void client_LoadByIdCompleted(object sender,PersonService.LoadByIdCompletedEventArgs e){ var callback = e.UserStateasReturnResult<IPerson>; if (e.Error != null) { // Pass the WCF exception to the original caller callback.Notify(e.Error); } else { PersonService.PersonDtoperson ReturnedByService = e.Result; var returnValue = new Person(); var adapter = new PersonModelToServiceDtoAdapter(); adapter.Adapt(personReturnedByService, returnValue); // Pass the populated model to the original caller callback.Notify(returnValue); } }

Figure 1. ReturnResult<T> serves as a bridge between the method that requests the WCF logic and the method that handles the results of the WCF call.

DESIGNDesign Applications That Help Run the Business

Our xamMap™ control in Silverlight andWPF lets you map out any geospatialdata like this airplane seating app tomanage your business. Come toinfragistics.com to try it today!

Infragistics Sales 800 231 8588 Infragistics Europe Sales +44 (0) 800 298 9055 Infragistics India +91 80 4151 8042

@infragistics

Untitled-6 1 11/10/10 11:41 AM

20 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

ViewModel and Unit Testing in SilverlightCOVER STORY

Let that sink in for a bit. There’s no connection between the call that requests the WCF call and logic that handles the response. So, if that LoadById method was part of a Repository called WcfPerson-Repository, that disconnect means that you can’t return a populated Person Model object from that method because you don’t have the data. Here’s where it gets even trickier—anything that calls anything that at any point calls an async WCF method is also not going to be able to return anything other than void.

Whew! That sure deflates your nicely layered architecture, doesn’t it?

At this point, you can either decide that all Silverlight applica-tions have to be single-tiered mounds of spaghetti code with everything jammed into a handful of unmaintainable ViewModel classes, or you need to figure out a way to actively embrace the asynchronousness.

My solution to this is a class called ReturnResult<T> (see Figure 1, p. 18). (Note: This can also be solved in a different way by using the Reactive Extensions for .NET, available at tinyurl.com/ydya2gy.) ReturnResult<T> serves as a bridge between the method that requests the WCF logic and the method that handles the results of the WCF call. It allows the WCF “complete” handler to return values or exceptions to the original caller while still being asyn-chronous.

In Listing 1, I’d prefer that the method signature was something like public IPerson-Load ById(int id), but it contains an asynchronous WCF call and therefore can’t return any useful value. By using ReturnResult<T>, you can achieve some-thing similar to returning an IPerson by introducing a method argument of type ReturnResult<IPerson>, as shown here:

public void LoadById(ReturnResult<IPerson> callback, int id)

{

// Create an instance of the service proxy

var client = new PersonService.PersonServiceClient();

// Subscribe to the "completed" event for the service method

client.LoadByIdCompleted +=

new EventHandler<PersonService.LoadByIdCompletedEventArgs>(

client_LoadByIdCompleted);

// Call the service method

client.LoadByIdAsync(id, callback);

}

In the LoadById method, when you’re ready to call LoadById-Async on the service proxy, instead of just passing the Id that you want to load, you’ll also pass the ReturnResult<IPerson> callback variable as the user state. All async calls provide a method overload that takes a variable of type object named userState. This userState parameter lets you pass data in to the async call that will later be available during the completed event via the CompleteEventArgs.

Listing 2 (p. 18) shows the code for the LoadByIdCompleted event handler. Notice that it retrieves the ReturnResult<IPerson> callback variable by accessing e.UserState. This event handler method now has a way to communicate back to the original caller so that it can pass back either a populated instance of IPerson or an Exception by calling the Notify method.

In Listing 3, you can see some sample code from a ViewModel class that makes a call to the Repository LoadById(ReturnResult<IPerson>, int) method. When the ViewModel makes the call, it creates an instance of ReturnResult<IPerson> and passes in a delegate to a method in the ViewModel that will handle the asynchronous response from the Repository call. In this case, it’s a method named LoadCompleted, which is where the ViewModel consumes the IPerson data that has been retrieved.

As you can see, by using the Repository pattern, all the details of how the IPerson was loaded and from where it was loaded are abstracted away into another class. All that the ViewModel needs to worry about is handling any errors or data that get passed to it via the ReturnResult<IPerson> callback.

A convenient side effect of using the Repository is that the WCF service proxy data-transfer objects can be completely

encapsulated within the Repository class itself—no other class has to or should ref-erence those classes generated by Add Service Reference. Without a solution to the asynchronous call problem, the ViewModel would have a lot more knowledge of how the IPerson data is retrieved, which would be a violation of the separation of concerns.

Tip 6: Simplify your bindings and your validation code with ViewModelField<T> and Custom Controls.

The basic idea of the Adapter pattern is to take two diff erent

object structures and create a third object (the Adapter) to make

them work together.

Listing 3. ViewModel code to call LoadById on a repository.

private void Load(){ m_Repository.LoadById( new ReturnResult<IPerson>(LoadCompleted), IdToLoad.Value);}

private void LoadCompleted(ReturnResult<IPerson> callback){ if (callback.Error != null) { ShowMessage(callback.Error); } else { var adapter = new PersonModelToViewModelAdapter(); adapter.Adapt(callback.Result, this); m_Model = callback.Result; }}

Figure 2. ViewModelField<T> encapsulates the value of the field plus the metadata.

DEVELOPRich Business Intelligence Applications in WPF and Silverlight

Infragistics Sales 800 231 8588 Infragistics Europe Sales +44 (0) 800 298 9055 Infragistics India +91 80 4151 8042

@infragistics

Robust Pivot Grids for WPF andSilverlight let your users analyze datato make key business decisions.Visit infragistics.com to try it today!

Untitled-6 1 11/10/10 10:57 AM

22 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

ViewModel and Unit Testing in SilverlightCOVER STORY

When you’re coding a business application in Silverlight and WPF, you’ll almost definitely be writing validation code and code to show/hide controls based on choices made in the UI or user security permissions. For example, if you’re writing a Person Editor window that has an EmailAddress property, you’re going to need a way to validate that e-mail address and notify the user if it’s invalid. You might also have the case where Administrators can view all details about a person while non-Administrators can see everything except for the person’s salary. In this case, your ViewModel would be checking the type of user and setting the visibility of the Salary field.

What this means is that, for nearly every field on your page, your ViewModel is going to have the following:

1. A property for the value you want to display/edit.2. A Boolean property that says if that value is valid. 3. A string property with the error message if the value is invalid.4. A Boolean property that indicates whether that field should

be visible. So, what started out as a small number of fields on your relatively

simple ViewModel has now exploded by a factor of four because of metadata about the field.

You can cut down on this metadata clutter by creating a generic object to represent the field, for example ViewModelField<T>, shown in Figure 2 (p. 20). The exploded version of PersonViewModel

would need four separate properties—Salary, IsSalaryVisible, IsSalaryValid and SalaryVali-dationMessage—to represent Salary, while the version that used ViewModelField<int> needs only one (see Figure 3). When you think about what this means for the whole PersonView-Model, one way has 14 properties (most of which are similar) and the other has only five.

When you’re implementing your Views, lots of ViewModel properties means lots of Data-Context data bindings to write, maintain and debug. Creating a common design pattern for your common ViewModel logic allows you to start simplifying your data bindings by cre-ating custom controls that understand how to efficiently talk to your ViewModelField<T> properties. Here’s the XAML that would be required to display and bind the Salary field without ViewModelField<T>:

<Grid Visibility="{BindingIsSalaryVisible ...}}">

<Grid.RowDefinitions>

<RowDefinition />

</Grid.RowDefinitions>

<StackPanel Orientation="Vertical">

<TextBlock Text="SALARY" />

<TextBlock Text="{BindingSalaryValidationMessage}"

Visibility="{BindingIsValid ...}}" Foreground="Red" />

<TextBox Text="{Binding Salary, Mode=TwoWay}" />

</StackPanel>

</Grid>

It’s fairly complex for one field, and this technique starts to become seriously ugly when you bind all of PersonViewModel—and it’s the same stuff over and over again. Using ViewModelField<T> and a custom textbox control, you can not only encapsulate and reuse the layout so that your View has a consistent look, but you can also simplify the XAML in the page that contains the control. Plus, there’s only a single binding, as shown in Listing 4.

Using ViewModelField<T> plus custom controls can greatly simplify the code for your ViewModels and Views and eliminates lots of duplicated or nearly identical code from your application. Plus, once you’ve created and tested your custom controls, writing the rest of your application starts to go a lot faster because you can add new fields to your Views and ViewModels without having to write a lot of code.

The MVVM pattern is an indispensable tool for developing applications for Silverlight, Windows Phone 7 and WPF. If you avoid some of the architectural pitfalls and use MVVM in conjunc-tion with other design patterns like Repository and Adapter, you can easily write unit-testable and maintainable applications. VSM

Benjamin Day is a consultant, trainer and speaker based in Brookline, Mass., specializing in Team Foundation Server, Scrum, Silverlight and Windows Azure. He’s a Microsoft MVP for Visual Studio Application Lifecycle Maintenance (ALM) and can be contacted via benday.com or his blog, blog.benday.com.

GO ONLINEA sample application that demonstrates the concepts in this article, including source code, is available for download from VisualStudioMagazine.com/Day0411.

Listing 4. The XAML and data binding for PersonViewModel with ViewModelField<T>.

<StackPanel> <controls:TextboxField DataContext="{Binding Id}" LabelText="Salary"/> <controls:TextboxField DataContext="{BindingFirstName}" LabelText="First Name"/> <controls:TextboxField DataContext="{BindingLastName}" LabelText="Last Name"/> <controls:TextboxField DataContext="{BindingEmailAddress}" LabelText="Email Address"/> <controls:TextboxField DataContext="{Binding Salary}" LabelText="Salary"/></StackPanel>

Figure 3. The exploded version of PersonViewModel needs four separate properties to represent Salary, while ViewModelField<int> only needs one.

EXPERIENCEBeautiful Data Visualizations That Bring Your Data to Life

Use our Motion Framework™ to see your dataover time and give your users new insightinto their data. Visit infragistics.com/motionto try it today!

Copyright 1996-2010 Infragistics, Inc. All rights reserved. Infragistics, the Infragistics logo and NetAdvantage are registered trademarks of Infragistics, Inc.Motion Framework is a trademark of Infragistics, Inc.

Infragistics Sales 800 231 8588 Infragistics Europe Sales +44 (0) 800 298 9055 Infragistics India +91 80 4151 8042

@infragistics

Untitled-1 1 1/11/11 1:39 PM

24 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

ILLU

STR

ATI

ON

BY

RYA

N E

TTER

Windows Identity Foundation turns authentication over to token servers, reducing demands on developers while preparing the way for a service-oriented world. It also integrates with the authorization mechanisms you’re already using. BY PETER VOGEL

Improve Authentication withWindows Identity Foundation

FEATURE

Creating secure applications is hard. And, arguably, the most critical part of managing security is authentication—ensuring that only approved users can access your application or site and that you can identify users once they’ve been given access. The second part of security—authorization and controlling access to your application’s resources—is really only possible if you know whom the current user is.

In authentication, some domain (a server, a Web farm, an applica-tion) establishes a set of identities: a name with associated information. The authentication process consists of a user adopting one of those identities (by specifying an identity’s name) and providing some credentials (such as a password) that only those users allowed to adopt that identity will have. Managing all the identities needed for all applications, setting the right permissions for those identities, and managing/checking those credentials are just the basic issues in authentication. In a service-oriented world, to quote Ethan Hunt in the first “Mission Impossible” movie: “Relax, Luther—it’s much worse than you think.”

In a service-oriented architecture (SOA) world, applications are created by stitching together applications and services that are often in different domains. As a request passes from the user to the application and services, developers need to deal with federated security: authentication that involves multiple organizations.

The first solution for authentication issues is usually to make the user’s life more difficult. Users are bedeviled by having to remember the names of the identities they’re supposed to adopt across all the

domains/applications they access—and then the users have to keep track of the credentials associated with each identity.

In a SOA world, if a user accesses an application that accesses other services (potentially, in other domains) how is authentication to be handled? One solution is to pass the credentials that the user provides to the original application onto the service. This can work within an organization, but if the service is provided by an external organization, the required coordination of identities and credentials is difficult to manage. An alternative is for each of the services involved to assign an identity and set of credentials to the application. Effectively, the application (rather than the user) authenticates with the service—the service trusts the application and all those the application authenticates.

Authentication also makes developers’ lives complicated. And, because authentication is difficult to do right, there’s a real possibility that developers will do it wrong. No matter which solution you choose, if a malicious user can fool the authentication process in the application they’re accessing, that malicious user gains access to the services the application accesses.

The WIF SolutionWindows Identity Foundation (WIF) provides solutions to the issues in authentication at all levels of application development. It’s a shame that the word “federated” crops up so much in discussing WIF—it suggests that WIF is for handling complicated, multiservice security issues. In fact, WIF simplifies security even for basic Web applications. WIF incorporates Active Directory Federation Services (part of Windows Server 2003 R2), Windows Cardspace and Windows Azure Access Control Services … but you don’t care. WIF handles encrypting and decrypting certificates, extracting user attributes, validation and the rest of the plumbing. It just works.

Untitled-11 1 3/9/11 2:47 PM

26 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

FEATURE Windows Identity Foundation

One of the key features of WIF is that it externalizes authentication, passing it over to security token services (STS) and reducing the demands on individual developers (authorization and controlling access to resources remain the responsibility of the application). WIF also supports trust relationships where a service trusts anyone authenticated by the application accessing it. The technology supports federated systems that involve multiple organizations (vendors, customers, partners) by supporting trees of STS leaves. In addition, WIF provides support for a single sign-on (SSO) for users by sharing STS among applications. Developers can also, at run time, support both by creating a trust relationship between the application and service or by passing the user’s credentials from the application to the service.

In addition to all of this, WIF also supports authorization by allowing developers to drive app behavior from user attributes. Those attributes are delivered to the application so that there’s no need to keep looking up information in some security store (a performance benefit). You can embed this authorization into your application code to put it where you need it or centralize/externalize authorization so that it’s independent of any developer’s work. WIF provides all this and backward-compatibility, too: WIF, in many cases, integrates smoothly with the authorization tools you’re already using.

The core of the WIF solution is the STS. An application contacts an STS (Active Directory, for instance), and the STS handles authen-tication and returns to the application a token that represents the user and the user’s attributes. These attributes are the claims made by the user and can include the user’s name, group affiliations or even permissions—for example, whether the user has read-only access or can also perform updates. The STS providing the token is referred to as the issuer, while the application receiving the token is called the relying party. I won’t discuss it here, but the relying party can determine the name of a token’s issuer and use that to decide just how much reliance to place on any token.

So, let’s get practical. In this article, I’m going to concentrate on what a developer (specifically, an ASP.NET developer) would do to exploit WIF. I’ll be concentrating on using WIF in an application rather than, for instance, creating a custom STS.

One note: In this article, I’ll refer to authenticating a user; however, the same techniques would be appli-cable to a service that was authenticating an access by a consumer (either an application or another service).

Confi guring for WIFTo try out WIF, start Visual Studio as an administrator and open or create the project you want to work with (pick the empty ASP.NET template to highlight the changes that WIF makes). Press F5 to run your appli-cation once and copy its URL from the address bar of the Web browser.

The first step in integrating WIF into your appli-cation is to add a reference to an STS. As with any other reference, you add this by right-clicking on your project and selecting the Add STS reference menu choice. This starts a wizard that gives you three choices for your STS: You can skip connecting to an STS (which still allows you to work with claims), create a new STS or use an existing STS.

In real life, you’d probably pick an existing STS. However, for development purposes (or just to play with STS), pick the second option, which gives you a

new ASP.NET project that will act as your STS. The wizard also adds a certificate to the IIS certificate store, which is why you need to run Visual Studio as an administrator. The wizard will generate the name for your STS site, but you’ll need to paste in the URL for your site. It would also be a good idea to set the Specific port option in Project Properties to ensure that the port number in your URL doesn’t change.

If all goes well, you should get a message that the Federation Utility site has completed successfully. If you get a message that it didn’t complete, the most likely cause is that you’re not running Visual Studio with administrator privileges.

Running the wizard adds a number of elements to your web.config file and, more importantly, comments out any existing <authentication> tags. Instead, your authentication tag is set to the ominous:

<authentication mode="None" />

While this looks like you’ve turned off authentication, you’ve really just turned off any native authentication built into the frame-work you’re using. Now all authentication will be handled by WIF. Your authorization elements will also be set to prevent access to your site:

<authorization>

<deny users="?" />

</authorization>

Your application will have a folder called Federation MetaData, containing an XML file. That folder’s security is set to allow all users to access it:

<location path="FederationMetadata">

<system.web>

<authorization>

<allow users="*" />

</authorization>

</system.web>

</location>

Also added to your application’s config file are references to the identityModel DLL, two new HttpHandlers/modules and an entry in appSettings. The entry in appSettings points to the XML file in your new FederationMetaData folder:

To use an existing token service (like Active Directory), you need to select the third option in the Visual Studio wizard and provide some configuration information.

Untitled-11 1 3/9/11 2:48 PM

28 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

FEATURE Windows Identity Foundation

<appSettings>

<add key="FederationMetadataLocation"

value="c:\inetpub\wwwroot\MySite\

FederationMetadata\2007-06\FederationMetadata.xml" />

</appSettings>

Confi guring the identityModelWhat matters most is a new configuration section called micro-soft.identityModel. It’s within the identityModel that you’ll find the issuerNameRegistry element, which specifies which STS you’ll use and which certificate issuers you’ll trust. The wsFederation ele-ment points to the STS you selected in the wizard (and references your site in the realm attribute):

<federatedAuthentication>

<wsFederation passiveRedirectEnabled="true"

issuer="http://localhost/Sample_STS/"

realm="http://localhost/MySite/" requireHttps="false" />

<cookieHandler requireSsl="false" />

</federatedAuthentication>

The trustedIssuers element contains reference to which certificates you’ll trust. The wizard will add the thumbprint for the STS that you selected:

<issuerNameRegistry

type="Microsoft.IdentityModel.Tokens.

ConfigurationBasedIssuerNameRegistry, Microsoft.IdentityModel,

Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">

<trustedIssuers>

<add thumbprint="40A1D2622BFBDAC80A38858AD8001E094547369B"

name="CN=IdentityTKStsCert"/>

</trustedIssuers>

</issuerNameRegistry>

After running the wizard, you’ve done everything you need to enable authentication in your application—no additional code is required. When a page is requested, the new WIF HttpHandlers will note the absence of any tokens from the STS and call the STS specified in the wsFederation element. The STS will do whatever is necessary to authenticate the user and return control (and a token, if the user is authenticated) to the application. Upon receipt of the token, the application will consider the user authenticated.

You have some administration tasks to perform around setting up your users in your STS, but, as an application developer handling authentication, you’re done.

Advertising and Checking ClaimsAuthorization (controlling access to resources) still requires some coding, however. The token, in addition to indicating that the user has been authenticated, also contains information about the user—the user’s claims. Your application “advertises” what claims it needs to support its authorization process (this prevents the STS from sending every claim related to the user).

You specify the claims you want in the web.config file inside the claimTypeRequired element, which is part of the identityModel. This example asks for three claims (name, role and postal code):

<applicationService>

<claimTypeRequired>

<claimType optional="true"

type="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name" />

<claimType optional="true"

type="http://schemas.microsoft.com/ws/2008/06/identity/claims/role" />

<claimType optional="true"

type="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/postalCode" />

</claimTypeRequired>

There are a large number of pre-defined claims (including gender and address information, among others). If you were creating a custom STS, you could extend the system to advertise for other claims. If you select the option in the wizard that allows you to connect to an existing STS, the wizard displays the list of the claims provided by the STS you’re connecting to.

In the application, it’s your responsibility to check the claims pro-vided by the STS and determine what access you want to provide to the user. The good news here is that, in many cases, claims infor-mation is returned through existing components of your object model. For instance, in my config file, I requested the user’s name. In my code, I can retrieve the name provided by the STS through the User object already available in ASP.NET:

Me.txtName.Text = User.Identity.Name

As another example, if you want to lock users out of particular folders using role-based security, you add the same authorization element that you used with ASP.NET forms-based security. This example, for instance, allows users in the Administrator role into the Admin folder and only users in the Manager and Administrator roles into the root folder:

<configuration>

<location path="Admin">

<system.web>

<authorization>

<allow roles="Administrator"/>

</authorization>

</system.web>

</location>

<system.web>

<system.web>

<authorization>

<allow roles="Manager, Administrator"/>

<deny users="?" />

</authorization>

...

However, for cases where no integration with an existing autho-rization framework is provided, you’ll need to examine the claims

Getting Started with WIFWindows Identity Foundation (WIF) works on Windows Vista SP2, Windows 7, Windows Server 2008 (either R2 or SP2) and Windows Server 2003. You must have IIS 7 (or later) and the Microsoft .NET Framework 3.5 (or later). For this article, I used Windows 7. You’ll need to download the runtime and the SDK from the Microsoft Download Center. Make sure you pick the right downloads—there are separate versions of the runtime for Windows 7 and Windows Vista (with separate versions for 32-bit and 64-bit computers), one for Windows Server 2003, and one for the other supported versions (again, in 32-bit and 64-bit versions).

The simplest way to make sure you have everything you need is to download the Identity Training Kit. Installing the Dependency Checker that comes with the kit and then double-clicking on the .dep files for any of the labs in the Training Kit will prompt you through the downloads you need, and it will also configure IIS correctly. —P.V.

Untitled-11 1 3/9/11 2:48 PM

30 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

FEATURE Windows Identity Foundation

yourself using the WIF object model. To access the WIF objects, you’ll need to add references to both the Microsoft.IdentityModel and System.IdentityModel libraries. To save yourself some typing, also add this Imports statement to your code file:

Imports Microsoft.IdentityModel.Claims

The next step is to access the IClaimsPrincipal interface of the object returned by the current thread’s CurrentPrincipal property. Once you have that, you can retrieve the first ClaimsIdentity in its Identities collection:

Dim prin As IClaimsPrincipal

prin = CType(Thread.CurrentPrincipal, IClaimsPrincipal)

Dim id As IClaimsIdentity

id = prin.Identities(0)

The IClaimsIdentity object has a collection of Claims that hold the values for the claims that you advertised for in your config file and that were available from the STS. Rather than loop through all the claims returned by the STS, this code uses a LINQ statement to retrieve the value for the postal code claim advertised for in the config file (or returns null if the claim isn’t there):

Dim pCode =

(From c In cId.Claims

Where c.ClaimType =

System.IdentityModel.Claims.ClaimTypes.postalCode

Select c.Value).FirstOrDefault()

I can now run tests on the postal code to control access to func-tionality on the page:

If pCode.Contains("N7A") = False Then

Me.Request.Redirect("NoAccessAllowed.htm")

End If

The values for claims are always strings, so complex objects used in claims will need to be serialized.

Externalizing AuthorizationInserting code to check claims throughout your application isn’t a best practice: it’s only a matter of time until the check is omitted somewhere where it’s needed. You can centralize the authoriza-tion by creating a class that checks claims for you, and then by telling your site to invoke it automatically with entries in your web.config file.

The first step is to add the ClaimsAuthorizationModule to both the existing httpModules and modules sections in your web.config file:

<httpModules>

<add name="ClaimsAuthorizationModule"

type="Microsoft.IdentityModel.Web.ClaimsAuthorizationModule,

Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral,

PublicKeyToken=31bf3856ad364e35" />

...

</httpModules>

...

<modules>

<add name="ClaimsAuthorizationModule"

type="Microsoft.IdentityModel.Web.ClaimsAuthorizationModule,

Microsoft.IdentityModel, Version=3.5.0.0, Culture=neutral,

PublicKeyToken=31bf3856ad364e35"

preCondition="managedHandler" />

...

</modules>

With those modules in your site’s processing pipeline, you can add a reference to the class that will process your claims. That requires an entry in the service element of the identityModel.

Within its service element you add a claimsAuthorizationManager element with a type attribute that points to the claims class you’ll write. Within that element you can add any custom tags or elements that you want to hold information; you’ll use that information in conjunction with the user’s claims as part of authorizing access to resources.

This example show a custom element I’ve called claimData that specifies a folder and a Canadian postal code to be used in processing requests (you can ignore any warning messages you get about the content model being empty):

<microsoft.identityModel>

<service>

<claimsAuthorizationManager type="MySite.PHVCheckClaims">

<claimData folder="/Admin/"

pCode="N7A" />

</claimsAuthorizationManager>

This flexibility allows you to support configuring any part of your authorization process through entries in your application’s config file. For applications that will be installed on multiple servers, this provides a convenient way for site administrators to tailor the authorization process to meet their needs.

You’re now ready to create your claims-checking class. Add a Class file to your App_Code folder with the same name as you used in the config file. This file must inherit from the Claims-AuthorizationManager:

Public Class PHVCheckClaims

Inherits ClaimsAuthorizationManager

End Class

Within this class, you’ll want to retrieve any values you set in your custom element inside the claimsAuthorizationManager element. To support that, your custom element is passed to your class’ con-structor as a nodelist. This example retrieves the attribute values from the claimData element I used:

Imports System

Imports System.Linq

Imports System.Xml

Imports Microsoft.IdentityModel.Claims

Public Class PHVCheckClaims

Dim clmPCode As String

Dim folder As String

Public Sub New(ByVal cfg As Object)

Dim nd As XmlNode

nd = CType(cfg, XmlNodeList)(0)

folder = nd.Attributes("folder").Value

clmPCode = nd.Attributes("pCode").Value

End Sub

Finally, you’ll need to override the GetAccess method. This method is passed by an AuthorizationContext object that provides access to information about the current request (including the resource being requested, the action and the user’s identity).

The following code retrieves the page name from the context object’s Resource property. The page being requested and the user’s postal code are matched against the values retrieved from my claimData element. If I’m not happy with the result, I return false and prevent the access. Otherwise, I return true to let the user have the page:

Untitled-11 1 3/9/11 2:48 PM

32 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

FEATURE Windows Identity Foundation

Public Overrides Function CheckAccess(

ByVal context As AuthorizationContext)

As Boolean

Dim requestPage As Uri

requestPage = New Uri(context.Resource.First().Value)

Dim pCode = (From c In context.Principal.Identities(0).Claims

Where c.ClaimType = System.IdentityModel.Claims.

ClaimTypes.PostalCode).First().Value

If requestPage.PathAndQuery.Contains(folder) Then

Return clmLocation.Contains(pCode)

End If

Return True

End Function

I can now remove the authorization elements in the config file that controlled access to the Admin pages.

Dealing with a SOA WorldBy default, an ASP.NET application accesses all other resources (data-bases, Web services) with a fixed identity. This allows the application (rather than the individual users) to be granted permission to access other resources as a trusted application. The problem with this strategy is that the resource loses visibility of the user’s identity behind the ASP.NET application and can’t grant—or refuse—access based on the user’s permissions.

Some applications turn on impersonation, which allows the ASP.NET application to pass on the user’s identity and gain the user’s permissions (though usually resulting in poorer performance). How-ever, this used to be an either/or choice, typically made at design time.

With WIF, your ASP.NET application can send the user’s identity along with the application’s predefined identity to the service—at least when working with Web services. The application is config-ured to not use impersonation. You then write some additional code to retrieve a token that represents the user and include that token in your call to the service. WIF leverages the Web services standard WS-Trust ActAs mechanisms to incorporate the user’s identity into calls to the service’s methods (because of that, this technique will only work with services that support WS-Trust).

To implement this, you first need to modify the binding elements in your ASP.NET config file, which are generated when you add a refer-ence to a Web service. These changes configure the service to use the ActAs mechanisms. In the issuer element within the bindings element, set the binding and bindingConfiguration elements to turn on ActAs:

<issuer address=http://OtherHost/ActAsSTS/Issue.svc

binding="ws2007HttpBinding"

bindingConfiguration="ActAsBinding"/>

Still within the service’s bindings element, configure the ws2007-HttpBinding settings like this:

<ws2007HttpBinding>

<binding name="ActAsBinding">

<security mode="Message">

<message establishSecurityContext="false" />

</security>

</binding>

</ws2007HttpBinding>

The last change in the config file is in the identityModel element. You must add the saveBootstrapTokens attribute to the inputModel’s

service tag. That attribute causes the STS to return a “delegated” token that represents the user, in addition to any other tokens returned by the STS. The attribute also causes the delegated token to be saved for later use:

<microsoft.identityModel>

<service saveBootstrapTokens="true">

To send the delegated token to the service, you must add code that retrieves the delegated token and then uses the service’s channel factory to send the token. You can save yourself some typing by adding imports for System.Security.Cryptography.X509Certificates and Micro-soft.IdentityModel.Protocols.WSTrust.ChannelFactoryOperations.

Your first step is to retrieve the delegated token from the first Identity on the current thread, using the BootstrapToken property:

Dim tkn As System.IdentityModel.Tokens.SecurityToken =

CType(Thread.CurrentPrincipal, IClaimsPrincipal).

Identities(0).BootstrapToken

The next step is to retrieve and configure the channel factory for the service (I’ve assumed that the service implements an interface called ISalary):

Dim cfact As System.ServiceModel.ChannelFactory(

Of ISalaryServiceChannel)

cfact = New System.ServiceModel.ChannelFactory(

Of ISalaryServiceChannel)

("CustomBinding_ISalaryService")

cfact.Credentials.ServiceCertificate.SetDefaultCertificate(

"CN=localhost",

StoreLocation.LocalMachine,

StoreName.My)

cfact.ConfigureChannelFactory()

The final step is to configure the factory to use the delegated token:Dim chnl As ISalaryServiceChannel

chnl = cfact.CreateChannelActingAs(tkn)

Finally, you can call methods on the service and catch the result:Dim res As Decimal

res = chnl.GetPayment()

chnl.Close()

Wrapping UpThe story that you should take away from this exploration is that the key issue in WIF is establishing your STS—and, if you’re using Active Directory, much of that is taken care of for you. Actually using the STS can be as simple as running the wizard to connect to an existing STS. The security features you’re already using will often simply leverage the STS tokens.

When you want to go beyond declarative authorization or extend authorization beyond just the user name or role to handle additional claims, all you have to do is advertise for the claims you want. Within your application, you place your authorization code where you want in your application’s code or externalize it (within your application) to ensure that it’s handled correctly everywhere. When accessing Web services you can act as a trusted application or impersonate the user.

One last thing: If you’re having trouble debugging your application, you can drag the SecurityTokenVisualizer control that comes with the WIF package onto your Web Forms page. This control displays infor-mation about the tokens received from your STS—just make sure you remove it before putting your application into production! VSM

Peter Vogel ([email protected]) is the tools editor for Visual Studio Magazine and a principal in PH&V Information Services, specializing in ASP.NET development.

Untitled-11 1 3/9/11 2:49 PM

34 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

Design by Contract (DbC) is a methodology introduced several years ago by Bertrand Meyer based on the idea that each piece of software has a contract in which it formally describes what it expects and what it provides. The If-Then-Throw pattern nearly covers the first part of the contract; it lacks entirely the second part. DbC isn’t natively sup-ported in any mainstream programming language. However, frameworks exist to let you taste flavors of DbC in commonly used languages such as Java, Perl, Ruby, JavaScript and, of course, the Microsoft .NET Framework languages. In .NET, you do DbC via the Code Contracts library added to the .NET Framework 4, located in the mscorlib assembly. Note that the library is available to Silverlight 4 applications but not to Windows Phone applications.

I believe that nearly every developer would agree in principle that a contract-first approach to development is a great thing. But I don’t think so many are actively using Code Contracts in .NET 4 applications, even though Microsoft has integrated software contracts into Visual Studio. This article focuses on the benefits of a contract-first approach for code maintainability and ease of development.

Reasoning About a Simple Calculator ClassCode Contracts are useful for just about any type of application, as long as you have a good grasp of them. So let’s start with a simple class—a classic Calculator class such as this:

public class Calculator

{

public Int32 Sum(Int32 x, Int32 y)

{

return x + y;

}

public Int32 Divide(Int32 x, Int32 y)

{

return x / y;

}

}

This code lacks at least one important piece: a check to see if you’re attempting to divide by zero. As we write a better version of it, let’s also assume that we have an additional issue to deal with: The calculator doesn’t support negative values. Listing 1 has an updated version of the code that adds a few If-Then-Throw statements.

So far, we can state that our class either starts processing its input data or, in the case of invalid input, it just throws before doing anything. What about the results generated by the class? What facts do we know about them? Looking at the specifications, we should expect that both methods return a value not less than zero. How can we enforce that and fail if it doesn’t happen? We need a third version of the code, as shown in Listing 2 (p. 36).

Both methods now are articulated in three distinct phases: check of the input values, performance of the operation and check of the output. Checks on input and output serve two different purposes. Input checks flag bugs in your caller’s code. Output checks look for bugs in your own code. Do you really need checks on the output? I admit that check conditions can be verified via assertions in some unit tests. In that case, you don’t strictly need such checks buried in the runtime code. However, having checks in the code makes the class self-describing and makes it clear what it can and can’t do—much like the terms of a contracted service.

Content provided by MSDN Magazine, Microsoft’s premier publication for developers.

Listing 1. The If-Then-Throw pattern.

public class Calculator{ public Int32 Sum(Int32 x, Int32 y) { // Check input values if (x <0 || y <0) throw new ArgumentException();

// Perform the operation return x + y; }

public Int32 Divide(Int32 x, Int32 y) { // Check input values if (x < 0 || y < 0) throw new ArgumentException(); if (y == 0) throw new ArgumentException();

// Perform the operation return x / y; }}

Give Your Classes a Software ContractWith the Microsoft .NET Framework 4, software contracts are available and even integrated with Visual Studio. Here are some of the benefi ts of a contract-fi rst approach, such as code maintainability and ease of development. BY DINO ESPOSITO

Untitled-11 1 3/9/11 2:49 PM

36 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

FEATURE Contract-First Approach

You'll notice that our code grew by quite a few lines—and this is a simple class with few requirements to meet. Let’s take it one step further.

In Listing 2, the three steps we identified (check input, operation and check output) run sequentially. What if the performance of the operation is complex enough to accommodate additional exit points? What if some of these exit points refer to error situations where other results are expected? Things can really get complicated. To illustrate the point, however, it suffices that we add a shortcut exit to one of the methods, as shown in Listing 3.

In the sample code (and it is just an example), method Sum attempts a shortcut if the two values are equal—multiplying instead of summing. The code used to check output values, however, must be replicated for each early exit path in the code.

The bottom line is a contract-first approach demands some serious tooling, or at least a specific helper framework. Checking preliminary conditions is relatively easy and cheap to do; dealing manually with post-execution conditions makes the entire code base unwieldy and error prone. Other issues can complicate matters such as checking conditions when input parameters are collections and ensuring that the class is always in a known valid state whenever a method or a property is called.

Enter Code ContractsCode Contracts support three types of contracts: preconditions, post-conditions and invariants. Preconditions indicate the preliminary conditions that should be verified for a method to execute safely. Post-conditions express the conditions that should be verified once the method has executed either correctly or because of a thrown exception.

Finally, an invariant describes a condition that’s always true during the lifetime of any class instance. It indicates a condition that must hold after every possible interaction between the class and a client—that is, after executing public members, including constructors. The conditions expressed as invariants aren’t checked and subsequently may be temporarily violated after the invocation of a private member.

The Code Contracts API consists of a list of static methods defined on the class Contract. You use the Requires method to express pre-conditions and Ensures to express postconditions. Listing 4 (p. 38) shows how to rewrite the Calculator class using Code Contracts.

Compare Listing 3 and Listing 4. Method code is back to a highly readable form in which you distinguish only two levels: contract information including both preconditions and postconditions and actual behavior. You don’t have to mix conditions with behavior, as in Listing 3. Code is easier to read and maintain. For example, you can quickly and safely add a new precondition or edit postconditions; you intervene in just one place and your changes can be clearly tracked.

Contract information is expressed via plain C# or Visual Basic code. Contract instructions aren’t like classic declarative attributes, but they still maintain a strong declarative flavor. Using plain code instead of attributes increases the programming power of developers, as it makes it more natural to express the conditions you have in mind. At the same time, using Code Contracts gives you more guidance when you refactor the code. Code Contracts, in fact, indicate the behavior you should expect from the method. They help maintain coding discipline when you write methods and help keep your code readable even when preconditions and post conditions get numerous. Even though you can express contracts using a high-level syntax such as that in Listing 4, when code actually gets compiled, the resulting flow can’t be much different from the code outlined in Listing 3. Where’s the trick, then?

An additional tool integrated in the build process of Visual Studio—the Code Contracts rewriter—does the trick of reshaping the code, understanding the intended purpose of expressed preconditions and postconditions and expanding them into proper code blocks placed where they logically belong.

Listing 2. Checking preconditions and postconditions.

public class Calculator{ public Int32 Sum(Int32 x, Int32 y) { // Check input values if (x <0 || y <0) throw new ArgumentException();

// Perform the operation Int32 result = x + y;

// Check output if (result <0) throw new ArgumentException();

return result; }

public Int32 Divide(Int32 x, Int32 y) { // Check input values if (x < 0 || y < 0) throw new ArgumentException(); if (y == 0) throw new ArgumentException();

// Perform the operation Int32 result = x / y;

// Check output if (result < 0) throw new ArgumentException();

return result; }}

Listing 3. A shortcut exit duplicates the code for postconditions.

public Int32 Sum(Int32 x, Int32 y){ // Check input values if (x <0 || y <0) throw new ArgumentException(); // Shortcut exit if (x == y) { // Perform the operation var temp = x <<1; // Optimization for 2*x

// Check output if (temp <0) throw new ArgumentException();

return temp; }

// Perform the operation var result = x + y;

// Check output if (result <0) throw new ArgumentException();

return result;}

Toll Free USA (888) 774-3273 | Phone (913) 390-4797 | [email protected]

Download the FREE fully functional 30-Day evaluation of SpreadsheetGear 2010 today at

www.SpreadsheetGear.com.

ASP.NET Excel Reporting Easily create richly formatted Excel reports without Excel using the new generation of spreadsheet technology built from the ground up for scalability and reliability.

Excel Compatible Windows Forms Control Add powerful Excel compatible viewing, editing, formatting, calculating, charting and printing to your Windows Forms applications with the easy to use WorkbookView control.

Create Dashboards from Excel Charts and Ranges You and your users can design dashboards, reports, charts, and models in Excel rather than hard to learn developer tools and you can easily deploy them with one line of code.

Microsoft Chose SpreadsheetGear...“After carefully evaluating SpreadsheetGear, Excel Services, and other 3rd party options, we ultimately chose SpreadsheetGear for .NET because it is the best fi t for MSN Money.”

Chris Donohue, MSN Money Program Manager

Untitled-9 1 11/2/10 12:10 PM

38 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

FEATURE Contract-First Approach

Expressing ConditionsYou can figure out the exact syntax of preconditions and postcondi-tions from the Code Contracts documentation; an up-to-date PDF can be obtained from the DevLabs site at bit.ly/f4LxHi. I’ll briefly summarize it. You use the following method to indicate a required condition and otherwise throw the specified exception:

Contract.Requires<TException> (Boolean condition)

The method has a few overloads you might want to consider. The method Ensures expresses a postcondition:

Contract.Ensures(Boolean condition)

When it comes to writing a precondition, the expression will usually contain only input parameters and perhaps some other method or property in the same class. If this is the case, you’re required to decorate this method with the Pure attribute to note that executing the method won’t alter the state of the object. Note that Code Contract tools assume property getters are pure.

When you write a postcondition, you may need to gain access to other information. You do this through ad hoc methods such as Contract.Result<T> to get the value (of type T) being returned from the method, and Contract.OldValue<T> to get the value stored in the specified local variable at the beginning of the method execution. Finally, you can verify a condition when an exception is thrown during the execution of the method by using the method Contract.EnsuresOnThrow<TException>.

AbbreviatorsThe contract syntax is certainly more compact than using plain code, but it can grow large. A natural remedy is grouping several contract instructions in a subroutine, as shown in Listing 5.

The ContractAbbreviator attribute instructs the rewriter on how to correctly interpret the decorated methods. Without the attribute to qualify it as a sort of macro to expand, in fact, the Validate Result method (and other ValidateXxx methods in Listing 5) would contain rather inextricable code. What would, for example, Contract.Result<T> refer to, as it’s used in a void method? Currently, the ContractAbbreviator attribute must be explicitly defined by the developer in the project, as it isn’t included in the mscorlib assembly. The class is fairly simple:

namespace System.Diagnostics.Contracts

{

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]

[Conditional("CONTRACTS_FULL")]

internal sealed class ContractAbbreviatorAttribute : System.Attribute

{

}

}

Visual Studio 2010 comes with a configuration page in the project properties specific to the configuration of Code Contracts. For each project, you must go there and explicitly enable runtime checking of contracts. You also need to download runtime tools from the DevLabs Web site. Runtime tools include the Code Contracts rewriter, the Code Contracts interface generator and the static checker.

Code Contracts help you write clean code by forcing you to indicate expected behavior and results for each method. At the very minimum, this gives guidance when you go to refactor and improve your code. VSM

Dino Esposito is the author of “Programming Microsoft ASP.NET MVC” (Microsoft Press, 2010) and coauthor of “Microsoft .NET: Architecting Applications for the Enterprise” (Microsoft Press, 2008). Follow him on Twitter at twitter.com/despos.

Listing 4. The calculator class written using Code Contracts.

using System.Diagnostics.Contracts;public class Calculator{ public Int32 Sum(Int32 x, Int32 y) { Contract.Requires<ArgumentOutOfRangeException>(x >= 0 && y >= 0); Contract.Ensures(Contract.Result<Int32>() >= 0);

if (x == y) return 2 * x;

return x + y; }

public Int32 Divide(Int32 x, Int32 y) { Contract.Requires<ArgumentOutOfRangeException>(x >= 0 && y >= 0); Contract.Requires<ArgumentOutOfRangeException>(y > 0); Contract.Ensures(Contract.Result<Int32>() >= 0);

return x / y; }}

Listing 5. Using ContractAbbreviators.

public class Calculator{ public Int32 Sum(Int32 x, Int32 y) { // Check input values ValidateOperands(x, y); ValidateResult();

// Perform the operation if (x == y) return x<<1; return x + y; }

public Int32 Divide(Int32 x, Int32 y) { // Check input values ValidateOperandsForDivision(x, y); ValidateResult();

// Perform the operation return x / y; }

[ContractAbbreviator] private void ValidateOperands(Int32 x, Int32 y) { Contract.Requires<ArgumentOutOfRangeException>(x >= 0 && y >= 0); }

[ContractAbbreviator] private void ValidateOperandsForDivision(Int32 x, Int32 y) { Contract.Requires<ArgumentOutOfRangeException>(x >= 0 && y >= 0); Contract.Requires<ArgumentOutOfRangeException>(y > 0); }

[ContractAbbreviator] private void ValidateResult() { Contract.Ensures(Contract.Result<Int32>() >= 0); }}

we are Countersoftyou are Control

I am testing

I am answers

you are software development

I am product supportyou are bug tracking

I am project management

I am agile

The first comprehensive solution to address ISVs’ profitability AND customer experience by targeting product support

The most versatile projectmanagement platform for software development and testing teams around the world

Project Platform Product Support Optimized

Patent pending Atlas addresses the area of the software business that matters most: the product support eco-system. Atlas is the logical alternative to revenue-sapping, customer-irritating forums. Atlas offers smarter community-based support and integrates FAQ/KB, documentation and how-to videos.

BOOK A DEMO / FREE TRIAL / 3-for-FREE Seeing is believing! We’ll talk about YOU. www.geminiplatform.com

The award-winning Gemini Project Platform is the .NET based project platform that uniquely allows teams to work the way they want to work with more functionality and easy customization for optimum team performance and predictable results.

DOWNLOAD NOW and lead from the front. www.atlasanswer.com

ENABLING COLLECTIVE CAPABILITY www.countersoft.com

I am creating

I am job management

I am issue tracking

rates FAQ/KB,

m

Contact // Europe/Asia: +44 (0)1753 824000 // US/Canada: 800.927.5568 // E: [email protected]

I am help desk

Untitled-1 1 2/22/11 2:19 PM

40 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

</Language Lab>YO U R CO D E S O U R C E

IN THIS SECTION: Practical .NET · 40 // C# Corner · 46 // Mobile Corner · 51

Why You Should Be Using LINQPeter Vogel introduces a new column on application develop-ment in the real world, and begins by advocating for Language Integrated Query.BY PETER VOGEL

Welcome to Practical .NET, a new column offering how-to insight and advice for developers working with the flagship Microsoft programming framework. You may be familiar with my Practical ASP.NET column, which I’ve been writing weekly (more or less) on the Visual Studio Magazine Web site since May 2008. Now my focus expands from ASP.NET to explore a variety of .NET technologies. Think diversity.

What won’t change is my commitment to “practical” program-ming. I’ll focus on the tasks developers do right now, every day, in delivering business applications to their users. There will be times when I cover a really cool technology that’s worth an early look. But by and large this column will focus on the tools and techniques that developers use to build functional applications.

One of the technologies that developers would be well advised to adopt is Language Integrated Query (LINQ). Not only does LINQ reduce the amount of code you have to write, it also gives you better performance and positions you for other technologies (like Parallel LINQ, or PLINQ, parallel processing).

Despite the advantages of LINQ, in my experience as a consultant and an instructor I’ve found that most of the developers I meet aren’t using it. In this month’s column I’ll look at why you’re not using LINQ, how to get started with it and the key technology you’ll need to fully exploit the technology.

In this column I assume that you’ve created an Entity Framework model based on the Northwind database and now want to retrieve the entities that correspond to your tables. I then walk through using LINQ to do that and introduce a key technology for exploiting LINQ.

LINQ BasicsLINQ is easy to get started with, assuming you’ve ever written an SQL statement or a For…Each loop. While SQL lets you select rows from

a table, LINQ lets you select objects from a collection. With Entity Framework (and LINQ to Entities), the two technologies overlap because the objects in the collection you’re querying represent the rows in the table you want to retrieve. Not only can you leverage your SQL knowledge in LINQ, but you can also take advantage of what you know about For…Each loops. In a For…Each loop, you’re used to creating a range variable like the cust variable in this example:'...a bunch of ADO.NET code to retrieve rows

'into a collection of objects called custs

For Each cust As Customer In custs

Next

If you counted up all the lines involved in converting the rows into objects, it would probably be several dozen lines of code—impossible for the compiler to optimize.

In LINQ/Entity Framework the equivalent statement also uses a range variable but, other than that, it looks much like a SQL statement: '...code to instantiate an Entity Framework

'ObjectContext called Northwind

Dim res = From cust In Northwind.Customers

Select cust

Ignoring the cust range variable, the major difference between this LINQ statement and an equivalent SQL statement is that first, I don’t need an asterisk (*) in the Select clause to retrieve all the properties (I just retrieve the whole object), and second, the From clause appears first. The From clause comes first because it allows you to specify the collection that your range variable is retrieving objects from. This gives you IntelliSense support for your range variable as you type in the rest of your LINQ statement.

One of the benefits of LINQ is that it’s only one statement (plus the Entity Framework code, of course) compared to the For…Each loop’s multiple statements. That compression makes it considerably easier for the compiler to optimize your code. Another benefit is that LINQ to Entities will generate all the appropriate SQL for you. It will also take care of generating all the ADO.NET activity, and manage opening and closing the connections. There are a lot of benefits to turning that work over to LINQ.

LINQ queries are also .NET friendly—I can use the output of a LINQ query to set the DataSource on a grid and let the user update the query’s results:

{ PRACTICAL .NET }

LIN

Q a

nd

En

tity

F

ram

ewor

k

My friends who know more about the inner workings of the .NET compilers tell me that the method-based syntax is the “real” syntax—the SQL-like syntax is just “syntactical sugar.”

Free 60 Day Evaluation! www.leadtools.com/sd (800) 637-1835

The LEADTOOLS Silverlight SDK gives Web and Windows Phone developers the ability to load, display, process, convert and save many different image formats that are not intrinsic to the Silverlight framework. Because of the 100% managed Silverlight binaries, images can be processed and annotated directly within the Silverlight application without the need for external server calls. The SDK comes with several viewing components including Image Viewer, Image List and Pan Window. The viewer is equipped with automated interactive tools including scroll, pan, scale, zoom and magnifying glass.

Adding Annotations and Markup to your Silverlight application is a cinch with LEADTOOLS fully automated annotations. Load, save, create and edit popular annotation types like line, ruler, polygon, curve, note, highlight, redaction and more.

Create highly interactive medical imaging applications with LEADTOOLS support for DICOM. Load, save and modify tags from DICOM data sets and display 12/16 bpp grayscale images with Window Leveling. LEADTOOLS is currently the only Windows Phone imaging toolkit on the market with support for DICOM data sets.

LEADTOOLS includes sample projects to demonstrate all of the SDK’s capabilities including the display of over 100 image formats and incorporation of more than 200 image processing functions.

Untitled-7 1 3/7/11 2:17 PM

42 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

LIN

Q a

nd

En

tity

F

ram

ewor

k

</Language Lab>{ PRACTICAL .NET }

Me.grdCust.DataSource = From cust In Northwind.Customers

Select cust

SQL OperationsIn SQL, the input and the output for a query is a table, so I can use the output from one SQL query as the input to another. I can also use the output from one LINQ query as the input to another LINQ query. That way, instead of having to write one big complicated query (that I probably won’t understand), I can write several simpler queries (that I will understand). This example, for instance, finds all the customers without a PostalCode and, if none are found, searches that collection based on the Customer’s city:

Dim res = From cust In Northwind.Customers

Where cust.PostalCode IsNot Nothing

Select cust

If res.Count > 0 Then

Dim res2 = From cust In res

Where cust.City = "Berlin"

Select cust

End If

Doing other “typical SQL” operations, like sorting and filtering, lets you continue to leverage your SQL experience:

Dim resBC = From cust In Northwind.Customers

Where cust.Region = "BC"

Order By cust.CustomerID

Select cust

As I noted earlier, part of the problem developers have with LINQ is that it has two different syntaxes. One is this “SQL-like” syntax that I prefer. The other is based around methods and lambda expressions. The method-based equivalent to my previous example would look like this:

Dim resMethod = Northwind.Customers.

Where(Function(cust) cust.Region = "BC").

Select(Function(cust) cust).

OrderBy(Function(cust) cust.CustomerID)

My friends who know more about the inner workings of the .NET compilers tell me that the method-based syntax is the “real” syntax—the SQL-like syntax is just “syntactical sugar.” It’s because of this underlying method-like syntax that you need to include an imports statement in your code for the System.LINQ namespace that these methods are part of.

Personally, I’m willing to take the hit on the compile time required to convert my pseudo-SQL to methods so that I can stick with a syntax I recognize. For instance, joining two collections looks enough like SQL to make me happy. The Join and On clauses look like SQL, except with range variables where I’d normally have table names:

Dim resjoin = From cust In Northwind.Customers

Join ord In Northwind.Orders

On cust.CustomerID Equals ord.CustomerID

Select cust

The biggest annoyance is that I can’t use “=” in the On clause (which is what my fingers want to type). I have to use “Equals.”

Not the Way It SeemsI think another problem that developers have in embracing LINQ is that the syntax looks so very, very inefficient. It looks like every row in

the table is converted into an object in a collection and then processed in a For…Each loop, with the rows you don’t want being discarded.

That’s not what’s happening. Instead, the compiler and LINQ to Entities looks at your LINQ statement and generates the appropriate SQL statement. I’ve looked at the SQL statements that are generated: They’re good. By which I mean, they look like what I would’ve written. Except that I didn’t have to.

Developers also, I think, get uncomfortable about the use of implicit declarations. I have to admit that I sometimes wonder what data type is being returned by this LINQ statement:

Dim resjoin = From cust In Northwind.Customers

Select cust

The range variable (cust) represents items from the Customers collection, so the result is probably going to be a collection of Customer objects. I’m comfortable with letting the compiler figure it out. I’m just interested in what methods and properties appear in the IntelliSense list when I type “res.” That answers the only question I’m really interested in: What can I do with this collection?

If you hover your mouse over the res variable, the tooltip will tell you what data type you’re getting. It usually turns out to be

WHY YOU’RE NOT USING LINQMicrosoft and magazines like this one have been telling you to adopt LINQ and Entity Framework for what seems like years. The main reason that you haven’t done so is, probably, because you’re smart. Smart developers build up a body of what I call “programmer lore”—all the knowledge that you accumulate while you build applications in a particular technology. This includes all the standard solutions you keep in your mental toolbox, the generalized code you use in multiple applications, the cut-and-paste code you move from one program to another, and more. Programmer lore is knowing what the compiler’s error messages really mean.

When it comes to data access, you’ve probably built up a working knowledge of SQL—you can pretty much get SQL to do what you need. You’ve probably assembled a personal toolkit with some combination of writing raw SQL, a graphical tool for building SQL and ADO.NET. Your fi ngers can probably automatically type out the fi ve or eight lines of code to create and open a connection, defi ne a command object, call the ExecReader method and loop through the retrieved records.

And you know that you’ll need to learn a bunch of new stuff to really use LINQ. During that period your productivity will be reduced, but your deadlines won’t change. Finally, there are surely things you’ll want to do in LINQ that, currently, your fi ngers won’t type out automatically.

I’m here to help. It’s time to give up ADO.NET and SQL and commit to the potent combination of LINQ and Entity Framework. Instead of writing all that ADO.NET code, line by painful line, you’ll declare what you want with LINQ, and LINQ/Entity Framework will write the code for you.

The code that’s generated by the LINQ/Entity Framework combination is, effectively, code written by the people who designed

.NET and ADO.NET. I don’t know if these people are smarter than me or you (though I have my suspicions), but they do know a great deal more about how those underlying technologies work. Trust me, you want their code. —P.V.

Untitled-1 1 1/13/11 9:48 AM

44 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

LIN

Q a

nd

En

tity

F

ram

ewor

k

</Language Lab>{ PRACTICAL .NET }

something like System.LINQ.IQueryable(of someObject). The “I” prefix in IQueryable indicates that the declaration is an interface. So, apparently, the compiler isn’t even sure what’s coming back from the LINQ expression: All the compiler seems to know is that the return value will be some class that implements the IQueryable interface. If the compiler doesn’t know, why should I?

If not explicitly knowing the declaration bothered me, I could type it in:

Dim res As System.Linq.IQueryable(of northwndModel.Customer) =

But why bother? What value am I adding to the process if I do? Either I type in the same declaration that the compiler has figured out or I type in a different (and wrong) one. If the only “value” that I can add to a process is getting it wrong, I’m just as happy to skip it.

Quite frankly, not having to work out the data type is one less piece of trivia for me to have to worry about. In addition, small changes to my LINQ statement result in significant changes to what’s returned. For instance, I might decide to return just the CompanyID instead of returning the whole Customer object, giving this LINQ statement:

Dim resCustId = From cust In Northwind.Customers

Select cust.CustomerID

My variable res is now going to be some collection of strings. If I’d declared the data type on res when the query retrieved the whole object, I’d now have to go back and change it. Life is too short.

The extreme example is when you don’t even specify the object that’s being returned. If, for instance, I only want a few of the properties on an entity, I’ll create an anonymous object in my Select clause. With an anonymous object, I use the New keyword but don’t specify a class name—the compiler will make one up. I just have to list off the properties I want on the object and what values to set them to. You even get to take advantage of your knowledge of the With keyword (though you have to use some annoying braces).

This code causes the compiler to generate a class with two properties called Id and Contact, and sets those properties to values from the object being retrieved:

Dim resPart = From cust In Northwind.Customers

Select New With {

.Id = cust.CustomerID,

.Contact = cust.ContactName}

It’s a very Zen approach to programming: just let go and let the universe—or the compiler—take care of the trivial details (like what your class is called). All you need is the IntelliSense list so you know what properties you can access.

Don’t ExecuteSomething else that bothers developers about LINQ (and that I personally like) is the deferred execution. While I can put a LINQ statement into my code, it’s not going to execute at the place where I put it. At run time, the connection to the database won’t be

opened, the SQL statement won’t be sent to the database, and the rows won’t be retrieved until I manipulate the LINQ query’s result. In this case, that means that the code won’t touch the database until I get to the For…Each loop:

Dim resDefered = From cust In Northwind.Customers

Select cust

'...126 lines of code…

For Each c In res

Next

I used to spend a lot of time ensuring that I never opened a connection that I didn’t use—and that I closed that connection as soon as I possibly could. This deferred execution of LINQ means I’m now guaranteed that I won’t open a connection until I need the objects being returned. I count on LINQ and Entity Framework to ensure that the connection is closed as soon as possible.

If I’ve used the output of one LINQ expression as the input to another LINQ expression, it’s only when the output of the second expression is used that the SQL is generated. So, I may have written this:

Dim res = From cust In Northwind.Customers

Where cust.PostalCode IsNot Nothing

Select cust

If res.Count > 0 Then

Dim res2 = From cust In res

Where cust.City = "Berlin"

Select cust

End If

But it won’t be until I touch the res2 collection that my SQL will be generated. At that point the compiler will look at both LINQ expressions and, probably, generate a single SQL statement that combines both. So I get the benefit of having only one query issued to the database, but I also get the clarity in my code that comes from breaking down the problem into simpler steps.

Of course, if I’m returning data from a method, returning an “unresolved” LINQ query is probably a bad idea. In addition, if I’m returning some data, I probably want to define the data type that I’m returning. While I’m OK with the compiler specifying my collection’s data types and leaving me in ignorance, developers using my methods probably want something more definite. And, if my method is part of a Web service, I also want to return something that will convert well into an XML format.

I can force my LINQ query to retrieve all of its data and nail down the data type by using one of the To* methods on the LINQ result to convert my results to a List or an array. It’s not unusual for my methods to look like this:

Public Function GetCustomerByID(ByVal CustId As String)

As List(Of northwndModel.Customer)

Dim nw As New northwndModel.northwndEntities

Something else that bothers developers about LINQ (and that I personally like) is the deferred execution. While I can put a LINQ statement into my code, it’s not going to execute at the place where I put it.

VisualStudioMagazine.com · April 2011 · VISUAL STUDIO MAGAZINE 45

LIN

Q a

nd

En

tity

F

ram

ewor

k

{ PRACTICAL .NET }

Dim res = From cust In nw.Customers

Where cust.CustomerID = CustId

Select cust

Return res.ToList

End Function

Thinking ArchitecturallyThis method (and ones like it) creates a problem with architecturally rigid developers. Does this method go in the data layer of your architecture? After all, it’s accessing data, and because you’re no longer writing methods filled with ADO.NET code, this is as close to your database engine as you’ll get. Or does this method belong in your business layer because it’s performing a business function: returning the customer object that corresponds to the customer Id? Should I call the method from my presentation layer or should I call the method from an intervening business layer that my presentation layer calls? LINQ and Entity Framework, to a certain extent, blur the boundary between the business and data layers.

But the underlying driver is the “separation of concerns.” Each class and each method does one thing; each method is (relatively) simple and easy to understand; and applications are assembled out of simple components that come together to do complex things. The criteria that are set by the separation of concerns are what matter and this method, LINQ and Entity Framework, meets all of those criteria.

Getting Rid of the For…Each LoopAnother issue that developers have with LINQ is that it seems like you do everything twice: You write the LINQ statement to retrieve your rows and then you write a For…Each loop to process each item you’ve retrieved. With ADO.NET, you can just retrieve each row and process it as you retrieve it.

This reflects how developers are missing the connection between LINQ and another important technology: Extension methods. Extension methods let you skip that For…Each loop after your LINQ statement.

Extension methods are methods that aren’t attached to a specific object. Instead, you specify the kind of object the methods should be attached to and .NET takes care of listing the method in those objects’ IntelliSense lists. This allows you to define a method that, for instance, will attach itself to any IQueryable collection—such as the result of any LINQ query.

For example, let’s say that you want to convert the results of your LINQ query into a comma-delimited string (I don’t think there’s an existing .NET extension method that would do this). You want the CustomerId and CompanyName properties in each row, separated by a comma, and with each row terminated by a carriage return.

You could write a For…Each loop to run after your LINQ query: Imports System.Runtime.CompilerServices

...

Dim res = From cust In nw.Customers

Where cust.CustomerID = CustId

Select cust

Dim csv As StringBuilder = New StringBuilder

For Each cust In res

csv.Append(cust.CustomerID & "," &

cust.CompanyName & vbCr)

Next

Or you could put that code in a method in a Module and decorate that code with the Extension attribute:

Module PHVExtensions

<System.Runtime.CompilerServices.Extension()>

Public Function ToCsv

(ByVal custs As IQueryable(Of northwndModel.Customer)) As String

Dim csv As StringBuilder = New StringBuilder

For Each cust In custs

csv.Append(cust.CustomerID & "," &

cust.CompanyName & vbCr)

Next

Return csv.ToString()

End Function

End Module

The first parameter in the method establishes what kind of object this ToCsv method will attach itself to. In this case, it will attach itself to any collection of Customer objects that implements the IQueryable interface. Or, to put it another way, it will attach itself to the output of any LINQ query that works with Customer objects. The collection this method is attached to will be passed into the method where it can be manipulated.

You can now eliminate the For…Each loop that follows your LINQ query and use your new extension method:

Dim res = From cust In res

Where cust.City = "Berlin"

Select cust

Dim csv As String = res.ToCsv

Or, because there’s always at least four different ways to do something with LINQ, just attach your extension method to your LINQ expression (after all, your expression is really just a set of methods anyway):

Dim csv As String

csv = (From cust In res

Where cust.City = "Berlin"

Select cust).ToCsv

Letting GoI think another reason developers avoid LINQ is because it implies a certain amount of “de-skilling.” I spent years developing expertise in SQL (and I have the three-foot shelf of books to prove it), and now I may never need it again. Ditto for ADO.NET: I know all sorts of ways to optimize ADO.NET, and I may never need it again. LINQ seems to take care of it all.

But that’s not a bad thing. When I started programming, the course that I took had me learn machine code and then assembler (and this was long enough ago that I was typing my code into a punch-card machine). What can I say: I’m very old. That’s another set of skills I just don’t need any more.

But there are lots of opportunity to develop skills in LINQ to Entities—for instance, optimizing LINQ by leveraging or disabling lazy loading (that’s the Include function). Understanding all of those built-in extension methods and how you can leverage them to get what you want is another area where you can develop expertise.

It’s time to develop some new lore. VSM

Peter Vogel ([email protected]) is the tools editor for Visual Studio Magazine and a principal in PH&V Information Services, specializing in ASP.NET development.

46 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

Exp

ress

ion

T

rees

</Language Lab>

Using Expression Trees in Your APIsHow to translate C# code into expression trees to eliminate strings, standardize parameter validations and interact with other data structures.BY PATRICK STEELE

You’ve probably used expression trees before, but may not have realized it. Expression trees offer a convenient way to “examine” and “take apart” a lambda expression to find its parts. You can even execute the lambda (or not!) depending on your needs.

Are you using expression trees today? If you’ve used any mocking frameworks like Rhino.Mocks or Moq, you’ve probably written code like this:

parser.Expect(p =>p.Process());

This is setting an expectation that the method “Process” on the variable “parser” will be called during the test. The Process method is not called at this time (more on that later).

Or maybe you’re using the new Fluent NHibernate library to do your nHibernate configuration without XML files:

public class EmployeeMap : ClassMap<Employee>

{

public EmployeeMap()

{

Id(x =>x.Id);

Map(x =>x.FirstName);

Map(x =>x.LastName);

References(x =>x.Store);

}

}

That’s right—it’s using expression trees. So what exactly is an expression tree? MSDN documentation

(tinyurl.com/4jvt9cc) defines it as follows:“The expression tree is an in-memory data representation of the

lambda expression. The expression tree makes the structure of the lambda expression transparent and explicit. You can interact with the data in the expression tree just as you can with any other data structure.”

That last part is important. By giving you a data structure that represents the entire lambda expression, you can do all sorts of things with it.

Let’s start with a simple example. Suppose we had a method that takes two strings and combines them in some way to produce a new string:

Func<string, string, string> combine = (a, b) =>a.ToLower() + b.ToUpper();

var one = "One";

var two = "Two";

var combined = combine(one, two);

After running this code, the variable “combined” will have the value “oneTWO.” Now, instead of defining a lambda for the combine method, let’s build an expression tree that represents the lambda itself:

Expression<Func<string, string, string>> tree = (a, b) =>a.ToLower() + b.ToUpper();

What can you do with this? With a few lines of code, you can examine the structure of this lambda:

Console.WriteLine("Paramter Count: {0}", tree.Parameters.Count);

foreach (var param in tree.Parameters)

{

Console.WriteLine("\tParameter Name: {0}", param.Name);

}

var body = (BinaryExpression) tree.Body;

Console.WriteLine("Binary Expression Type: {0}", body.NodeType);

Console.WriteLine("Method to be called: {0}", body.Method);

Console.WriteLine("Return Type: {0}", tree.ReturnType);

Running this code produces:Paramter Count: 2

Parameter Name: a

Parameter Name: b

Binary Expression Type: Add

Method to be called: System.StringConcat(System.String, System.String)

Return Type: System.String

As you can see, very little code is needed to determine:■ The number of parameters■ The names of those parameters■ The return type of the lambdaBecause this example is a BinaryExpression, we also see that it’s

an “Add” of two other expressions and that the String.Concat is the method that’s going to be used for the “Add.”

If you’re interested in diving a little deeper, check out the “Expression Tree Basics” blog posting written by Charlie Calvert, the Microsoft community program manager for the C# group (tinyurl.com/4eoatx7).

Using Expressions to Eliminate StringsThe Castle MonoRail project (castleproject.org) is a popular Model-View-Controller (MVC) framework for ASP.NET. You don’t need to understand MVC or even be a Web developer to appreciate this example.

When working with a controller (a class), often times you need to “redirect” an ASP.NET request to a different method (“action” in MVC) within the controller, or possibly even a different controller. In its simplest form, the redirect method looks like this:

this.RedirectToAction("LogonFailed");

Remember, an MVC “action” is just a method. In the previous example, the framework will look for a method called

“LogonFailed” and will move execution to that method, which is simple and effective.

The problem comes when you do some refactoring. Perhaps you rename the “LogonFailed” method to “UserLogonFailed.” The refactor tools in Visual Studio 2010 will make this rename easy, but in the previous sample, that’s a string—it doesn’t represent the name of a method to the compiler. This is the perfect place where an expression tree can help you out.

First we’ll create an extension method that can be called from within our controllers:

{ C # C O R N E R }

Project3 12/16/09 11:55 AM Page 1

48 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

Exp

ress

ion

T

rees

</Language Lab>{ C # C O R N E R }

public static void RedirectToAction<TController>(

this TController controller,

Expression<Action<TController>> expression) where TController : Controller

{

}

The method takes a generic parameter which must be of type “Controller” (a base class provided by the MonoRail MVC framework). The one parameter this extension method accepts is an expression that represents an Action<> on the particular controller. Methods (actions) on a MonoRail controller must be

“void” methods, so the Action<> delegate can represent any void method on the particular controller class.

Now that we’ve got an expression that represents a lambda for a particular method, we can examine that data structure and pull out the name of the method:

var methodCall = expression.Body as MethodCallExpression;

if (methodCall == null)

{

throw new ArgumentException("Expected method call");

}

controller.RedirectToAction(methodCall.Method.Name);

First make sure the lambda represents a method call. If it doesn’t, we need to throw an exception. If it’s a method call, we call the built-in version of RedirectToAction that takes a string—in this case, the name of the method within our lambda! Now our code can use our new extension method to redirect to a different action:

this.RedirectToAction<SampleController>(c =>c.LogonFailed());

As you can see, by including the type of controller, we get IntelliSense and auto-completion within the IDE as well as full support for all of the refactoring tools. If you use the Visual Studio rename method, this lambda expression will get renamed, too. No more search and replace of strings when you rename a method in MonoRail!

A technique similar to this one is employed in the Fluent NHibernate project (see earlier example, on p. 46). Before Fluent NHibernate, the “Id” property along with the “FirstName” and

“LastName” properties were defined in an XML mapping file (as strings). The technique shown here is much easier and gives you full support of the Visual Studio refactoring tools.

Here’s a simple example of accepting a lambda that represents access to a property. In this example, the generic type “T” represents that type of class the property belongs to:

public void Id<V>(Expression<Func<T, V>> expression) {

var exp = expression.Body as MemberExpression;

if (exp == null)

{

throw new ArgumentException("expression must be a member expression.");

}

var columnName = exp.Member.Name;

// Regular nHibernate stuff

}

Func<T,V> represents a function that accepts our class type as the first parameter and returns V (the return type of the property). This lets us maintain the strong typing in C#.

As before, we make sure that the body of the lambda represents a MemberExpression. A MemberExpression represents access to a field or property. Finally, if we do have a MemberExpression, we can look at the member’s name—which represents the

property name—and plug that into nHibernate. No more hardcoded strings!

Executing Expression TreesSo now that we can use an expression to take a lambda apart and examine its constituent parts, what if we want to actually execute that lambda and get some results? That’s easy.

The Expression<T> type contains the method “Compile,” which, as you might expect, compiles the lambda represented by the expression. It returns a delegate that can be used to execute that lambda. Here’s a simple example of compiling an expression tree into executable code:

Expression<Func<int, bool>>isEvenExpression = i => i%2 == 0;

Func<int, bool>isEven = isEvenExpression.Compile();

for(var a = 0 ; a <= 10 ; a++)

{

Console.WriteLine("Is {0} even? {1}", a, isEven(a));

}

First, we define an expression that represents a lambda that takes an integer and returns a Boolean indicating whether the integer is even or not. We then compile that expression into an actual delegate. Finally, we can use that delegate just as you would any other delegate.

Now let’s look at some things we can do with the ability to compile expression trees.

Standardized Parameter ValidationWe’ve all written parameter validation logic. It’s tedious, but necessary to ensure our applications perform properly. A simple case of making sure a couple of parameters passed in to a constructor aren’t null becomes:

public DataFileParser(string filename, IColumnNames names) : base(filename) {

if (filename == null)

{

throw new ArgumentNullException("filename");

}

if (names == null)

{

throw new ArgumentNullException("names");

}

}

We can take advantage of expression trees to standardize some of this code as well as eliminate the hardcoded parameter names—which will not get renamed if we use the Visual Studio refactor tools to rename the parameters!

For this example, we’re starting with a static Validator class, which will hold our standardized validation code. Our first step is to create a method, which accepts an expression representing access to a parameter:

public static class Validator

{

public static void ThrowIfNull(Expression<Func<object>> expression)

{

}

}

As we showed in earlier examples, we need to make sure this expression represents access to a field (a parameter, in this case). Once we decide we have the proper expression, we’ll compile it,

ement1); areaSeries Add(seriesElement2); areaSeries Add(seriesElement3); // Add series to the plot area plotArea Series Add(areaSeries); //page Elements Add( new LayoutGrid() ); // Add the page elements to the page AddEAement1); areaSerieies.AAdd(se(s rriesElement2t2); a) reaSeries.AdA d(seriesElement3); // Add series to the plot area plotArea.Series.Add(areaSeries); //page.Elemenem ts.Add( ddd( new ne LaLayyoutGrid() ); // A/ dd the page elements to the page AddEA

s, 240, 0); AddEAN1AN 3SupSup5(pa5(p ge.Elemeentnts, 480, 0); AdddUPCVersionA(page.Elemene ts, 0, 135); AddUPCVersionASup2(page.Elements, 240, 135); AdddUPCddUPCd CVerssionAionAo Sup5((page.Elemennts, t 480, 135); AddEAN8(page.Elements, 0,

.Elements, 480, 2270);; AddddUUPCVersionE(papage.Elementts, 0, 405); AddUPCVersionESuE p2(page.Elements, 240, 405); AddUPCVersionESup5(pageage.Ele.Elelemmments, 4s, 48800, 4405); // AAdd the page toe t the document document.Pages.Add(pa

CaptionAndRectanga lee(elemeements, “EAN/JA/JAN 13 Bar Codde”, x, y, 204, 99); BarCode barCode = new Ean13(“123456789012”, x, y + 21); barCode.ode.X +=X +X +=X + ((2004 -4 - baarCoode.GettSymbolWidth()h ) / 2; elements.Add(barCode); } private vovo

dRectangle(elemente s,, “EANEAN/JAN 13 Bar Car Code, 2 digit supplement”, x, y, 204, 99); BarCode barCode = new Ean13Sup2(“12 234556789678 0121212”, 2”, x, yy + 2+ 211); 1); barCoode.XX += (204 - barCode.GetSymbolWidth()) / 2; elements.Add((barC

ts, float x, float yy) { A{ AddCaCaptionAndRectanangle(elements, “EAN/JAN 13 Bar Code, 5 5 digit supplement”, x, y, 204, 99); BarCoa C de bbarCoarCCode =de = newn EEanEan113SuS pp5(“12234556789001212345”, x, y + 21); barCode.X += (204 - barCodee.Get

ddUPCVersionA(GrouGroup elemenem ts, float x, floatfloa y) { AddCaptionAndRectangle(elemente s, “UPC Version A Bar Code”, x, y, 2y, 204, 99);9)99);9) Bar BarB rBa CodeC barbarCCode = neew UpcVepcVersionAA(“12345678901”, x, y + 21); barCode.X += (204 - baarCo

ddUPCVersionASSup2(up2 Grououpp elements,, floatoa xx, float y) { AddCaptionAndRectangle(ele( ments, “UPC Version E Bar Code, 2 digit git supsuppsuppup lement”nt”, x,x, x y, 204, 999); BaarCodde barCCode = new UpcVersionASup2(“123456787 90112”, xx, yx, y +

s.Add(barCode); } } pprivate te vooid AddUPCVPCVersiers onASup5(Group elements, float x, floato y) { AddCaptionAndRectangle(eleementmmentm s, “s, “UPC UPC VerVersion EE Bar Code, 5 diggit suupplemment”, x, y, 204, 99); BarCode barCode = n ew UpcVeeersio

ode.GetSymbolWWidth(dth )) / 2; 2 elements.AddAdd(bar(ba Code); } privatee voi v d AddEANEAN8(Group p elements, float x, float y) { AddCddCaptitionAnonAn dRecReccecttaangle(elemments, “EANN/JANN 8 BBar Codde”, x, y, 204, 99); BarCode barCode == newn Ean8(“123434

g(); fileDialog.Title =le = “Op “Open Fen File Dialogg”; filfi eDialog.Filter = “AdAdobe PDFF fileses (*.pdf)f)|*.pdf|All Files (*.*)|*.*”; if (fileDieDialog.log.ShSSShowwDDiallog()og == DialoggResult.OK) { pdfVieweewer.OppenFile(fileDialog.FileName, “”); } SaveSav FileF Diallog saavaveFa

File Dialog”; s saveFveFileDialoal gg.Filter = “AdoAdobee PDF files (*.pdf)f)|*.pdf|All Files (**.*)|*.*”; if (saveFileDialog.ShowDowDialoialoa g()=g()==DiaDi=DiaDi logResulsule t .OOK) {{ pdfVfVieweewerr.SaveAs(saveFileDiaDialog.FileNamee); ); } } if (p( dfVidfV ewewer.PPagP e

WithDialog(); } e else se { MessMe aageBox.SShow(w “PPlease open a fifile tto printt”); } OOpenFileF Dialog fileDiD aloglog = n = nnnew Oew Oe pepenpenFileDDialog(); fifile Dialog.Tiitle = “Open File Dialoog”; filleDialog.InitialDirecectoory = @”c:\”:\ ; fi fileDleDialoglo .Filterter = “= “All F

) == DialogResules t.Ot.OK) { Dy D nnamicPDFFViewewerrClass test = new Dew DynammicPDFVieewerCr lass(); PDFDFPrinPrintter prinprinninterter er = = test.OpenFpe ileForPorPrinnter (file(fileDiaalog.FileName); pprinnter.PrintQuieQuiet();() } bytbybytby ee[] contcontentst =

pServices; GCHGC andandle gcchh = GCHandled .AllAl occ(contents, GCHHandndleTypType.Pinnedd); IntIntPtr contcontentsentsIntPtr ===gcch.ch.h.AAAddrOfPinnednn Objeect()ct ;ppdf Viewer.O.OpenBpepe ufffefeuff r(cor(r(cor( ntentsIntPtrt ,

kmark Page Elemelement:”, x,x, y); y); p pageEleementen s.AAdd(new Bookkmarrk(“( BBookB marked Text”x , x , x + 5,+ 5, y + 20,0 parpareenenttOe utline)); pageElg emennts.Ats.Add (new Label(“This tes texxt is bookmaokmaokmarkedrked ”, .”, xx + 5, y + 20, 2

ageElements, fls, float a x, float at y) {{ // Addsdss a circltt to the pageEllemeents AddCaptioonnAndRAndRectaectangle(paggpagpaggeEleeEl mentmen s, “Circle PPaage Elemment:ent:”, x, y); pageElgeElements.As.Add(ndddd(ndd ew CCircle(x (x + 112.5f2 ,

shLarge)); } pprivavate te void AddFAd orormattedteede TextArrea(Group pp ageeEg lemennts, float x,x, floafloat yt y)t { /{ / AdA dsds ads a for forfofoo matttedd text area too tthepageEeElle ments strring formattm edHtedHtml = “<p“<p><<i>Dynamic</i><b>PDb>P F</bb>&tm>&tmmtm; Generaeraaator oro v6.0 foror .NE

matting suppoort for or text thath t aappears s in the ddocument. Yt. Youuu havve “ + “comcompletetple e cooontrorol ovovovovver 8r 8e parar agraph pph properties: ssppacing befoeforee, spacingg after, firfirst liine “ + “indentantation, left indentatitation, righr t ininndentdentdentntatiotionn, a, aaliignment, alalllowi

<font face=’Tim’Times’es’>fontt fac f e, </fonnt>t>><f> ont ppoino tSizSize=’6’>fffont “ + “““size, </</fonfonntn ><fo<f nt ct coolorloolorlol =’FF000000 ’>>coloor, </font>><b>b<b> old, </b</b><<i>italic aannd </i><<u>uunderline</u>>; “ + “and 2 line proopertrties: leaeadingng, anndd leleeeaadinaad g type. Text

extArea = neew FoFormatrm tedTdTextAArea(fororrmmattedHHtmtml, x + 5, y +++ 20,, 21555, 60, F, FontontFamilmmi y.HeHeelvvelveteticaica, 9, ffalse)e); // SSets the the indent properoperty foformatteatt ddTextAreeaa.Styyle.PParagrapph.Inndent = 18; AddCCapttionAndRectRectanglgle(pae(papa(paapae geEgeElements, ts, “F

ageElemem ntts, “Fo“FormmattedTdTextAtArea OOvvverflow flow TText:”, x + 27999, y); pagpaggeEleeEleementmen ss.AdAdAdd(fod(foodd ormrrmatrmatmatatttedTextAtArea)); // CCreate e an oa verflow formatteded t text art a ea for tr the ooverflflow textt FoormattedTextArea ova oveerflowForFormattma edTeTTeextArxtArxtArtxttArea =ea =e formatte

a(x + 284, y + 20)20); pap geEElemenements.Adddd(o(overflverflowwFowFoow rmatarm tedTeextAe rrea); } privprivate ate vvooidov AddAddA dA Imagmagmagmage(Group up paggeElememeents, float x, float y) { // A/ dds an in magee tto thhe paageElemmenents AddCaptionAndRedRectangle((pagpageElemmmentsenntstsnts, “Imagegee Pag

es/DPDFLoogo.pn.png”), x ++ 1112.55f, y ++ 550f,50f, 0.20.244f);4f // Image is sizeed annnd centeenteredd in tn tthe rrrrectataec nglengle imam ge.SetBoB unds(215, 60); image.VAlign = VAlign.Cenenterr; imaage.Alignn = Align.Center; paggeEeElements.Ad.Addd(imaggee)g ;; } } privvate ate vvoidv A

pageElemennts AdAddCapdC tiononAndRectaannglengle(pag(paggeeEleeElements, “LLabell & PPagePageNumbeerinerine gLgLabg bel PPage ElememE entts:”, x, y); string labelText = “Labels can be rottaated”; strring numbermbe Text = “PageNummbeeringLabelsels contcontaiain ppage nummbeerib ngTT

xt, x + 5, y + 12+ 12, 22220, 80, F0 ontt.TimemessRomRoman,an, 12, TextAlign..Cennter);; l lababel.AngAngglle = 8; 8; 8; PagePageeNNumNumbN erinri gLagLabel pageNumLabel = new PageNumberb ingLabelab (nnumbberText, x +x + 5, y + 55, 220, 880, FFont.TimesResRoman, 1212, TextAltAligignign.n Ce

mem nts.Add(labeabel); l); } private voe id AAddLdLinne(Gne(Groupp pageElemennts, flflfloat x, floaoat y) {{{) { / // Addss a l a lla inne to the phe pageEag lements AddCaptionAndRectangle(p( ageElemee nnts, ““Line Paage Element:”, x, y); ppageeElemennts.As.Add(neew Lw ine(x +x + + x 5, y5 +

w Liw ne(xx + 2+ 220, y + 20, x + + 5, yy + 8 + 0,0, 30, 3, Rg, RgbCoolor.Green)); } prprivivaate vvoid Ad AddLiinknk(Groupup p pagpap eElementments, float x, float y) { // Adds a link to the ppageElemeem ntts Foont font == Foont.TimesRoman;; st string text = “TThisT iss s a lia nk tnk tk o o Dynaamic

mentm :”, x, y); Label label == newne LaLabbbel(textt, x + 5, y + 20, 2155,5, 800, fonnnnt, 1t, 2, R2, RgbbColor.or.BBluelu ); l; abel.UndUndererline = true; Link link = new Link(x + 5, y + 20, font.on GGetTeextWidthh(texxt, 12), 12 - font.GGetDDescendder(1r(12), neeww ee UrlUrlAlAction(“n(“hhttp

EleE mennts.Add(li( nk);; } p } privavate ve voidd AAddPath(ath Grroup pageElemmentts, floatoatfl x, floatt y) y) {{ // AddAd s a s pathh to the pageElements ceTe.DynamicPDF.PageElementen s.Pathh path = nneww ceTe.DynamicPDPDF.PF.PageElemenmennnts.Ps.Paaath(h(x + x + 55, y, y + + 2+ 20, R

PathP s.AAdd(new LineeSubPatPa h(xx ++ 2215, y + 4+ 0))); path.Suh.S bPatths.Ahs.AAAdddd((new CurvurveeToSubPatPa h(x h(x + 1008, y + 80, x + 160, y + 80)); path.SubPaths.Add(neww CCurvveSuubPath(x + 55, y + 40, x + 65, 6 y + y + 80, x + 5, y5, yy + + 60))); AddAddCCaCaptC ionAAnd

AAdd(ppaaath); } privatee void AAddRReccttaangle(GrG oupp pageeEElemennnts, flflflofloatat x, float yat y)) oorderee dLisdL t = t = ordderedList.GetOverFlowList(x + 5, y + 20); AddCaptionAnAndRRectaanggle(pagge.Elements, “Orderr ed Led List Pagegee Ele EleEl mentttn OOOve Ovev rflowrfl :”, x, y, 2

88; /8; // C/ Create an unoordereedd list UUnnornorderede List uunorderere edListt =t =stt neeew UUnonorderrderedLLied st(xx + 55, yy + 20+ 20, 400, 90, Font.Helvetica, 10); unorderedList.Items.Add(Add(“Fruits””); uunorderedere List.Items.Add(“d “VegeVegeg tablees””); UnU U ordeeer redSreedSd ubbList unord

tt((); (); unorderedSubList.Items.ms.Add(“dd((““ Citrus”); unordorderededederedSuSubLiist.Iteeemss.AdAddd(“ Nonn-Citrt us”)s” ; AdAddCCaptionAndRectangle(page.Elemennts, “Unordered Lisst Pagee Elemmente :”, x, yx, y, 225, 110); Unonn rddereedSubLisbLisst ununnu ordederedSredSdredSdd ubLiub st2 = uno

rederer SubbbList2.Items.Add((“PoPotato”); unorderedSSubLiiubLisstt2.Itemmms.Addddd(“BBeans”); Unorno derederedSubdSubLisst subUnorderedSubList = unorderede SubLS ist.Items[0]].SuubLists.AAddddUnorderrde edSubList(); subs bUnorUnorderedSubdSSubbLListLLL .Iteteeems.Ams.Am Addd(“Lime”); s

LList subbbUnorderedSSubList2st = unorderedSubLbLS iist.ist.Itemss[1].SuubLissts.AAddUnordeeredSuedS bLisbL t();t() suubUnorderedSubList2.Items.Add(“Mana go”); subUnorrdereedSSubList2.It2 temms.AAdd(“Banana”); ); UnUnordderedSSuSubdS ListLissLis sububUnorn derede dSubList

tt(()(); subUUnordereddSSubList3.Items.Add(“SweSweew t PoPotato””); Unoorderred dSSubList subbUnorUno dereer dSubdSubListList44 = unorrderedSubList2.It2 ems[1].S].SubLists.AddUnoordeereddSubbList(s ); subUnubU orderedSubLibubListst4.s Iteems.AdAdAdA d(“Sd(“S“Strining BeeBeean”)an” ; subUnoU rde

AAdddd(“Kiddney Beanean”); x += 279; paga e.Elemeements.Addd(ud nnordereedLisst); uunorderedListLis == unordnorderedere List.GetOvere FlowList(x + 5, y + 20);) AddA CaptionAndRecctanngle(ppageag .Elemeents, “UnorUnorderederer d Lid st PPage e ElemElemmmeent ee Oveverve flow:flow:flo ”, x, y, 225

ooiddd AdddTedTextFxtField(Group pageElemenme ts, , flofloat x,, flooat y)) { TexxtField txtt = new TextFixtF eeld(“txt“t fnafname”, x + 20, y + 40, 120, 20); txt.Defaulu tValue = “This iis s a Scrrollabble Te extFFieldd”; txt.BordederColrColC or =o RgbRgbCColoor.Br.Br.Br.Black; txtxttxtxt.BacackgrokgroundCun o

(td(d xt); TTexTextField txt1 = new TextFiField(ld “txxtf1naf1 me”,me” x + 175, yy + 440, 120, 20); txtt1.DefDe aultu Valualue = “TextField”; txt1.Password = true; ttxt1.MaxLength = = 99; txtt1.BoordderColor = RgbCollor.BBor.Black; txt1.B1.Backgckgrounou dCololor =oror =or = RgbRgbR ColoColor Alr.Al

eree ies(); pieSeries.DataLabel == da;a;da plop tAreAreaa.Sea riesrie .Add(pieSSeriess); ppieSeries.Elemelementss.Add(Add(27,27, “Website A”); pieSeries.Elements.Addd (19, “Website BB”)); pieSerrieses.Elementmen s.Add(21d(21, “WWWebsiseb te Cee ”); ”); ); pieSpieSp eries.ElElemenmeements[0ts[0s[0s[0].Co].C lor or == a

esess.Elements[2].Color = aututograog diendientt3;”RgbCRgbColoor.AliceeBlue; txt2.Too.ToolTip = “Multilinnee”; pagepageElElements.Add(txt2); AddCaptionAndRectangle(pageElememennts, “TexxtFiField Formorm PagPage Ele Elemenemenemennnt:”,:”, x, y, 5, 5y 0404, 85);5) } p} rivaate ve ve ooid oid AddCdCombomb

CombCCC ooBox(“cmmbnambna e”, e”, x + x + 51, 51, y + y + 40,40, 150,15 220); cb.BBorderColoor = RgbColor.BlacBlack; ccb.Bab.BackckgroundColor = RgbColor.AliceBlue; cb.Font = Font.Helveelveticaa; cbb.FonFo tSizzSizze = e 12; cb.Icb.Itemsstemsstems.AddA (“Item 1eme ”); ); cb.cb.Itemstems.AddAdAd.Add(“It(“It(“It( Item 2em ”); ”); cbcb

didd table””)”); cb.Itemms[s[“[“Editaabble”].Selectcteded = true; c; cb.Editable = truue; ccb.ToolTip == “Edi“Ed tablab e CoC mmbo Box”; pageElements.Add(cb); ComboBox cb1 = new Cew omboombbb Box(B x(“cmbmb1nammee”, x + 303,3303, y + y + 40, 150, 20 20); c); cb1.BBb1 ordedederderrColor == R

= F== ont.HHHelveticca;a; ca; cbb1.FontSnt ize = 122; cb1.Itemss.AAdd(“IItem 1”); ccb1.Ittems.Add(“It“Item 2em ”); ”) cb1.cb1.ItItems.Add(“Item 3”); cb1.Items.Add(“Item 4”); cb1.Itemss.AAddd(“Noon-Ediditabtablee”);); c cb1.Items[““[“Non-Non-EditEditableable”].S”].Seelected = tr= ue; ue; cb1.1 Edita

ntnntts.Ads dd(cb(cb(cb1); Converter.CoC nvert(“http://www.gogoogogle.ccom”, “Outputt.pdf”);Convertve er.Cer.Conveonvert(GetDocPath(“DocumentA.rtf”), “Output.pdf”);System.Diaiagnooosticscss.ProoPP cesssess.SStart(“Outptput.pput.pdf”)df”); As; AsyncCncConverterrt aCooCoonnvenverteer = new A

errr((aC(aCo( nverrter_Converted); aConverter.ConversionErroor += nnew ConnversionErrorEvventHtHandler(aConverter_ConversionError); aConverter.Convert(@”C:\tC:\ emmp\mpmm DDocummenmenttAA.rtf”, @”C:\tememmmp\Oup\OutputtputA.pdA.pdf”);f”);) aConverv rter.ter.Coonvert(@”C

verve t(@”C:\temp\DocumentC.rtf”, @”C:\temp\OutputCC.pdf”)); aCoonveerter.Convert(e “hhttp://p://www.yahoo.com”, @”C:\Temp\yahoo.pdf”); ConversionOptionsoni ooptop ionnsnsns = = new CConversiosionOptnOpttionsions(720(720, 72, 720, 72, ttrue); ceeTe.DTe. yynamicPDF

tempte \\ooutput.pdf”, options); ceTe.DynamicPDF.Conveersion.Connvertter.Convert(“C:\\\teemp\\Document2.docx”, “C:\\temp\\output.pdf”, options); string sg ammmpamplmpam eHtmH ml = l “<hth ml><ml><bodybody><p>><p>pp TThis is a very ssimplm e HTML ML strring includ

<tab<t le bborder=\”1\”>1 <tr><td>100</td><td>200</td>”” + “<ttd>3300<</td></tr><tr><<td>>400</td><td>500</td><td>600</t< d></tr></table><></bod/body><y><//</</hhthtmhtmlhtmhtm >”;Conveveo rsion.Con.CoCC nvernverter.ter.CConvvertHtmlString(saamplempleHtmll, “C“C:\\\temp\emp\\Sam\Sam

ererNamee”, Path.Combo ine(GetPath(), “LetterPortrait.pdff”)); prrintJoob.DDocumentNamee = “LettLetter Pe ortrait”; if (printJob.Pob. rinter.Color) prinprinprinprinpri tJobtJob P.PrintOpntOn tions.Cos. lor o = trtrue; ue; if (if (prinprinri tJobtJo .Printer.ColC late) printJob.Pb.P.PPrrintr OOptiOptip ons.ons.onons CoCoollollate at = tr= tr= uurr

innt

;ppd

t:: ,

Untitled-3 1 12/7/10 3:43 PM

50 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

Exp

ress

ion

T

rees

</Language Lab>{ C # C O R N E R }

execute it and make sure the result (the value of the parameter) isn’t null. If it’s null, we’ll throw an exception and include the parameter name from the expression tree instead of hardcoding it:

public static class Validator

{

public static void ThrowIfNull(Expression<Func<object>> expression)

{

var body = expression.Body as MemberExpression;

if( body == null)

{

throw new ArgumentException(

"expected property or field expression.");

}

var compiled = expression.Compile();

var value = compiled();

if( value == null)

{

throw new ArgumentNullException(body.Member.Name);

}

}

}

And now we have a simple, standardized null parameter validation to be used throughout our code. We can now change our old validation logic to use this new code:

public DataFileParser(string filename, IColumnNames names) : base(filename)

{

Validator.ThrowIfNull(() => filename);

Validator.ThrowIfNull(() => names);

}

Could we make this validation even more generic? Why not just define an expression that represents a Func<bool> lambda and the Boolean represents the pass/fail result of our validation logic? Let’s tweak our earlier example a bit and create a new validation method:

public static void Validate(Expression<Func<bool>> expression) {

var body = expression.Body as MemberExpression;

if (body == null)

{

throw new ArgumentException (

"expected property or field expression.");

}

var compiled = expression.Compile();

var value = compiled();

if (!value)

{

throw new ArgumentException (

"Argument failed validation", body.Member.Name);

}

}

This seems like a more complete validation technique that could handle more situations. For example, let’s make sure our “filename” parameter isn’t null, isn’t empty and begins with the letter “C”:

Validator.Validate(() => filename != null

&&filename.Length> 0

&&filename.StartsWith("C"));

But if we run this code, we’ll get the error “expected property or field expression.” The lambda is now much more than just a field

access (MemberExpression). It’s a LogicalBinaryExpression. The contents of this binary expression could be anything. As long as it returns a bool, it’s a valid lambda. But because it can contain anything, pulling out just a parameter name becomes virtually impossible.

Therefore, if you want to build up a generic validation library using expressions, create methods that can perform the validation within the library, not within the lambda. An example of this is checking for a null or empty string. We could expand on our original ThrowIfNull and create a new ThrowIfNullOrEmpty. This one will compile the lambda into a string value and then validate it:

public static void ThrowIfNullOrEmpty(Expression<Func<String>> expression) {

var body = expression.Body as MemberExpression;

if (body == null)

{

throw new ArgumentException(

"expected property or field expression.");

}

var compiled = expression.Compile();

var value = compiled();

if (String.IsNullOrEmpty(value))

{

throw new ArgumentException(

"String is null or empty", body.Member.Name);

}

}

You could add more methods for range validation, set validation and so on. The sample code included with this article contains a range validator.

Expression Trees and the Entity FrameworkI hope this has shown you some of the power that can be achieved using expression trees to examine lambdas. If it hasn’t, you may want to check out the Microsoft Entity Framework. This O/R mapper relies heavily on expression trees. The Entity Framework will expose your database as a set of classes; the properties of those classes represent columns and relationships. You can write standard LINQ to query those classes:

var highBalanceNames = context.Customers

.Where(c =>c.Balance> 2000)

.Select(c =>c.Name);

Those lambdas represent expression trees. The Entity Framework will “take apart” the expression trees using the same techniques shown in this article and translate what the lambda represents into actual T-SQL code! So with only basic knowledge of the Entity Framework (or LINQ to SQL or LLBLGenPro, among others), you can write simple, easy-to-read LINQ queries and let the library do the conversion to SQL, execute the results and return them in a standard format. VSM

Patrick Steele is a senior software consultant with SRT Solutions. A recognized expert on the the Microsoft .NET Framework, he’s a Microsoft MVP award winner and a presenter at conferences and user group meetings. 

GO ONLINEGo to VisualStudioMagazine.com/Steele0411 to read and download the sample code for this article.

VisualStudioMagazine.com · April 2011 · VISUAL STUDIO MAGAZINE 51

Win

dow

s P

hon

e 7

</Language Lab>

Intro to Windows Phone 7New VSM columnist Nick Randolph shows how to build a YouTube search app using Visual Studio 2010 and Expression Blend.BY NICK RANDOLPH

Welcome to a brand-new column dedicated to Windows Phone 7 development. In the coming months we’ll step through the major features of the latest mobile platform from Microsoft. You’ll learn how to work with both Visual Studio 2010 and Expression Blend 4 to develop and design applications for Windows Phone 7.

The best place to go to get started building Windows Phone 7 applications and games is create.msdn.com. From this site, the first things you’ll need are the Windows Phone Developer Tools—follow the link on the main page to download and install the latest release. If you don’t already have either Visual Studio or Expression Blend, the installer will download and install the Express versions of these tools.

Let’s get right to it with a quick example application—a YouTube search application—that offers a 10,000-foot view of designing and developing a Windows Phone 7 program. Because you’ll be switching between Visual Studio and Expression Blend, I’ll note the when we move to a different tool by placing either [Visual Studio 2010] or [Blend] in front of the paragraph where discussion of the new tool starts. This will indicate which application you should work in.

Before we get started, you’ll need to ensure you have the necessary tools. Go to create.msdn.com and follow the link to download the free tools. If you already have the professional versions of Visual Studio or Expression Blend, this will install the SDK components into these products. Alternatively, as I’ve already mentioned, if you don’t have one of these products, an Express version will be installed for use in building Windows Phone 7 applications.

Designing the Search Application[Visual Studio 2010] To get started building a Windows Phone 7 application, open Visual Studio and select File | New | Project. This will open the New Project dialog where you can select the Windows Phone Application project template. Give the project a name, YouTubeSearch, and click OK to proceed.

You’ll most likely see the first page of your application displayed in the designer, along with the corresponding XAML. I’m not a fan of the Visual Studio Designer, so I’ve chosen to always open XAML files in XAML-only mode. If you want this to be your default, just check the “Always open documents in full XAML view” option in the Options dialog (Tools | Options menu item, followed by selecting the Text Editor | XAML | Miscellaneous node on the tree). Rather than trying to guess what the page looks like while you edit XAML, open Expression Blend by right-clicking the YouTubeSearch project node in the Solution Explorer tool window and selecting “Open in Expression Blend.”

[Blend] You’ll notice that the application starts with a default Windows Phone 7 look,

which includes the application title and the name of the page. Select each of these TextBlock elements by clicking them in the designer, and change the Text property in the Properties tool window to match Figure 1.

Next, add a TextBox to accept user search terms, a Button to submit the search and a ListBox to display the search results. Each of these can be found in the Assets tool window. You can either expand out the tree on the right of this tool window, or you can type the name of the control you want to locate in the search box at the top of the tool.

Find, select and drag a TextBox, a Button and a ListBox onto the design surface. Arrange the controls so that they make good use of the screen real estate (you can see an example of a layout in Figure 2on p. 52). The Background of the ListBox has been set to a solid color, #33FFFFFF, so that it can be seen against the Background of the application. You’ll also notice that the Text property of the TextBox has been cleared and that the Content property of the Button has been set to Search. The controls have also been given names so that they can be referenced from code: SearchText, SearchButton and ResultsList, respectively.

The only design work that’s left is to specify how each of the search result elements will appear in the ListBox. This is done by altering the ItemTemplate for the ListBox. Rather than doing this blindly and guessing at how the search results will appear, you can create and use sample data within Blend. From the Data tool window, select “New Sample Data …” from the drop-down menu.

Give the new sample data a name, YouTubeSampleData, and uncheck the “Enable sample data when application is running” option, as you’ll be using real search results at run time. Clicking OK will create some initial sample data that consists of a property called Collection that exposes an array of objects with two properties: Property1, which is a String, and Property2, which is a bool. Double-click each of these elements to change the property name, and click the down arrow on the far right of each property to change the property type. Each of the array items has a property, a Title that’s a string, and another property called VideoImageUrl that’s an Image. The Location attribute of the VideoImageUrl property has been set to a folder that contains a number of images. These images will be used to populate the sample data.

Now that you have some sample data to work with, it’s just a matter of dragging the SearchResults node from the Data tool window onto the ListBox in the designer. As you drag this node across you should see a prompt indicate that Blend will be data binding the ItemsSource property of the ListBox with the SearchResults property. Complete the data binding by releasing

the drag while hovering over the ListBox.You should now have a list that contains

alternating text and images, where each pair represents an item in the list. To improve the layout, right-click on the ListBox in the designer and select Edit Additional Templates | Edit Generated Items (ItemTemplate) | Edit Current. If you look in the Objects and

{ MOBILE CORNER }

Figure 1. The TextBox design in Expression Blend.

52 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

Win

dow

s P

hon

e 7

</Language Lab>{ MOBILE CORNER }

Timeline tool window you can now see that the ItemTemplate is made up of a StackPanel in which there’s a TextBlock and an Image control. In this case we want to use a Grid instead of a StackPanel, so right-click the StackPanel and select Change Layout Type | Grid. This will allow you to rearrange the TextBlock and Image so that they’re alongside each other.

Each change you make in the designer is modifying the XAML file for the page you’re editing. To view the XAML that’s being generated, change the layout of the document area to Split View. Select Active Document View | Split View from the View menu. Now, as you select different elements in the design area, you’ll notice that the corresponding item is highlighted in the XAML area. The XAML for the ItemTemplate should be similar to the following:

<phone:PhoneApplicationPage.Resources>

<DataTemplate x:Key="SearchResultsItemTemplate">

<Grid Margin="0,0,0,30">

<TextBlock Text="{Binding Title}" VerticalAlignment="Top"

HorizontalAlignment="Left" Width="355" Margin="100,0,0,0"

TextWrapping="Wrap"/>

<Image Source="{Binding VideoImageUrl}" HorizontalAlignment="Left"

Width="100" Height="100" Stretch="UniformToFill"/>

</Grid>

</DataTemplate>

</phone:PhoneApplicationPage.Resources>

The last change we’ll make is to increase the size of the font in the TextBlock. Rather than explicitly setting the font size, we’ll make use of the built-in styles that are available to all Windows Phone 7 applications. Right-click the TextBlock in the Objects and Timeline window and select Edit Style | Apply Resource | PhoneTextLargeStyle.

Adding the Logic[Visual Studio 2010] With the design of the page complete, it’s time to head back to Visual Studio and add the logic that will perform the search. In the MainPage.xaml file, locate the

XAML that defines the button and add an event handler for the Click event:

<Button x:Name="SearchButton" Content="Search" HorizontalAlignment="Right"

VerticalAlignment="Top" Click="SearchButton_Click"/>

Right-click in the middle of the text “SearchButton_Click” and select Navigate to Event Handler. This will open the MainPage.xaml.cs file and create the event handler, SearchButton_Click. YouTube exposes a REST API that can be used to search for videos. In this case you’re going to search for videos that contain the text typed into the TextBox, and you’re going to restrict the search to videos that are supplied in MP4 format (format=6). The following code uses an instance of the WebClient class to download the XML search results:

private void SearchButton_Click(object sender, RoutedEventArgs e){

var wc = new WebClient();

wc.DownloadStringCompleted += DownloadStringCompleted;

var searchUri = string.Format(

"http://gdata.youtube.com/feeds/api/videos?q={0}&format=6",

HttpUtility.UrlEncode(SearchText.Text));

wc.DownloadStringAsync(new Uri(searchUri));

}

Before writing the logic to handle the XML search result, you’re going to need an object model that will represent each search result. You’ll parse the XML search result into an array of these objects, which in turn can be connected to the ListBox you designed earlier. The following YouTubeVideo class exposes both the Title and VideoImageUrl properties that match the sample data created previously:

public class YouTubeVideo{

public string Title { get; set; }

public string VideoImageUrl { get; set; }

public string VideoId { get; set; }

}

[Visual Studio 2010] Now all that remains is to parse the XML search results. To use XLinq to parse the XML, you’ll need to add a reference to System.Xml.Linq (select Add Reference from the Project menu). The following code parses out an array of YouTubeVideo objects from the XML search results; it then applies this to the ItemsSource property of the ResultsList ListBox:

void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)

{

var atomns = XNamespace.Get("http://www.w3.org/2005/Atom");

var medians = XNamespace.Get("http://search.yahoo.com/mrss/");

var xml = XElement.Parse(e.Result);

var videos = (

from entry in xml.Descendants(atomns.GetName("entry"))

select new YouTubeVideo{

VideoId = entry.Element(atomns.GetName("id")).Value,

VideoImageUrl = (

from thumbnail in entry.Descendants(medians.GetName("thumbnail"))

where thumbnail.Attribute("height").Value == "240"

select thumbnail.Attribute("url").Value).FirstOrDefault(),

Title = entry.Element(atomns.GetName("title")).Value}).ToArray();

ResultsList.ItemsSource = videos;

}

From the drop-down in the Standard toolbar, select either Windows Phone 7 Device, if you have an actual Windows Phone 7 device to debug on, or Windows Phone 7 Emulator. Press F5 or click the Start

Figure 2. The basic layout.Figure 3. The YouTube search results.

{ MOBILE CORNER }

Debugging button on the Standard toolbar to run the project. After entering a search term and clicking the Search button you should see results populate the ListBox, as shown in Figure 3 (p. 52).

Ready for LaunchThe last thing to do is to launch the YouTube video when the user taps on one of the items in the ListBox. To do this you need to wire up an event handler to the SelectionChanged event on the ListBox. Open MainPage.xaml and locate the XAML for the ListBox. Update the XAML to include the SelectionChanged event handler, as shown in the following code:

<ListBox x:Name="ResultsList" Margin="0,76,0,0" Background="#33FFFFFF"

ItemTemplate="{StaticResource SearchResultsItemTemplate}"

ItemsSource="{Binding SearchResults}" SelectionChanged="VideoListSelecti

onChanged" />

Right-click in the middle of the VideoListSelectionChanged text and select Navigate to Event Handler. Update the event handler with the following code, which strips out the video identifier from the VideoId and combines it with the vnd.youtube protocol prefix:

private void VideoListSelectionChanged(object sender, SelectionChangedEventArgs e)

{

var video = ResultsList.SelectedItem as YouTubeVideo;

if (video != null){

var parsed = video.VideoId.Split(‘/’);

var id = parsed[parsed.Length - 1];

var playbackUrl = "vnd.youtube:" + id;

var task = new WebBrowserTask { URL = playbackUrl };

task.Show();

}

}

The last part of this code uses the WebBrowserTask to launch the YouTube video in the YouTube application on the device. Actually, what happens is that Internet Explorer is launched and it attempts to load the supplied URL, which in this case starts with vnd.youtube. Internet Explorer realizes that this isn’t a protocol it can handle, so it passes it off to the OS, which knows to load it in the YouTube application. Run the application again and you’ll notice that Internet Explorer appears briefly before YouTube is launched and the video is played.

The first time you run this you’ll be prompted to download and install the YouTube application from the Windows Marketplace. Unfortunately, the emulator doesn’t support installing applica-tions from the Windows Marketplace, requiring an actual device in order to see the video play.

This has been a whirlwind tour of a Windows Phone 7 project. Hopefully you’ll join me in the coming months as we dive deeper into this exciting space. Also, watch the Visual Studio Magazine Web site, where I’ll be writing frequent columns about building better Windows Phone 7 applications and games. VSM

Nick Randolph runs Built to Roam, a consulting company that specializes in training, mentoring and assisting other companies in building mobile applications. With a heritage in rich-client applications for both the desktop and a variety of mobile platforms, Randolph currently presents, writes and educates on the Windows Phone 7 platform.

GO ONLINETo download the sample code for this article, go to VisualStudioMagazine.com/Randolph0411.

VISUAL STUDIO LIVE! LAS VEGAS TRACKS

Visual Studio Live! Pre-Conference Workshops: Monday, April 18, 2011 (Separate entry fee required)

MWK1 An Introduction to Multi-Platform Mobile Development Using C#: iPhone, Android, and Windows Phone 7 Ken Getz & Brian Randell

MWK2 Workshop: Making Effective Use of Silverlight and WPF Billy Hollis & Rockford Lhotka

MWK3 Workshop: Programming with WCF in One Day Miguel Castro

Visual Studio Live! Day 1: Tuesday, April 19, 2011

T1 Easing in to Windows Phone 7 Development Walt Ritscher

T2 Getting Started with ASP.NET MVC Philip Japikse

T3 Azure Platform Overview Vishwas Lele

T4 Best Kept Secrets in Visual Studio 2010 and .NET 4.0 Deborah Kurata

T5 Silverlight in 75 Minutes Ken Getz

T6 Test Driving ASP.NET MVC2 Philip Japikse

T7 Building Azure Applications Vishwas Lele

T8 How We Do Language Design at Microsoft Lucian Wischik

TCT1 Chalk Talk: Silverlight, WCF RIA Services, and Your Business Objects Deborah Kurata

TCT2 Chalk Talk: Building N-Tier Applications With Entity Framework 4 Leonard Lobel

TCT3 Chalk Talk: Join the XAML Revolution Billy Hollis

T9 Transitioning from Windows Forms to WPF Miguel Castro

T10 HTML5/IE9 inspire T11 Building Compute-Intensive Apps in Azure Vishwas Lele

T12 Turn Your Development Up to 11: Debugging to Win with Visual Studio 2010 Brian Randell

T13 Programming for Windows 7 with WPF Miguel Castro

T14 Improving Your ASP.NET Application Performance with Asynchronous Pages and Actions Tiberiu Covaci

T15 Using C# and Visual Basic to Build a Cloud Application for Windows Phone 7 Lucian Wischik & Srivatsn Narayanan

T16 Designing and Developing for the Rich Mobile Web Joe Marini

Visual Studio Live! Day 2: Wednesday, April 20, 2011

W1 Bind Anything to Anything in Silverlight and WPF Rockford Lhotka

W2 HTML 5 and Your Web Sites Robert Boedigheimer

W3 How to Make Your Application Awesome with JSON, REST, WCF and MVC James Bender

W4 Visual Studio LightSwitch—Beyond the Basics Robert Green

W5 Design, Don't Decorate Billy Hollis

W6 Styling Web Pages with CSS 3 Robert Boedigheimer

W7 RESTBuilding RESTful Services in the Microsoft Platform: When to Use What? Jesus Rodriguez

W8 Advanced LightSwich Development Michael Washington

WCT1 Chalk Talk: CSLA .NET Rockford Lhotka WCT2 Chalk Talk: Busy Developer’s Guide to (ECMA/Java)Script Ted Neward

W9 Leveraging the MVVM Pattern in Silverlight, WPF and Windows Phone Rockford Lhotka

W10 HTML5 Messaging, Web Workers and Web Sockets with JavaScript Jeffrey McManus

W11 WCF Workflow Services Rob Daigneau

W12 The Almighty @— A Razor Primer Charles Nurse

W13 Top 7 Lessons Learned On My First Big Silverlight Project Benjamin Day

W14 jQuery Application Development Jeffrey McManus

W15 WCF Tips & Tricks – From the Field Christian Weyer

W16 WebMatrix Real World Data-Centric Applications Charles Nurse

Visual Studio Live! Day 3: Thursday, April 21, 2011

TH1 Multi-touch Madness! Brian Peek TH2 The Best of jQuery Robert Boedigheimer

TH3 Making WCF Simple: Best Practices for Testing, Deploying and Managing WCF Solutions in the Big Enterprise Jesus Rodriguez

TH4 Digging Deeper in Windows Phone 7 Walt Ritscher

TH5 XAML Primer Clarifying the UI Markup Language Walt Ritscher

TH6 Single Sign-On for ASP.NET Applications Dominick Baier

TH7 How to Take WCF Data Services to the Next Level Rob Daigneau

TH8 C# on Android: Building Android Apps with .NET Christian Weyer

TH9 Silverlight Security Dominick Baier

TH10 The Scrum vs. Kanban Cage Match Benjamin Day & David Starr

TH11 Busy .NET Developer's Guide to Parallel Extensions for .NET 4 Ted Neward

TH12 C# on Android: Building Android Apps with .NET Christian Weyer

TH13 LINQ Programming Model Marcel de Vries

TH14 Patterns of Healthy Teams using Visual Studio and TFS David Starr

TH15 Designing Applications in the Era of Many-Core Computing Tiberiu Covaci

TH16 XNA Games for Windows Phone 7 Brian Peek

TH17 So Many Choices, So Little Time: Understanding Your .NET 4.0 Data Access Options Leonard Lobel

TH18 Produce Better Quality Code by Leveraging the Visual Test Tools You Never Discovered Before Marcel de Vries

TH19 Building Event-Driven Applications with Microsoft StreamInsight Torsten Grabs

TH20 Windows Azure and PHP Jeffrey McManus

Visual Studio Live! Post-Conference Workshops: Friday, April 22, 2011 (Separate entry fee required)

FWK1 Architectural Katas Workshop Ted Neward FWK2 SQL Server 2011 Andrew Brust & Leonard Lobel

Silverlight/WPFProgramming

Practices

Visual Studio 2010/ .NET 4

WCF Cloud ComputingData

ManagementWeb/

HTML 5"Simplification"

ToolsMobile

Development

AGENDA

VISIT US ONLINE AT VSLIVE.COM/LV FOR DETAILS ON SESSIONS, SPEAKERS, AND MORE!

Untitled-8 2 3/7/11 2:09 PM

APRIL 18–22, 2011LAS VEGAS, NEVADARIO ALL-SUITE HOTEL & CASINO

REGISTER NOW

WITH CODE APRAD

WWW.VSLIVE.COM/LV

CHECK OUT THE FULL 50+ SESSION SCHEDULE NOW!

If you’re looking for hard-hitting .NET development training, look no further than Visual Studio Live! Las Vegas. Our goal? To arm you with the knowledge to build better applications.

JOIN US IN LAS VEGAS THIS MONTH.

DOWNLOAD THE AGENDA NOW!

VISUAL STUDIO LIVE! LAS VEGAS is 5 days packed with full

day workshops, keynotes from industry heavy weights and your choice

of 50 hard-hitting sessions.

You'll learn tips, tricks and fi xes from .NET pros like Billy Hollis,

Rockford Lhotka, Andrew Brust, Deborah Kurata and Dave Mendlen,

Senior Director, Developer Platform and Tools at Microsoft.

SUPPORTED BY:

PRODUCED BY:

Untitled-8 3 3/7/11 2:10 PM

56 VISUAL STUDIO MAGAZINE · April 2011 · VisualStudioMagazine.com

As I write this, this year’s MVP Summit just ended, and before it even started, I knew I’d write this month’s column about it. But it’s hard to write about an event when its content is covered by a non-disclosure agreement. I wasn’t sure how I’d handle this. As it turns out, I learned something important at the Summit that I can share in full. For me, it’s a huge help in analyzing Microsoft in the market. It’s not about technology, though. Rather, it’s about getting older.

Microsoft is at a point in its history where it’s mature and enjoying the spoils of its dominance, but where it’s also fi ghting off stasis. I realized the same is true for a number of MVPs. I might include myself in that category: I was 28 when my fi rst column ran in this magazine, and the column you’re reading now is being published almost exactly on my 45th birthday. This is about more than years of service. It’s about success, coming of age, coping with keeping up, and changing one’s game to stay relevant. That’s true for me, that’s true for other MVPs and it’s true for Microsoft, too.

Report from the FrontMicrosoft’s mature enterprise technologies must continue to be fed and cared for. We need SQL Server to remain a solid relational database. We need SharePoint to keep growing market share. We need Windows Server and all of its SKUs to remain in full force in the datacenter. We need the .NET Framework and Visual Studio to stick around, and we need Team Foundation Server and the rest of the Visual Studio application lifecycle management (ALM) suite to continue their ascent to critical mass. At the MVP Summit, it was clear that each of these efforts is going well. That’s good.

But you can’t just keep dancing to the same music, or you’ll go from having cutting-edge taste to being someone who listens only to oldies. The same is true for Microsoft, and for us. We need to create “apps” on the smartphones people like to carry around, not just applications on PCs they use at work. We need to build software in the cloud, not just on the desktop, or the server in the datacenter. Our databases can’t just do relational storage and query anymore. Instead, we need them to handle data modeling and in-memory analytics—and the releases have to be rapid. In each of

these areas—with Windows Phone, with SQL Server and with Windows Azure—Microsoft is making great strides.

Microsoft also needs to draw on its history. It needs to return to its roots of making line-of-business application development simple, fast and relatively easy. As the Visual Studio LightSwitch product gets closer to release, as awareness of it grows throughout other product groups and as it harnesses the power of new technologies, including Windows Azure, I can’t help but be impressed by the resources of a company with a rich history, a future-facing outlook and the wisdom to combine the two.

In It TogetherI regret being short on specifi cs, but I guess they matter less than the trend anyway. The trend I see is gradual adaptation to the new realities of the industry, both on Microsoft’s part and on the part of the MVPs. In fact, not only are MVPs adjusting, they’re pushing hard on Microsoft to change, and to accelerate the rate of that change. As much as Microsoft is behind on tablet technology, as much of an uphill battle as it faces in the smartphone market, as tough a competitor as Amazon Web Services is to Windows Azure, MVPs new and old are there telling Microsoft to get a move on. I think it’s working.

What Microsoft has done well, perhaps better than any other company in the industry, is deputize its customers and its partners to shape the future of the company. The MVP Summit is perhaps the best example of that foundational practice.

In his keynote at the Summit, Steve Ballmer pointed out that the MVP program has been in existence for almost 20 years. That’s yet another sign of the company’s age, but it’s a byproduct of the timeless value of the program. If Microsoft can transition to meet the technology industry’s new demands, it will be due in no small part to the MVPs and other allies of the company. What makes me optimistic about that is that the reverse is true as well. VSM

Andrew J. Brust is founder and CEO of Blue Badge Insights, an analysis, strategy and advisory firm, as well as CTO for Tallan, a Microsoft National Systems Integrator. Brust is a Microsoft Regional Director and MVP, and coauthor of “Programming Microsoft SQL Server 2008” (Microsoft Press, 2008).

MVP Summit Highlights a Shared Future

BY ANDREW J. BRUST

</Redmond Review>

This is about more than years of service. It’s about success, coming of age, coping with keeping up, and changing one’s game to stay relevant.

Untitled-4 1 3/2/11 4:10 PM

Untitled-11 1 1/7/11 4:08 PM