Click here to Skip to main content
15,879,535 members
Articles / Artificial Intelligence / Machine Learning
Article

Excerpt from Microsoft Visual Studio 2010 Unleashed

Rate me:
Please Sign up or sign in to vote.
4.92/5 (10 votes)
22 Sep 2010CPOL63 min read 71.9K   32   5
This chapter covers the core make-up and capabilities of Visual Studio 2010.

visual-studio-unleashed.jpg

Mike Snell, Lars Powers
Published by Sams
ISBN-10: 0-672-33081-4
ISBN-13: 978-0-672-33081-0

In This Chapter

  • The Visual Studio Product Line
  • Languages, Frameworks, and Application Templates
  • Developing Rich(er) Clients
  • Creating Web Clients
  • Making the Client Choice
  • Coding for the Cloud
  • Working with Data
  • Write Connected, Service-Oriented Solutions

Visual Studio 2010 and the .NET Framework 4.0 introduce many new features to an already full-featured toolset. This latest version is about increasing developer productivity while providing developers with choices and flexibility when building their solutions. If you've been developing for a long time, you've come to expect that a new release of the IDE comes with advanced programming models, more acronyms, a few fresh project types, and improved ways to view and organize code. The 2010 product is no exception. Some highlights for this release include

  • An improved code editor built on WPF
  • Windows 7 development support
  • Improved support for Microsoft Office programming including SharePoint development
  • A better experience on multiple monitors
  • Support for simplified, parallel programming against multi-core devices
  • Support for ASP.NET MVC
  • Support for building Web applications using the latest versions of Silverlight
  • One-click Web deployments
  • Building for the cloud with Windows Azure
  • Major additions and improvements to Windows Workflow and Windows Communication Foundation
  • Increased support for database development on SQL Server and other database technologies such as Oracle and DB2
  • Expanded team development with the new architecture explorer, support for UML, and automated testing improvements built in to the Premium and Ultimate editions

This chapter covers the core make-up and capabilities of Visual Studio 2010. We first help you sort through the product choices available to .NET developers. We then provide a comparison of the .NET programming languages. The remaining sections of the chapter cover the many possibilities open to .NET programmers including building Web, Windows, cloud, data, and service applications. Our hope is to give you enough information in this chapter to get the full picture of what is available to you when you build solutions using Visual Studio.

Note - Part I, "An Introduction to Visual Studio 2010," is broken into three chapters. This chapter provides a snapshot of all things Visual Studio. Chapter 2, "The Visual Studio IDE," is an introduction to getting the tool installed, running it, and creating that first project. It also serves to familiarize you with the basics of the IDE. Chapter 3, "The .NET Languages," is a quick primer on coding constructs in VB and C#. It also covers general programming against the .NET Framework.

The Visual Studio Product Line

With Visual Studio 2010, Microsoft has simplified the product line. There are now only three primary editions of the tool: professional, premium, and ultimate. This should make it easier to understand which tool you need for your development and price point. At a high level, these tools are differentiated as follows (each addition assumes an MSDN subscription, too):

  • Professional—Includes the basic features of the IDE to build applications of all types on all .NET languages. Includes support for writing, debugging, and testing code.
  • Premium—Builds on top of the Professional edition to include many features targeted toward the enterprise developer for testing and verifying code. Also includes database development tools.
  • Ultimate—Includes every tool in the Visual Studio product line as well as surrounding products. It builds on Premium to add additional debugging and testing support, architecture tools, lab management, and more.

Note - You can see a detailed product comparison at http://www.microsoft.com/visualstudio/en-us/products.

Of course, there are some peripheral versions and products that surround Visual Studio. These include the Express editions, Test Professional, and Microsoft Expression. We take a deeper look at all these products in the coming sections.

Express Editions

Microsoft offers the Visual Studio Express Editions on a per-language basis (VB, C#, C++, Web, Windows Phone, and SQL Server 2008 R2). These editions are free, downloadable, low-barrier to entry versions targeted directly at the novice, hobbyist, student, or anyone else looking to write some code without breaking the bank. In conjunction with the Express Editions, Microsoft has made a few development kits available for things such as Facebook and Windows Live. There are also tutorials, videos, support sites, and fun projects to review. To get started, check out the URL http://www.microsoft.com/express/Resources/.

The Express Edition can be seen as Microsoft's answer to all the "freeware" tools available to today's developers. After all, if you are a college student looking to put up a buddy's website, you are more likely to look for a low-cost solution. Of course, five years down the road when you're making decisions for your company, Microsoft wants to be sure you've had a chance to work with its products. The Expression Edition fits this niche nicely. Figure 1.1 shows an example of the Web Platform Installer that is part of the Visual Web Developer 2010 Express installation. This installer enables you to download and install the IDE for creating web applications. In addition, as the figure shows, it enables you to install many base web applications that you can use in your solution, including applications for blogging, eCommerce, content management, and more.

01fig01.jpg

Figure 1.1

The Web Platform Installer for Visual Web Developer 2010 Express enables you to install many application templates to kick-start your projects.

The Express Editions purposely do not have all the power of their professional patriarch (a Class Designer, test-driven development with unit testing, enterprise templates, XSLT support, source code control, and so on). However, they do have full language support (such as LINQ) and access to the .NET Framework items such as WPF.

The Express Editions also have a more streamlined user experience that does not expose the full complexity (or power) of the professional editions. However, developers are to create client/server form-based applications, websites, data-driven solutions, and even web services using these tools.

Note - For more information regarding the Visual Studio Express Editions or to download one of these editions, you can visit Microsoft's site at http://www.microsoft.com/express.

Professional Edition

Visual Studio Professional is the base entry for most developers who make a living writing code. Visual Studio Professional gives you all the language support (including VB, C#, F#, C++); the capability to write all types of applications, including Console, Windows, Windows Services, Class Libraries, Web, Cloud, WPF, Silverlight, Database, Office, SharePoint; and more.

Visual Studio Professional also gives you access to many additional tools that are key to building professional applications. The following features ship only with Visual Studio Professional (and above):

  • Unit Testing and Test-Driven Development
  • Access to Team Foundation Server (TFS) for source control, continuous integration, and work item tracking
  • Office development including SharePoint
  • Multi-core, parallel development
  • Class Designer
  • Server Explorer
  • Refactoring
  • And much more

Note - This book targets Visual Studio Professional only. A quick perusal of the book will allow you to see the depth and breadth of what you can do with this powerful tool.

Premium

Visual Studio 2010 Premium is targeted toward professional developers who build both corporate and commercial applications. It provides additional tools that help developers verify, test, and check their code against common issues. It also includes all the features inside Professional plus the following:

  • Static code analysis
  • Code metrics
  • Code profiling
  • Unit test code coverage and test impact analysis
  • Performance profiling
  • Coded UI tests
  • Database change management, unit testing, data generation, schema and data comparison

Note - Visual Studio 2010 Premium combines the features of the Developer and Database role–based products from the prior edition (2008).

Ultimate

Visual Studio 2010 Ultimate is for developers who really need it all. It includes additional testing tools targeted toward both the technical and manual tester. It also provides architecture tools for creating UML models and exploring code visually. This version of the product is the everything-but-the-kitchen-sink option in a single package. The following highlights the Ultimate features:

  • Historical debugging with IntelliTrace
  • UML modeling support for use case, class, sequence, component, and activity diagrams (including generating sequence diagrams from code)
  • Architecture Explorer, for coming up to speed on a code base
  • Layer diagrams
  • Web, load, and stress testing
  • Test Professional, including test case management, manual testing, and test record and playback
  • Test lab management

Note - Visual Studio 2010 Ultimate is similar to the prior edition, 2008 Team Suite. However, it adds even more features centered around architecture and testing.

MSDN

All editions of Visual Studio (above Express) now come with some form of an MSDN subscription. Depending on your edition, you get a little more of MSDN with each version up the product line. The following provides an overview of your MSDN benefit by product:

  • 2010 Professional—Includes a license for TFS, a client access license for TFS; 50 hours per month of Azure hosting; development editions of Windows 7, Windows Server 2008 R2, and SWL Server 2008; access to prior versions of Windows and SQL; two technical support incidents; priority forums; e-learning access; and MSDN magazine.
  • 2010 Premium—Includes everything in Professional with MSDN plus the following: 100 hours per month of Azure hosting; Expression Studio; Office 2010 Plus, Visio, and Project; development editions of Office, Dynamics, SharePoint, Windows embedded, and other Microsoft platform and server products; and four technical support incidents.
  • 2010 Ultimate—Includes all the preceding in Premium with the following extras: 250 hours per month of Azure hosting and access to Team Explorer Everywhere.

Note - Microsoft also sells a version of 2010 Professional with MSDN essentials. This is a limited MSDN version and does not include things like TFS access, SQL Server, Office, technical support, and MSDN magazine.

Related Tools

A number of additional tools outside Visual Studio surround the Windows development experience. This includes tools targeted toward the development team (Team Foundation Server and Team Explorer), those targeted toward designers (Microsoft Expression), and those targeted toward the tester (Test Professional). It's important to be aware of how these tools can help you build your next solution. This section provides a brief overview of each of these products.

Team Foundation Server (TFS)

TFS is the central hub that provides an integrated, application lifecycle management (ALM) experience around the many development tools from Microsoft. The first version of TFS was built with the release of 2005. This included source control, a centralized project management system, build automation, and reporting. By all accounts, these tools have been a great success. Microsoft has built upon this with the release of TFS 2010. Figure 1.2 shows TFS in relation to the many products that surround the ALM practices.

01fig02.jpg

Figure 1.2

Team Foundation Server acts as the hub that provides ALM services to the many development products available from Microsoft.

As you can see from the figure, TFS is at the center of development and ALM coordination. The following highlights the many services provided by TFS:

  • Process Guidance/Template—TFS includes two process templates out of the box: Microsoft Solutions Framework (MSF) for CMMI 5.0 and MSF for Agile 5.0. Both provide a set of work items, workflow, and reporting based on their specific methodology. Both also provide guidance to the team for executing key activities on the project (such as requirements management or build automation). The CMMI template is targeted toward those teams that have a strong need for auditing and traceability and might be looking to achieve a CMMI-level assessment. The MSF for Agile template provides tools for teams implementing a SCRUM approach to development.
  • Project Management—TFS enables project managers to define their projects in terms of iterations and functional areas. It provides work items that are used to define, assign, and track work on the project. A work item can be a task on the project, a requirement, a bug, a test scenario, and so on. In general, a work item represents a generic unit of work on the project. Of course, work items are customizable and can have states, new fields, and business rules associated with them. Work items play a central part in ensuring project team communication and reporting. Project managers can use the Excel and Project add-ins to Office to manage the work items on a project.
  • Requirements Management—TFS provides specific work items for managing requirements. In fact, 2010 introduces the concept of hierarchical work items. This means you can create work item children. For example, you might create a requirement work item and then define the tasks required to build that requirement. You might also define the test cases that will be used to verify the requirement. In this way, TFS enables rich reporting through the aggregation of the child work item data (such as tests passing for a requirement or work remaining at the requirement level).
  • Test Case Management—TFS and Test Professional enable work items specific to test planning and test case management. You can define a set of test cases for a given requirement. Each test case can define the steps required to execute the test case along with the expected results.
  • Version Control—The source control features in Team Foundation Server include enterprise-class features such as change sets, shelving, automatic build rules, the capability to associate work items to changed source, parallel development, a source control policy engine, branching, checkpoints, and more. The 2010 edition includes branch and changeset visualization.
  • Build Automation—The TFS build tools allow for automatic, scheduled, and on-demand builds. Builds are reported against, documented, automatically tested, and analyzed for code coverage and churn (as an example). The build engine in 2010 has been rewritten to leverage Windows Workflow (WF) 4.0.
  • Reporting—TFS provides a rich set of reports for tracking statistics and the overall health of your project. Reports include those built on SQL Reporting Services (that are accessible from the IDE, the Web, and SharePoint) as well as a new set of Excel reports for working directly with the data.
  • Collaboration and Dashboards—TFS includes a SharePoint site for each project. This site provides full access to manage and create work items outside Visual Studio and Office. In addition, it provides a set of dashboard reports for quick analysis of what is happening on the project as well as items specific to the logged-in user. Of course, the SharePoint sites also provide rich collaboration around documents and through a team wiki.
  • Web Access—TFS provides a web access version for working with work items over the web (outside SharePoint). This version also includes source control access through a web browser. The web access version, however, does not include the richer, dashboard features of the SharePoint TFS sites.
  • Integration with other IDEs—TFS is accessible from Visual Studio, Office, SharePoint, and the Web. In addition, there is Team Explorer Everywhere for accessing the TFS features using other IDEs running on operating systems outside Windows. This includes the eclipse IDE and the MacOS.

Team Explorer

There are those team members who will not have a development tool such as Visual Studio, Test Professional, or Expression that provides access to TFS. In this case, they can get full access through Team Explorer. Team Explorer is targeted at project managers, business analysts, directors, and others on the team who need to access TFS but do not do direct development. This tool is purchased as a client access license (CAL). It includes a basic explorer, the Excel and Project add-ins, and full access to the SharePoint site, Web access, and reporting.

Test Professional

Visual Studio Test Professional 2010 provides test planning, test case management, and manual testing for those people dedicated to the testing role. This is a separate tool that should seem comfortable and familiar to testers. Test plans are created based on application requirements (to provide traceability). Test cases are created and tracked as work items.

When a tester runs a test plan, they work through each test case and each step in the test case. They indicate the success or failure of the given test. For failures, they can log bugs (also work items). The bugs can automatically include things such as diagnostic trace information, event log data, network information, and even video recording of the steps the tester was executing when the bug was found.

Test Professional also enables testers to create action recordings of their steps. These recordings can be played back to execute the same steps again. This helps automate many portions of manual tests.

Finally, Test Professional includes lab management, which is a suite of tools for provisioning test environments from a template. These environments are virtual machines that are meant to be set up and torn down as needed for testing. You also can create checkpoint environments for various builds.

Visual Studio Agents 2010 and Load Test Virtual User Packs

Visual Studio Ultimate is used to create web, load, and stress tests; however, when executing these tests, it is limited to simulating up to 250 simultaneous users. Sometimes you need to generate much more load for your tests. In this case, you need to purchase Visual Studio Load Test Virtual User Pack 2010.

This tool is a separate product that works with Visual Studio Agents 2010 (included with Ultimate) to generate massive loads for various load-testing scenarios. You use the VS Agents software to set up a central test controller. You can then put agents on different machines. You add virtual user packs to each agent (depending on how many users you want to simulate per machine). Each user pack you purchase enables you to simulate an additional 1,000 users. The controller then kicks off the agents. The agents report their data back to the controller, where things are centralized and aggregated for reporting.

The Expression Tools

A sister product line to Visual Studio is Microsoft Expression tools. These tools are targeted toward designers who build applications on the Microsoft platform. Microsoft Expression tools offer rich design experiences for building Web, Windows, and Silverlight applications.

The Expression tools are also built to enable workflow between designers and developers; that is, a designer can open a solution and work on the design without affecting the code a developer might be working on (and vice versa). In the latest version, the tools can also integrate into TFS for source control and work item tracking.

It is important that you are aware of these tools so that you know where you might use them, and also because they work with Visual Studio projects and project files by offering some similar capabilities (but in different ways). The following is a high-level overview of these tools:

  • Expression Web—A design tool for creating ASP.NET Web Forms and websites that comply with CSS standards.
  • Expression Blend—A tool for designing user interfaces based on XAML and Silverlight.
  • Expression Design—A tool that enables a designer to create vector-based illustrations that include drawing, text, and more.
  • SketchFlow—A tool for creating fast prototypes before moving forward with a solution.
  • Expression Encoder—A tool for encoding video and audio and publishing it to Silverlight applications.
  • Expression Media—A tool for organizing and managing design assets (files) into visual catalogs.
  • Expression Studio—The full set of Expression tools for designers who need it all.

Note - For more information on the Expression products (which are not covered further in this book), see http://www.microsoft.com/expression/default.aspx.

Languages, Frameworks, and Application Templates

Programming in Visual Studio and the .NET Framework means you have a variety of languages from which to choose. .NET was built to be somewhat language agnostic. The common type system (CTS), the common language specification (CLS), the common language runtime (CLR), and the Framework all work to abstract language from .NET and provide developers with choice. Once deployed and compiled, applications written on .NET are very similar in runtime execution.

Programming Language Choices

What should be important to developers is selecting a language that allows you to be productive and has a high degree of support inside the IDE. Productivity is about developing with syntax that is familiar and logical to you. IDE support means the tools can generate code, help you write code, and provide features and artifacts that accelerate your coding. This is where many third-party (non-Microsoft-supported) languages often fall short. It takes a lot to provide IDE support to build the many application types Visual Studio enables.

The following provides an overview of the Microsoft-supported languages for .NET development with Visual Studio:

  • Visual Basic—The VB implementation in Visual Studio is about productivity. Developers can rapidly build type-safe, object-oriented applications. Although VB developers have full access to all code constructs in .NET, they tend to use VB because of the productivity features inside the IDE and they are already familiar with it from past experience with VB (or a similar language built on basic).
  • Visual C#—Visual C# is a programming language designed for those who are familiar and comfortable programming in C-style languages (such as C, C++, and Java). C# is type-safe, object-oriented, and targeted for rapid application development. C# developers tend to spend more of their time inside the Visual Studio code editor and less time with the designers.
  • Visual C++—With C++, developers can build .NET managed applications. However, they can also create Windows-based applications that do not rely on .NET. Most C++ developers have a C++ background and are therefore more comfortable inside the C++ world than they are with other languages. A C++ developer also has access to build against Active Template Library (ATL), the Microsoft Foundation Class (MFC) libraries, and the C Runtime Library (CRT).
  • Visual F#—The F# language is said to be multiparadigm because it allows for functional, object-oriented, and imperative programming. F# is now a core language inside of Visual Studio 2010. It brings .NET developers a solution to many difficult programming problems. There are many features of F# including lightweight function types, functions as values, function composition and pipelining, recursive functions, and lambda expressions, to name a few. F# makes for simpler programming of math, scientific, engineering, and symbolic analysis (such as machine learning) problems.
  • JScript 8.0—A scripting language for .NET. This language is Microsoft's implementation of the ECMA standard for compliance. You can use this language to write a variety of applications including Windows and Web applications. On the Web, you can target both server-side as well as client-side processing. JScript 8.0 adds compilation and full object-oriented supports (typed variables, early and late binding, inheritance, overloading, and more) to JScript. It also now allows access to the .NET Framework.

Note - The basics of programming the languages of .NET are covered in Chapter 3.

Note - If you are familiar with one language but need to program in another (or translate), search for "Keywords Compared in Various Languages" on MSDN (http://msdn.microsoft.com).

The .NET Framework(s)

The .NET Framework represents the base classes, libraries, and useful functions that make programming in .NET so productive. The classes and functions found in the .NET Framework provide the majority of common features you need as a developer. Thanks to the CTS, each language can take advantage of this single Framework of functionality. Framework features include file I/O, web, workflow, collections, Windows, communication, and much, much more.

Of course, as the .NET languages evolve so does the Framework. However, to maintain backward compatibility, each version of the Framework remains as a separate entity. There are now at least six versions of the .NET Framework: 4.0, 3.5, 3.0, 2.0, 1.1, 1.0. There are also various versions of the .NET Compact Framework (used for mobile devices). There is even a Micro Framework for very small, resource-constrained devices (http://www.microsoft.com/netmf).

Versions 3.0/3.5 of the .NET Framework brought WPF, WCF, and WF support along with modifications for Windows Vista. The latest version (4.0) ships with Visual Studio 2010 and brings support for dynamic languages (dynamic language runtime or DLR) such as Ruby, profiling running applications, new types such as Tuple and BigInteger, a revised Windows Workflow (WF) engine, isolated storage improvements, and a whole lot more. Chapter 3 discusses many of these Framework elements in greater detail.

Note - See Chapter 2 for details on how you can target a specific version of the .NET Framework inside of Visual Studio 2010.

The Many Faces of a .NET Application

.NET has become the standard when building applications targeting the Microsoft Windows client or server products. It took only a few years, but it is now fair to say that .NET is everywhere; Windows programming and .NET programming are now synonymous. Many of the user applications we interact with have some if not all of their code base in .NET. This includes rich clients built on Windows, solutions built on Office (including parts of Office itself), smart clients that work across the Web, Web applications that run in a browser and execute on a Windows server, and mobile applications that run on devices such as phones. The good news is that the .NET developer is in high demand, and you can leverage your skills to target a wide audience across an array of user experiences.

Figure 1.3 shows the New Project dialog in Visual Studio 2010; it serves as an example of the myriad user solutions that are possible with .NET. This graphic cannot fit all of the possibilities available to you, but it does illustrate that Windows, Web, Silverlight, Office, SharePoint, and many other project types are within the reach of .NET developers working with Visual Studio.

01fig03.jpg

Figure 1.3

The many application faces made possible by Visual Studio 2010.

As discussed, you have many project templates available for your next solution. What is needed, however, is some sort of road map with respect to user experience. Choosing the right project template is an important part of making the delivery of your solution successful. The following list is meant to provide an overview of the core presentation technologies available to the .NET developer:

  • Windows Forms Application (WinForm)—Windows form applications are used to deliver business applications and tools built on the Windows platform. You typically select a WinForm application template when you need to build a solution that leverages the resources on the user's machine. This means the application is installed and users expect it to run more responsively than the typical Web application. WinForm applications can be stand-alone or data-driven (often client-server). WinForm applications might connect to web services and work in both connected an unconnected scenarios. Note that WinForm applications are being de-emphasized in the current versions of Visual Studio. If you're building a new application, you should consider WPF instead of WinForm.
  • WPF Application—Windows Presentation Foundation (WPF) combines features of WinForms with XAML and 3D capabilities to allow you to create the richest, most full-featured client solutions that run on Windows. WPF applications are delivered similar to a WinForm application. You choose WPF when you need to deliver a unique visual experience for your Windows application by taking advantage of vector-based scaling and 3D. In addition, WPF solutions can be browser-hosted in a security sandbox on Windows machines.
  • ASP.NET Web Application (WebForm)—ASP.NET applications allow you to build solutions that run inside a user's browser but communicate with a web server for application processing. These solutions use HTML, CSS, and AJAX on the client but communicate across HTTP to a server for centralized processing of code and data storage and retrieval. Choose ASP.NET if you need to target a wide client base with low (or no) installation and wish to centralize updates to the application (as you need only update the server).
  • Silverlight Application—A Silverlight application is similar to WPF; you can develop highly interactive user experiences that combine video, animation, 3D, and vector-based scaling. However, Silverlight applications are delivered across the Web and run using a browser plug-in. Therefore, unlike WPF, they are not constrained to just Windows machines. They also work on a Mac.
  • Office—Visual Studio enables you to build solutions based on the Office productivity tools, including Excel, Word, Project, Visio, Outlook, PowerPoint, and InfoPath. This includes support for both 2010 and 2007 versions of Office. Choose an Office project when you want to write a business-productivity application centered on the product of one of these tools (such as an Excel template or spreadsheet).
  • SharePoint—SharePoint applications are for delivering functionality through the collaboration portal. You might write a Web Part to deliver information, work with a document type, or allow users to enter data. You might also define business process workflow centered on a document. Note that 2010 provides expanded capabilities for developers building SharePoint applications (including F5 debugging support).
  • NetCF—The .NET Compact Framework 3.5 runs on small devices (phone and PDAs, which enables you to build applications that target mobile devices and mobile users. The .NET CF and mobile SDK is available as a separate download to Visual Studio.
  • XNA—XNA Game Studio 4.0 allows you to build games for Windows, Xbox, and Zune. This, too, is a separate download to Visual Studio.

Each of these UI delivery technologies is supported by Visual Studio 2010. With them, you have many options for creating great user experiences on .NET. The sections that follow highlight a number of these technologies for building both Windows and Web solutions.

Note - There are many client options in Visual Studio 2010. Many are highlighted here. However, they are also covered in greater detail throughout the book. Chapter 17, "Creating ASP.NET Applications," covers ASP.NET; Chapter 18, "Building Windows Forms Applications," is about standard Windows forms; Chapter 19, "Creating Richer, Smarter User Interfaces," focuses squarely on WPF; Chapter 20, "Creating Rich Browser Applications," discusses AJAX and Silverlight applications; and Chapter 24, "Developing Office Business Applications," covers creating solutions based on Microsoft Office.

Developing Rich(er) Clients

Today's users demand a rich, interactive experience when they work with software. The line between what a web-based client can do versus one that runs on Windows has blurred thanks to many UI advancements, technologies, and tools, which can make it difficult to choose the right UI delivery for your next application. It also means that if you do decide to write a Windows-based client, you need to be sure you take full advantage of having the desktop resources at your disposal. Your application should perform well, look great, provide a high degree of interactivity, be able to work with larger datasets at any given time, and more. Here we look at the Windows-based client options for Visual Studio 2010 and creating smart, rich applications using WinForms, Windows Presentation Foundation (WPF), and Microsoft Office.

Windows (WinForms)

Visual Studio provides a mature, feature-rich set of tools for the rapid development of Windows applications that includes a drag-and-drop form designer and many controls inside the form toolbox. With these tools, developers can quickly lay out a Windows application that includes menus, toolbars, data access, tabs, re-sizing support, common controls for working with and printing files, and much more.

You create a Windows application by selecting the Windows Form Application project template in the New Project dialog. This type of application is also called a WinForm application as it is based on the WinForm technology in Visual Studio and the .NET Framework.

The first step in a WinForm application is determining the layout of your form. You might decide to create a document-centric application (such as Word or Excel), of a single utility application (such as Calculator or Disk Defragmenter), or some other type of application. Whatever your choice, layout is typically controlled through the docking of controls to the form (through the Properties dialog) and the use of Panel controls.

For example, Figure 1.4 shows a MenuStrip control docked to the top of a WinForm and a StatusStrip docked to the bottom of the form. The menu allows a user to set a folder from which to view pictures. The status indicator provides updates as the application processes.

01fig04.jpg

Figure 1.4

Building a WinForm application inside Visual Studio 2010.

Docked to the center of the form is a SplitContainer control, which allows two panels to size relative to one another using a splitter bar. Together, these controls define the layout of the main, interactive section of the form. In this case, a user can open a folder of pictures and view them individually by selecting a thumbnail from the left side of the form. The form then displays the selection as a larger image to the right. These features are accomplished through ImageList, ListView, and PictureBox controls. Each control is added to the appropriate area of the form and then configured via the Properties window.

You can start to see that the initial layout and definition of a WinForm application is a rapid experience. You first need to decide your form layout. The tools make it easy from that point forward.

As with all .NET programming, you first create the visual design and layout of your user interface, and then write code to respond to events. The WinForm application has a standard model that includes such events as Load, Closing, and Closed. You also respond to the events triggered by the controls on the form. For more detailed information on building WinForm applications please refer to Chapter 18.

Windows Presentation Foundation (WPF)

WPF is a set of classes, tools, and controls with which developers can create even richer, more dynamic client solutions for Windows. This includes developing user experiences that combine traditional data view and entry with video, 3D graphics, shading, and vector-based scaling. The results are truly unique, visually appealing, rich applications.

WPF uses markup code to define the UI. This should be very familiar to web developers. The markup is based on XAML (Extensible Application Markup Language). The XAML is created for you using the Visual Studio WPF designer (or a similar tool from Microsoft called Expression Blend). The XAML is then processed by the .NET CLR. The CLR process this XAML on the client, outside the browser. Therefore, it is not bound by the limits of HTML and the browser. Instead, it can create vector-based, hardware-accelerated user experiences.

Visual Studio provides a familiar experience for creating WPF solutions. You first define a WPF project and add WPF forms to the project. When creating your solution, you select a project type based on whether the application runs as a browser add-in or as an installed desktop client. Figure 1.5 shows the WPF project templates. Selecting WPF Application creates a basic WPF application that is pushed to or installed on a client. It might have access to local resources on the client.

01fig05.jpg

Figure 1.5

Creating a new WPF project with Visual Studio 2010.

The WPF Browser Application, on the other hand, is meant to be deployed through a URL and run as a browser extension. The application, called an XBAP (XAML browser application), runs inside a sandbox. It does not have rights to the client machine and is cleaned up as part of the browser's cache. The application does not require a download provided that the user has the right version of the .NET Framework on her machine. It can work with the browser's cookies and is supported by both IE and Firefox.

Making the wrong choice here is not too problematic. You can move the WPF forms between application types. Note that the other two application types highlighted in Figure 1.5 are WPF User Controls and WPF Custom Control Library. Both are for creating reusable controls for WPF applications.

The next step in building your WPF form is to simply open it and drag and drop UI controls onto a design surface. One big difference for developers used to building WinForm applications, however, is that you now have control over the form layout code (or XAML), which is more akin to designing a web form with Visual Studio. Figure 1.6 shows the XAML designer in action.

01fig06.jpg

Figure 1.6

Designing a WPF form.

Notice that the XAML controls are listed in the toolbox on the left. Although they are similar to Windows and web controls, they are their own set of controls just for WPF. Also, notice how the designer has a split view between the design surface and the XAML. These views stay in sync as you develop your code. Finally, the properties window shown on the right provides a familiar experience for WinForm developers when editing the many properties of a selected control. We cover the WPF designer in greater detail in Chapter 19.

Office-Based Solutions

Developers have been able to customize Office for a long time now; some of us still remember writing Excel macros on Windows 3.1 or automating Word with Word Basic. Thankfully, these days the tools used to write Office solutions are built into Visual Studio. With them, you can create Office-based projects and solutions that leverage Word, Excel, Project, Visio, PowerPoint, Outlook, and InfoPath. Figure 1.7 shows the New Project dialog for Office solutions. The templates shown in the figure are for C#, but the same exist for Visual Basic.

01fig07.jpg

Figure 1.7

The many Office project templates inside Visual Studio.

There are a few scenarios that might lead developers to create an application based on Office. The most common is when you need to extend a line-of-business (LOB) application to provide functionality inside the common, information-worker productivity tools of Office. This type of solution typically combines structured, corporate data with a business process workflow that's centered on a document (such as an invoice or a purchase request).

As an example, suppose you work with a financial, manufacturing, or payroll application. Each of these fills a specific need. However, users might need to work with the data that is housed inside the application and make key decisions that feed back into these systems. This work is often done through cut-and-paste and is not captured by the systems. Users lose productivity switching back and forth between the Office tools and the LOB application. This is precisely where you should consider creating an Office Business Application (OBA) to help bridge this gap.

Develop Documents, Templates, and Add-Ins

Notice the many templates in Figure 1.7. There are three separate templates for Excel 2010 (and for 2007), for example. Each of these templates provides a specific purpose. Office application templates allow you to create solutions built on a single document, a document template, or as an add-in to the given Office application. The following list provides a brief overview of these three project subtypes:

  • Document (Workbook in Excel)—Document projects allow you to build a solution based on a specific document. There are typically not multiple instances of the document. As an example, suppose you have an Excel workbook that needs to read and write project resource billing information from and to an enterprise resource planning (ERP) system. This document might be updated weekly as part of a resource meeting. The data should be up-to-date and changes should feed the billing system. In this instance, you would create a solution based on this single document.
  • Template—An Office Template project is one that is based on an Office template file (an Excel .xltx, for example). Creating a solution based on an Office template file gives you the flexibility to provide users with assistance when creating a new instance of a given template. You might push common document templates out to your users. When a user creates a new instance, the template might reach into data housed in other systems to help the user fill out the details of the document. You might then, in turn, capture the results in a database after routing the template through a basic SharePoint workflow.
  • Add-in—An Add-in project allows you to extend the features and functionality of a given Office application. You create Add-ins to offer additional productivity and solutions inside a given application. You might, for example, write an Outlook Add-in that allows users to more easily file and categorize their email.

Whichever template you choose, Visual Studio 2010 provides a rich, design-time experience for building your Office solution. As an example, Figure 1.8 shows the Visual Studio 2010 design experience building a solution for a Word 2010 Template. In this example, a user is creating a quote for training. The fields in the document pull from a LOB database that includes customer information, resource data, and common pricing.

01fig08.jpg

Figure 1.8

Designing a Word 2007 Template project in Visual Studio 2010.

The Office templates in Visual Studio also provide design support for working with the Office Ribbon. In this way, your application can behave like the rest of Office. You can embed productivity features and aids on your own tab of the Office Ribbon. Figure 1.9 shows a simple example. The features shown apply to the document template for a sales quote discussed previously. In this example, a developer provides specific features based on the document template that are then accessed by the user from Word's Ribbon.

01fig09.jpg

Figure 1.9

Creating a custom Ribbon for a Microsoft Office solution.

Create SharePoint Solutions

While not a true Windows client, SharePoint and Office have become nearly synonymous. Therefore, we thought it appropriate to discuss the SharePoint-specific capabilities of Visual Studio in this section. Companies leverage SharePoint for knowledge management, collaboration, and business process automation. Of course, this inevitably means customization and extension by developers. If you customized SharePoint using the existing tools for Visual Studio prior to 2010, you know that this presented a big challenge. The design-time support was not there, configuration was difficult, and debugging was very challenging.

Visual Studio 2010 presents a much richer toolset for SharePoint developers. With it, you can create SharePoint workflows and build Web Parts based on ASP.NET. In addition, the debug experience has streamlined. SharePoint development is now a first-class consideration inside the IDE. This should help developers more easily extend SharePoint to meet the business demand this collaboration product has generated.

There are many more advancements, however, including access to open XML formats, the capability to add features to existing Outlook forms, custom task panes in Word and Excel, data binding, and improved deployment and security.

Creating Web Clients

The vast majority of applications built these days involve some semblance of a web component—be it a full-blown browser-based, web application; a smart client that works across the Web; or a web service. In fact, the line between a traditional rich client and a web application is blurring, and Technologies such as AJAX (Asynchronous JavaScript and XML), jQuery, Web Services, Smart Clients, XAML (Extensible Application Markup Language), and Silverlight have helped blur the line. You can now build rich user experiences as your needs dictate. Of course, Microsoft has remained suitably focused on expanding Visual Studio's capabilities with respect to web development.

Web developers want tools that help them through the common functions of building their application. Let's face it, the HTML, CSS, and XML standards can be a pain to follow by memory alone. Instead, we want tools that guide us. And of course, as soon as we want to work with code, we want to be able to access the entire related source and massage it as necessary. Visual Studio 2010 provides an array of web development templates from which to choose. This section presents many of these possibilities.

Note - We cover many aspects that follow in greater detail in both Chapter 17 and Chapter 20.

Building Websites with ASP.NET

Visual Studio 2010 provides a rich set of tools and controls for the web developer. It supports standard controls for building server-side ASP.NET and emitting standard HTML. Controls include labels, text boxes, buttons, and related data-entry controls. In addition, there are controls for validation, working with data, doing personalization with Web Parts, and managing the user process with the login controls.

You create an ASP.NET site in Visual Studio by either creating a new website (File, New, Web Site) or by selecting the ASP.NET Web Application template in the New Project dialog. The website template should be used to create projects that mimic basic, file-centric websites. This typically means all files (your markup and code) deployed to the website and managed as files. Figure 1.10 shows an example of a new site created with this basic project template. This template should feel comfortable to most developers who are used to working on file-driven websites.

01fig10.jpg

Figure 1.10

The ASP.NET website project template.

A Web Application, on the other hand, provides a project model similar to other .NET applications. You can generate .DLLs, set references, sign assemblies, manage build properties, and do similar tasks in a familiar manner as writing other .NET applications. Figure 1.11 shows an example. Notice the References folder for setting .NET references and the multiple versions of the Web.config file. Your template choice ultimately depends on the manner in which you plan to build and manage your site.

01fig11.jpg

Figure 1.11

The ASP.NET Web Application Project Template.

Develop and Design at the Same Time

You develop ASP.NET pages by editing the design and connecting code to that design. The design is referred to as markup. This is XHTML that defines the controls, their layout, and their look on your page. Visual Studio includes both a markup editor and a visual, WYSIWYG designer for laying out your page. Most web form developers switch between the Source (XHTML) and the Design (WYSIWYG) view of a web form many times during development. The Source view allows you full access to editing the XHTML of the page. Design view lets you see the page develop and gives access to the many shortcuts attached to controls in the designer. Visual Studio makes switching between these views simple. It provides a Split view. With it, you can see both the XHTML and the visual Designer. Figure 1.12 shows an example.

01fig12.jpg

Figure 1.12

The Web Form Designer Split view.

Split view tries to keep both the source and the design in sync. This works when you drag items from the toolbox to either the Source or the Design view panes. However, the Design view can get out of sync when you are doing a lot of edits to your source. In these cases, the Design view indicates that it is out of sync (see the middle of Figure 1.12). Click on the Designer and everything is back in sync.

Centrally Manage Navigation and Design

Visual Studio 2005 introduced the capability to create master pages. These pages centralize the management of a site's design and navigation elements. In addition, master pages are supported by the designer, which allows for a richer design experience. A developer is able to see the page in the context of the site's central design while in design mode.

You create a master page by selecting the Master Page template from the Add New Item dialog. You then define your site navigation, header and footer information, styles, and anything else that should apply to each page in the site (or sub-area of a site). After you define the navigation, you can create new web forms that provide specific content that should be enclosed inside a master page. Figure 1.13 shows an example of working with a web form whose outer content is based on a master page.

01fig13.jpg

Figure 1.13

Creating pages that use a master page.

Building Loosely Coupled Web Applications with ASP.NET Model-View-Controller (MVC)

Visual Studio supports an alternative to building your application using standard Web Forms. This alternative is based on the Model-View-Controller design pattern. The purpose of this pattern is to separate the application's logic and data (model), its user interface display (view), and the code that helps the user interact with the UI and the data (controller).

Sites built using MVC are typically easier to maintain as there is decreased dependency between the application layers. They also support test-driven development as you can write tests for each layer. In addition, you can use the MVC construct to separate the duties by developer where you might have designers creating views while other developers are working on models or controllers. Another advantage of MVC is that developers have more fine control over the processing as a web page.

The ASP.NET implementation of the MVC is both a set of namespaces (System.Web.Mvc) and project templates. You create a site in which you intend to use MVC by selecting the ASP.NET MVC Web Application project template in the New Project dialog. Note that you can mix sites that use both MVC and standard Web Forms. In addition, ASP.NET MVC supports most ASP.NET constructs such as master pages, sessions, caching, profiles and membership, configuration, and the like. However, it does not support ASP.NET view state. Figure 1.14 shows an example of the ASP.NET MVC site template inside Visual Studio. See Chapter 17 for a detailed discussion of ASP.NET MVC 2.

01fig14.jpg

Figure 1.14

An ASP.NET MVC project.

Adding Richer Features with AJAX

AJAX represents the capability to leverage the ubiquitous support for JavaScript in web browsers to create a more interactive user experience. Client applications built to leverage AJAX still have a client-server paradigm. However, with AJAX the client can update portions of a given page without appearing to have posted back to the server (of course, it typically does). In addition, most AJAX-enabled applications put more processing on the client for things such as toggling sections of a page, working with tabs, auto-completing data entry, popping up dialogs, and more. The result is a step forward in interactivity for a user.

AJAX is not a Microsoft-specific technology. Instead, it is a programming model. However, Microsoft created a set of controls called the AJAX Extensions for Visual Studio. These controls allow developers to more easily create AJAX experiences. Figure 1.15 shows the controls inside the Visual Studio Toolbox.

01fig15.jpg

Figure 1.15

The AJAX Extensions Controls in Visual Studio 2010.

The AJAX Extension Controls enable you to create a page that can receive partial, asynchronous updates (using UpdatePanel) and show update progress (using UpdateProgress). They also allow you to create your own controls and features that implement AJAX without having to write the client-side JavaScript.

In addition to these controls, Visual Studio 2010 supports IntelliSense, code comment documentation, and client-side debugging for JavaScript. It also contains the Microsoft AJAX Library, which is a JavaScript common library that supports object-oriented development for JavaScript. Finally, many of the ASP.NET controls have AJAX features built-in to give you control over partial postbacks versus full page refresh.

Developing for Silverlight

Microsoft's Silverlight is another exciting client technology for the Web. Silverlight allows for an even richer user experience than AJAX. You use it to create media-rich (images, video, graphic scaling), highly interactive experiences. Silverlight requires a browser add-on (or plug-in). It works with Windows, Mac, and Linux in a variety of browsers. Silverlight is based on WPF; it allows you to leverage XAML skills inside a browser and across platforms.

You create a Silverlight application in Visual Studio 2010 by selecting the Silverlight Application project template in the New Project dialog. When doing so, you need to select a host for your application. This can be an ASP.NET Web project or you can let Visual Studio generate a test page upon build. You can also set the target version of Silverlight on which you intend your application to be deployed. This is based on the versions of Silverlight you have installed. Figure 1.16 shows an example of configuring these two settings upon project creation.

01fig16.jpg

Figure 1.16

Targeting a specific Silverlight version.

You create a Silverlight application like you might a WPF application. You have a designer for laying out a page. You can also edit the XAML directly. In addition, the Silverlight controls are available to you from the Toolbox. The real difference is the application host. This is typically an ASP.NET web project. You can see in Figure 1.17 that there is both a host project (generated by the Visual Studio template) and the Silverlight project.

01fig17.jpg

Figure 1.17

A Silverlight Page Inside Visual Studio.

Designers and Developers

When discussing Silverlight (or even WPF), it's important to note the work that went into the technology and tools to support a strong designer-developer workflow. It's understood that traditionally developers have been left to try to "design" the UI. It goes without saying that many developers have not exactly shined as UI designers.

However, even in scenarios where designers are employed on projects, the design often falls short or the implementation is an arduous process. Designers have their own tools that often do not talk with those of the developers. Instead, a design is often provided to the development team as a picture or some basic HTML. Developers are left to try to realize the intricacies of the design while having to concern themselves with coding the solution. This can cause the design to never be exactly what was envisioned and developers to spend too much time trying to get the look right.

Silverlight and WPF try to right this wrong by keeping the UI markup (XAML) totally separate from the implementation code (C# or VB). In addition, Microsoft has provided design tools that let designers create real user interfaces that can be leveraged by the development team. There are no more "lost in translation" issues. Instead, a designer can create or open a XAML-based UI element, edit it using the power of Expression Blend (they do not have to learn Visual Studio), and save it back to the solution or send it to a developer. The developer can then open the same item inside of Visual Studio and begin responding to key UI events with their code. This back-and-forth can continue as the UI builds up over time. Nobody's code gets stepped on; everyone focuses on their strength.

Figure 1.18 shows a sample of the Expression Blend 3.0 tool. Notice that the same .xaml file that was in the Silverlight example is also open here. Designers can use a tool with which they are more familiar to lay out the UI and apply visual techniques such as 3D and animation. Also notice that the C# project file is being used to maintain continuity between the solutions. After the designers are finished, their save goes right back to the development team. They can even test their UI in the context of the application by building and running the project.

01fig18.jpg

Figure 1.18

Designing a WPF form inside of Expression Blend.

Making the Client Choice

The many options for developing rich user experiences on Windows invariably leads to this question: "What is the right UI technology for my next application?" Of course, the answer depends on your specific scenario. When making your decision, you should keep the intent of each technology in mind. The following should help you make the right decision:

  • Windows forms still has a place in creating installed, business applications that connect with the operating system, leverage resources on the client, connect to databases, and more. They offer standard, workman-like user experiences for data entry, file manipulation, configuration, and related task-oriented solutions. Windows forms are easy to develop and do not typically involve much work from a design perspective. We suggest you continue with WinForms if you already have a significant investment in them. If you are building from scratch, however, you should take a good look at WPF as that is where Microsoft has focused its innovation.
  • WPF is a Windows technology that requires the .NET Framework on the client. It can run in a browser sandbox or as a fully featured Windows application. You should leverage WPF when you need to create a rich user experience with high design aesthetics (3D, animations, vector-scaled graphics), want a low cost of installation and update, and require distributed connectivity.
  • Silverlight runs inside a browser. Silverlight is a very lightweight, cross-platform version of WPF. It provides many of the rich features of WPF, but it allows you to run on different platforms (Windows, Mac, Linux) and different browsers. Of course, Silverlight does require a browser plug-in. The browser plug-in runs a mini version of the CLR on the client. In this way, you still can write your code in C# or VB and have access to bits of the .NET Framework. However, clients must have the plug-in to run your application. Think of Silverlight as an alternative to Adobe's Flash product.
  • ASP.NET Web applications are a great solution for creating functionality that runs ubiquitously. The only requirement is a web browser. Therefore, Web Form applications are still a great solution when you need to reach the widest possible audience and you do not wish to deal with client installations. In addition, most modern browsers support JavaScript. Therefore, you can still get some interactive features using AJAX in your ASP.NET application.
  • Microsoft Office solutions are applicable when users tend to work directly with productivity documents. In these cases, you can combine the power of Word, Excel, or another Office product with your line-of-business data to create a rich solution. For instance, you might define an Excel document for creating an invoice or a timesheet. You could connect that document to a SharePoint library for managing approvals and workflow. All the while, the document can be built to get data from backend systems and write out results to the same.

Coding for the Cloud

Most of the distributed applications we write are deployed onto one or more servers for hosting and delivery out to the user base. This hosted environment might contain multiple web servers, a database server, and other servers as necessary. The environment then needs to be monitored and managed either internally or through a hosting provider. The management of this environment can be costly. Servers require repair and updates; as the demand for your application increases, you often have to add new hardware to scale with the demand.

Cloud computing is meant to help address these issues. In its basic form, cloud computing represents a hosting environment for your entire application (user interface, logic, database, and so on). The environment is meant to automatically scale with your demand and free you from hardware management and monitoring tasks. This is accomplished through massive amounts of distributed, automatically managed computing power.

Visual Studio developers that want to take advantage of cloud computing can do so via Windows Azure. You can think of this technology as the server operating system for hosting your application. The difference is that Azure is not a single server OS you install; rather, it is an OS that sits atop massive amounts of shared, computing power. You can develop, deploy, manage, and scale your application using the Windows Azure cloud as the single host for your application. Adding scale to that host is then simply a configuration change.

Creating a Cloud Application

You create an application destined for the cloud by selecting the Cloud Service template from Visual Studio's New Project dialog. Doing so brings up the New Cloud Service Project dialog as shown in Figure 1.19. Here, you select a role (or type) for your application. The ASP.NET Web Role, for example, tells Visual Studio to create a standard ASP.NET website for deploying in the cloud. This is also the role you would use to move your existing ASP.NET sites to the cloud.

01fig19.jpg

Figure 1.19

Selecting the role for your cloud service project.

Developing for the cloud is very similar to developing standard .NET applications. In the case of the Web Role, you simply develop your website as you might otherwise do it. This means adding pages, using standard controls, and coding to the ASP.NET event model. The Visual Studio solution template keeps the cloud service application and your ASP.NET site as separate projects in the solution. Figure 1.20 shows an example. Notice that the website looks like (and is) a standard ASP.NET site. The cloud service application serves as the configuration point for defining how the site should be deployed into the cloud.

01fig20.jpg

Figure 1.20

An ASP.NET website and its cloud configuration project.

Running a Cloud Application

You can run and debug your cloud-bound application from your client machine. That is, you are not required to deploy it into the cloud in order to run it at development time. This experience is very similar to debugging any local project. The primary difference is that the cloud is simulated locally. An application called the development fabric runs on your machine to host your application. The development fabric executes your application as if it were running on the Azure cloud. You can actually view and control this fabric by clicking on its icon in the task bar. Figure 1.21 shows a website running in the development fabric.

01fig21.jpg

Figure 1.21

An application running locally inside the cloud development fabric.

Publishing to the Cloud

When you are ready to push your application into the actual cloud, you can do so directly within Visual Studio. You first must publish your solution by right-clicking the cloud service and selecting Publish. This creates a package file (.cspkg) and a configuration file inside your applications bin\Publish directory.

To deploy, you must have an Azure services account (windows.azure.com). In Azure you define and manage your projects and their services. In this case, you would create a hosted service under a specific project you name. The hosted service must map to a URL that provides access to your service. After your service is defined, you can then deploy. You can deploy a staged or production version. Clicking on the Deploy button for either Production or Stage takes you to a screen where you can upload your application package and configuration file. Figure 1.22 shows an example.

01fig22.jpg

Figure 1.22

Deploying a hosted service (website) to the Azure cloud.

After deployment, you can start, suspend, configure, or upgrade the application from the Windows Azure management site. Of course, you can also now access it from its URL.

Working with Data

Data is the domain of the business developer. It makes sense then that the number one tool and framework for business development continues to provide new and better ways of accessing and exposing that data. Data access is everywhere inside Visual Studio and the .NET Framework. Here we highlight some of the things you encounter when working with data inside Visual Studio 2010.

Note - Data access is covered in detail in Chapter 21, "Working with Databases."

Design Your Data

A typed dataset allows you to create a .NET class that is based on your database table semantics but works as a dataset behind the scenes. This can simplify your programming as you are working with a class. Visual Studio can generate all the code for you. Typed datasets have been part of Visual Studio for a while now. You can auto-generate them based on your actual table schemas and edit the code that is generated to query, update, insert, and delete data.

Visual Studio 2010 provides design-time support for typed datasets. You create a typed dataset file (.xsd) by adding it to your project. You can then use the Toolbox and Server Explorer to build the dataset. Figure 1.23 shows an example. Tables from the Server Explorer were added to the design surface to build the dataset (and generate the underlying class code).

01fig23.jpg

Figure 1.23

Building a typed dataset with the dataset designer.

After you create the dataset, you can program against the actual class that represents your data. The class manages the task of working with your database. In addition, typed datasets support hierarchical updates. This feature allows you to save data in multiple, related tables through the typed dataset class. In Figure 1.23, this would mean you could edit both the employee details and the related territories and update both as a single process.

Separate Data Design from Storage Schema

Most business applications rely on data that is stored in relational databases. Databases have a well-known structure of tables, columns, and relationships. This structure makes for a nice storage and reporting mechanism. However, the needs of an object-oriented, .NET developer and the database technology are often mismatched. The object developer thinks in terms of objects, properties, methods, relationships, encapsulation, and the like. A lot of time is spent (and a lot of code is written) to convert data from a database into an object-oriented structure and back again.

Visual Studio provides the Entity Data Model (EDM) as a means for mapping objects to database schemas. The EDM and Entity Framework allow you to design the data classes that are used by your application independent of the actual storage mechanism used to persist those classes to a database. You can program against these data classes (the design schema) in your application. You can then define a separate schema for doing the actual persistence (called the storage schema). You create a mapping specification to indicate how the design and storage schema are connected. In this way, the EDM allows your application data to evolve independent of your database schema. They are not coupled. If either schema changes, you change the map to keep them connected.

You create an EDM by selecting the ADO.NET Entity Data Model item from the Add New Item dialog (see Figure 1.24). Selecting this template launches the Entity Data Model Wizard.

01fig24.jpg

Figure 1.24

ADO.NET Entity Data Model.

The EDM Wizard enables you to start with an Empty Model (as shown in Figure 1.25), which is great if you intend to model first and then build data storage. You can also generate an EDM from an existing database. This creates a set of class files that mimic your current database structure. However, your class files can then evolve independent of the underlying storage.

01fig25.jpg

Figure 1.25

The Entity Data Model Wizard enables you to generate a model from an existing database or start with an empty model.

The EDM is a file (.edmx) in your solution. You can open it inside a designer for editing. Figure 1.26 shows an example. Notice the storage schema is being mapped to the design schema using the Mapping Details pane (at the bottom of the figure). There are also options for handling relationships (Toolbox) and browsing the model (Model Browser on the left). Once your EDM is complete, you can code directly against it inside your application as you would any class object.

01fig26.jpg

Figure 1.26

Mapping design schema to storage schema in the EDM designer.

Build Occasionally Connected Applications

Many applications written these days require access to data both on- and offline. For example, you might need report data or customer service records when traveling or at a customer site. You cannot always rely on being connected. Developers have been dealing with this issue in various ways. Visual Studio provides data synchronization services to provide a common, easy solution to this difficult problem.

Data synchronization allows you to sync and cache data between a central database and a user's client system. The cache is a version of SQL Server Compact Edition (CE). Microsoft takes advantage of these services in Windows 7, Vista, Office, and even its Zune software. You, too, can take advantage of these services in your applications.

You can add a local database cache to a WinForms or WPF application. You do so through the Local Database Cache item template. With it, you create a .sync file to configure how synchronization should happen between a local data store and a server. Opening the local database cache file in Visual Studio opens the Configure Data Synchronization dialog. Here you can set up tables that should be cached on a client, set up the server database, and configure the client database information. Figure 1.27 shows an example of configuring this dialog to sync certain tables between a server and a client database cache.

01fig27.jpg

Figure 1.27

Configuring data synchronization.

Write Connected, Service-Oriented Solutions

Many business applications involve specific processes, or workflows, around documents, records, and related data. These business processes typically involve staged review and approval by business personnel; they might also require communication between various systems. A business process is also typically long-running—meaning the process is not a simple, single execution but rather a multistep process that has to "wait" before moving to the next step.

Building these processes into business application typically required a lot of custom development work with little guidance, or it meant tying your application to third-party tools. Web services helped, but developers lacked an easy way to build them with support for multiple protocols, different transports, strong security, and transactional support.

Visual Studio 2010 provides in-the-box support for building business processes as workflows and reliably integrating them with other applications, systems, and partners. This section takes a look at Windows Workflow (WF) for defining reusable business process and Windows Communication Foundation (WCF) for unlocking that business process across system boundaries.

Develop an Application/Business Process

A workflow represents a set of actions (called activities) that typically model a business process and often involve user interaction. WF provides the framework, tools, and an execution engine for enabling workflows in your application. With it, you can create a workflow and know that it runs wherever the .NET Framework is installed.

You create a workflow model to describe the order and logic of how activities are to be executed based on the data in the workflow, your business rules and conditions, and system and user actions. A workflow is executed and managed by a workflow instance object. This object is run in process with your application and controls the execution and current state of the workflow. You interact with your workflows from within your application. This could be any type of application (Windows, ASP.NET, console, service, and so on). You typically submit data to the workflow and you might get a response, or you might use the workflow to kick off certain tasks or activities in your business process. The workflow can also persist itself during long-running transactions and then rehydrate on demand.

Note - If you've used WF in a prior version of .NET, you should know that WF in .NET 4.0 is almost a completely new version. For example, the SequentialWorkflow and StateMachineWorkflow classes from the prior version are now gone. Instead, you have a single, hierarchical model.

Building a workflow is a visual process. The thought behind this is that workflows themselves are traditionally illustrated as a set of steps (boxes) and connections (arrows). For this reason, workflows are represented as a FlowChart item template. You can add activities (or a sequence of activities) to a flowchart and connect them accordingly. Activities are added from the Toolbox items. There are many standard activities for handling things such as branching, looping, error handling, and compensation. You will most likely also code your own, custom activities and add them to the workflow. Figure 1.28 shows an example workflow in the designer. There are activities listed in the toolbox on the left. Notice that once added to the diagram, you can code the activity (including IntelliSense) from within the designer.

A workflow can also have both variables and arguments. These concepts are similar to programming .NET C# or VB. A variable is used to store state (or data) inside the workflow (or activity). An argument is used to pass data into and out of a workflow. Notice the variable and arguments editor at the bottom of Figure 1.28. Here, you define the name, type, direction, and default value for variable or argument used by your given workflow.

01fig28.jpg

Figure 1.28

The Workflow designer inside Visual Studio.

Finally, notice the messaging activities listed on the Toolbox inside Figure 1.28. These activities allow you to define a receive and/or send port for the workflow. This enables your workflow to be listening for messages and then kick-off a hosted instance after a message is received. You can also use the send shape to communicate with other hosted services.

Note - See Chapter 23, "Embedding Workflow in Your Applications," for a more detailed discussion of Windows Workflow.

Create and Consume Services

Most organizations have multiple systems, each designed for a specific purpose. They might have systems for finance, HR, order management, inventory, customer service, and more. Each application houses specific business processes. However, most organizations need to unlock these business processes from their applications and reuse them as part of an integrated solution. This is where service-oriented solutions have helped. By exposing an application's business process as a service, multiple clients can take advantage of that process.

The promise of code reuse has been with us a long time. However, service-oriented code reuse became very popular with the advent of web services. The ubiquitous nature of HTTP and port 80 coupled with XML-based interfaces allowed for a new level of communication between application boundaries. Developers began wrapping key business functions as services and calling them from multiple clients.

Visual Studio 2010 and the .NET Framework 4.0 represent another step forward in this service-oriented paradigm. With these tools, you can create services based on Microsoft's Windows Communication Foundation. WCF is a framework that recognizes that developers need multiple layers of communication (not just the SOAP protocol carried over an HTTP transport), require strong security, often need to support transactions, and do not want to write all the plumbing code to do so.

You create WCF services as a code library, as a workflow, or as a web service application. Figure 1.29 shows the new project templates listed under the WCF project types. From here you can indicate that your web service contains a workflow or simply create a service library that calls custom code you write.

01fig29.jpg

Figure 1.29

The WCF Project Templates.

You can also still create XML web services (.asmx) through the web project templates. This same template area also provides access to the WCF Service Application template. With it, you can create a WCF service that is configured similarly to a standard web service (and hosted in ASP.NET).

WCF is all about configuration. It frees you from having to write all the service plumbing code. Instead, you can focus on the functionality of your service. For example, you can add service endpoints to your service depending on which communication stack you intend to support (HTTP, TCP/IP, MSMQ, Named Pipes, and so on). Figure 1.30 shows the WCF configuration editor. Notice how the binding support for an endpoint is a configuration (and not coding) task.

01fig30.jpg

Figure 1.30

Configuration of a WCF service.

After you've built your service, you need to host it so consumers (clients) can find it and call it. You have a lot of hosting options available to you including self-hosted, Windows service, IIS, and WAS (Windows Process Activation Service). Actually consuming the WCF service requires you to define a service reference and configure how you are going to talk with the service and pass messages. We discuss creating, hosting, and consuming services in detail later in this book (Chapter 22, "Service Oriented Applications").

Note - See Chapter 22, "Service-Oriented Applications," for a more detailed discussion of the tools for WCF and web services.

Summary

A new release of Visual Studio means a lot to all the various development camps out there. Visual Studio touches developers who write code in C++, C#, Visual Basic, and many other languages. Literally millions of developers boot up and launch their favorite tool every day. They spend the vast majority of their working hours, days, weeks, and months architecting and building solutions with the tool. Hopefully, this chapter oriented you to the many possibilities available for building your next application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.
This is a Organisation

1 members

Comments and Discussions

 
GeneralMy vote of 5 Pin
CS14011-Feb-12 19:53
CS14011-Feb-12 19:53 
GeneralLots of good info Pin
BillW3329-Sep-10 6:18
professionalBillW3329-Sep-10 6:18 
GeneralMy vote of 5 Pin
Slacker00725-Sep-10 5:13
professionalSlacker00725-Sep-10 5:13 
GeneralVery good article. Pin
Sushant Joshi22-Sep-10 17:09
Sushant Joshi22-Sep-10 17:09 
GeneralMy vote of 5 Pin
Eric Xue (brokensnow)22-Sep-10 16:36
Eric Xue (brokensnow)22-Sep-10 16:36 
Well-explained! Please keep up with your good work!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.