Chapter 1. Introduction

Silverlight is a web-based platform from Microsoft that can be used to build rich Internet applications (RIAs). Of the many new possibilities it enables, one receiving a lot of attention is its huge potential as a platform for building business applications. Despite its relative youth (having just reached its third birthday), it has achieved remarkable maturity, with its development progressing at a rapid rate—now at version 4. With just nine months separating versions 2 and 3, and a further nine months separating versions 3 and 4, its tremendous growth in that short time demonstrates the enormous amount of effort and commitment that Microsoft is pouring into Silverlight to bring it to the forefront as one of its core development platforms. Community support for the technology has been overwhelmingly positive, gaining more and more momentum as companies increasingly choose it over more mature platforms. This ensures its future as a viable platform for delivering applications that you can be sure will only grow and become ever more popular.

If you have previously only been exposed to Windows Forms and ASP.NET development, then it will no doubt be a brave new world and one with a steep learning curve, but ultimately the benefits make the initial effort worthwhile. However, with this book to help you through your journey, you should find that a lot of pain will be taken out of this learning curve, guiding you in the right direction while you build your first business application in Silverlight.

Who This Book Is For

This book covers not just Silverlight as a technology, but also the important concepts in designing and developing business applications. From structuring your application to actually implementing features, it covers most of the lifespan of developing a business application in Silverlight.

To follow along with this book, you should know your way around Visual Studio well, have a reasonable degree of familiarity with C# and its features (such as generics and LINQ, although experience with a similar language such as Visual Basic or Java may suffice), be comfortable with designing and querying databases, and have a good understanding of object-oriented design (OOD). Some prior business application development experience (or understanding of what building a business application entails) is an advantage, but isn't absolutely necessary, as I will discuss the reasoning behind many of the concepts unique to business application development as needed. However, you may need to refer to alternative sources for more information when the concepts are particularly complex and detailed. For example, a good understanding of design patterns (such as those described in the influential book Design Patterns, by the authors typically referred to as the Gang of Four [Addison-Wesley, 1994]) is important when designing business applications, but complete coverage of these in the context of designing Silverlight applications is really a whole book in its own right.

About This Book

This is not a book simply about Silverlight as a technology, and it isn't about covering Silverlight from end to end. Silverlight features such as streaming video, Deep Zoom, animations, sounds, and others that are not generally a core requirement in business applications will not be covered. This is a book specifically targeting the needs of designing and developing business applications, and how to do so when using Silverlight as a development platform. That said, you may be well versed in the process of business application design and merely be interested in how to apply your knowledge to developing business applications in Silverlight—this book caters to you too. You may even pick up a few things as you go!

You may have noticed that many books, web articles, and presentations take a rather simplistic view when discussing building business applications in Silverlight, and these techniques rarely hold up to scrutiny in real-world projects. Instead of just giving you all the pieces and leaving you to fit them together yourself, this book is designed to act as your guide when creating your own business application, leading you through the difficult problems you will encounter and providing one or more solutions for each problem. Business applications involve more than simply retrieving some data and displaying it on the screen—this book will allow you to peer into all the nooks and crevices that represent good business and application design practices.

This book is designed to be read from start to end rather than simply being a reference guide. Concepts will be introduced gradually to save overwhelming you with too much information at one time. This means that when some concepts are introduced, we will take shortcuts in other areas in order to focus your attention on the concept being taught, and move on to more robust implementations in subsequent chapters. Therefore, some of the Silverlight-specific lessons in early chapters are not designed to be put into immediate use, as they won't necessarily reflect all the recommended development practices in real-world projects, but are provided merely as a means of easing you into the world of Silverlight development.

Note

For example, the Model-View-ViewModel (MVVM) design pattern commonly used in Silverlight/WPF business application development is not covered until the later parts of the book. You will probably want to use this pattern in your applications, but the earlier chapters forgo this pattern in their examples as it introduces a lot of complexity that will potentially confuse you when attempting to focus on and convey other important concepts. Despite its position in the later parts of the book, incorporating this design pattern into your project should not be an afterthought. Therefore, this book should not be considered a step-by-step guide, but an all-encompassing guide to all the concepts important to building business applications in Silverlight.

By the completion of this book you will have learned most of the techniques for designing robust business applications and how to implement these techniques with a Silverlight-based project. While it would be impossible to cover each and every concept that could be employed when designing and developing business applications, this book will cover those that are the most important for developing a well-structured business application.

Unfortunately, it simply isn't possible to explore the complete myriad of scenarios that you may encounter in your application requirements, nor is it possible to cover every possible solution to a problem. The primary decision that needed to be made when writing this book was to decide on a single means for communicating between the server and the client. Due to the wide array of means for a Silverlight application to communicate with a server, it would be simply impossible to cover them all and do them justice. For this reason I chose to cover one technology in depth. For most end-to-end business applications in Silverlight (which is the primary focus of this book), RIA Services is your best option for handling communication between the Silverlight client and the server, and therefore this book will focus on using that technology as the framework for this task.

Note

If you decide RIA Services is not suitable for your needs, don't despair—the functionality covered in this book is not solely dependent on RIA Services, and there should still be large amounts of information useful to your project.

Note that you don't need to use every concept taught in this book in your own applications, as at times some of the concepts may be excessive (depending on the size and type of project you are working on) or inappropriate. These concepts will be noted with the scenarios that they are most suitable for, in order to help you make an informed choice.

About the Author

My name is Chris Anderson and I will be guiding you on your journey building business applications in Silverlight. Throughout my career as a developer I've designed and built many business applications across multiple platforms and a number of industries, including construction, finance, sales, logistics, and field services, to name just a few. However, despite all my development experience, I still faced a steep learning curve when I first came face to face with Silverlight. Back then, Silverlight was still in its infancy, but I could see its future potential as a platform for applications of all types—especially in the area of business application development. It had a small runtime, could be easily deployed, had a lot of exciting potential for rich user experiences, and best yet, had the power of the .NET Framework behind it (albeit a subset, but a substantial subset).

While it wasn't a vision shared by many people at the time, there was quite an interest in the topic when I started writing about it. I published a series of articles on the topic on SilverlightShow (www.silverlightshow.net) (for Silverlight 2 beta 2, and then Silverlight 2 RTW), but it was still somewhat hard going, with a lot of hurdles to jump and issues to work around. However, it was obviously a market that Microsoft saw potential in too, and with the beta release of Silverlight 3 there was suddenly a big focus on support for developing business applications (making much of my previous work redundant in the process). The vision had suddenly become reality.

Developing business applications in Silverlight has become substantially easier since those days, but you will still face a steep learning curve that will make getting up to speed a little slower than you might like (primarily in order to effectively use XAML to design your user interface). I've specifically structured and written this book to help you through that learning curve, guide you in the right direction, and solve the problems you are likely to encounter, so you can be working and productive in minimal time.

I welcome any and all feedback (preferably constructive), and you can contact me via my blog at http://chrisa.wordpress.com or on Twitter at @ChrisTheCoder.

Silverlight Overview

Microsoft has a number of different platforms for building applications, with Silverlight being the newest addition to the family. Let's take a look at what Silverlight is and how it fits into the big picture of software development platforms.

What Is Silverlight?

It's important at this stage to establish exactly what Silverlight is and what it isn't. Silverlight is an RIA platform. RIAs could be considered a cross between desktop applications and web applications. They run within the user's web browser (with some exceptions), and use a browser plug-in to run the application within a sandboxed environment. RIAs are not a new concept, although not all have been a success for various reasons. Adobe Flash is a notable exception, having achieved huge popularity over the last decade, as well as a market penetration of 99 percent of Internet-enabled personal computers in mature markets and 98 percent in the enterprise (according to Adobe's own statistics available here: www.adobe.com/products/player_census/flashplayer).

You will find a number of parallels between Silverlight and Adobe Flash. Both run as browser plug-ins, providing a platform for self-contained applications running on the client (as opposed to being dependent on a server controlling the application). Of course, there are numerous differences between the two technologies, but if you take a look at the space in which Silverlight is competing, Adobe Flash (combined with Adobe Flex) is without doubt Silverlight's biggest competitor.

Market Penetration/Reach

Although Adobe Flash has an almost ubiquitous market penetration, Silverlight is rapidly catching up. Scott Guthrie (corporate vice president, .NET Developer Platform) announced at PDC (in November 2009) that the market penetration was at 45 percent of all Internet-connected devices in the world (note that this is a broader measure than that of Adobe's), and at the launch of Silverlight 4 in April 2010 he announced that it was now approaching 60 percent. For in-house business applications, the issue of Silverlight's market penetration is of little concern, as the Silverlight runtime can be incorporated into the organization's standard operating environment (SOE) and deployed automatically to all the machines throughout the organization. However, for public-facing applications, you should be aware that not all client machines will have the Silverlight runtime installed. Deploying both the Silverlight runtime and your application effectively to users is detailed in Chapter 15.

Note

Another good source of statistics comparing the two platforms is www.riastats.com.

Focus

Silverlight's initial focus was toward media applications (directly targeting Adobe Flash's biggest market)—particularly in the area of video, vector graphics, and animations. However, it has more recently been focusing heavily on business applications, and is arguably the most powerful of the competing RIA platforms in this respect. With advanced support for data binding, a rich set of standard and supporting controls, and the RIA Services framework, it has also become a viable platform for many business applications.

Compatibility

The Silverlight browser plug-in works with all major browsers and across the two most popular operating systems—Microsoft Windows and Apple Macintosh (Intel x86–based machines only). The Moonlight project (by the team from Novell working on Mono, a cross-platform open source implementation of the .NET Framework) can be used to run Silverlight applications on Linux, but unfortunately its releases lag behind the Microsoft releases.

At the time of writing, there is currently no support for mobile operating systems such as Windows Mobile (which is superseded by the Windows Phone 7 mobile operating system), Apple's iPhone, Symbian (for Nokia devices), BlackBerry, or Google's Android, although there are future plans to provide support for some of these devices.

Windows Phone 7

The Windows Phone 7 operating system has a Silverlight-based user interface, meaning that most applications for it will be written in Silverlight. Applications need to be written specifically to run under this operating system, as it won't run standard Silverlight applications (i.e., you won't be able to browse to an application in the web browser and run it). Applications written for the phone are essentially Silverlight 3 based; however, this should be enough to enable you to port much of the same code you write for your business applications targeting Silverlight 4 to a Windows Phone 7 version at some point in the future. This will open up many exciting new possibilities for sharing a large portion of your code base (and development knowledge) between both your standard and mobile applications.

The Silverlight Runtime

The Silverlight runtime is a 6MB download (small by today's standards) including a subset of the .NET Framework and a full implementation of the Common Language Runtime (CLR) engine. This has the huge advantage of enabling you to write code in any .NET language, including (but not limited to) C# and Visual Basic—meaning that you can develop applications in exactly the same language that you are used to working with. Application code is compiled into assemblies, which (along with XAML files and other resources) are combined into a XAP file. This file can then be downloaded to the client and executed within the plug-in. Silverlight applications run within the browser are sandboxed (having extremely limited access to the host machine), meaning that Silverlight applications are inherently safe to be run and cannot cause any damage to the user's computer (by design).

Note

Silverlight applications cannot make use of assemblies that target the full .NET Framework, but assemblies targeting Silverlight can be used in applications that target the full .NET Framework.

Out-of-Browser (OOB) Experiences

While Silverlight applications are generally run from within a browser, from version 3 of Silverlight onward they can be run outside of the browser as if they were standard Windows or Macintosh applications. An icon can be placed on the desktop and/or the Start menu to open the application, and the application can automatically update itself when a new version is available on the server. The original implementation of OOB used the same sandboxing model as running the application in the browser, but in version 4 of Silverlight a new feature was added to allow the application to be installed with elevated trust permissions. This enables the application additional access to the host machine, and opens up a new range of possibilities to your Silverlight applications (when the application is being run in OOB mode). With trusted applications in Silverlight 4, you also have access to the Component Object Model (COM), which being a Windows-only feature breaks the truly cross-platform nature of the platform, but enables you to access many Windows features and integrate more tightly with other applications and the operating system.

In the .NET world, Silverlight's closest relative would be Windows Presentation Framework (WPF). Both use Extensible Application Markup Language (XAML) to define their user interfaces, and Silverlight essentially started as a web-deployed subset of WPF. If you are familiar with WPF, you will be able to write Silverlight applications reasonably easily (and vice versa), despite there being a number of features in each that are not available in the other (although they are becoming more aligned with each release of each).

A Short History of Silverlight

Silverlight has matured rapidly in its short three-year lifespan. Let's take a look at how it has grown in that time.

Before there was Silverlight, there was WPF. Introduced with version 3 of the .NET Framework, it targeted the rich client market, pioneered the use of XAML to define user interfaces, and demonstrated the new possibilities that XAML could enable. WPF applications could be deployed via a web browser (as an XBAP), but they required the full .NET Framework to be installed, and could only be run under Windows.

Silverlight 1: September 2007

Microsoft then started working on a project called WPF Everywhere (WPF/E), which was essentially designed to be a web-deployed subset of WPF that had its own runtime and ran within a browser plug-in. It used XAML to define the user interface and required you to write code in JavaScript. It had little in the way of controls, and was predominantly focused on media-type applications (video, animations, and vector graphics). This became Silverlight 1.

Silverlight 2: October 2008

With Silverlight 2, developers really started to become interested in Silverlight. It now had the CLR, a subset of the .NET Framework, and a decent set of standard controls, enabling developers to start writing rich Internet applications without needing to learn a new language.

Silverlight 3: July 2009

Silverlight 3 began to focus its use on developing business applications. It included richer data binding support, the DataForm control, validation controls, and the ability to run Silverlight applications OOB. In addition, although it was in beta, Microsoft simultaneously released a new framework called RIA Services, which provided a powerful and structured means for Silverlight applications to communicate with servers and pass data between them—making business applications easier to develop in Silverlight.

Silverlight 4: April 2010

Whereas Silverlight 3 immediately made creating business applications viable in Silverlight, Silverlight 4 smoothed many of the rough edges. Silverlight 4 introduced printing capabilities (resolving a big complaint by business application developers of a missing feature), implicit styling (making theming your application a lot easier), webcam and microphone support, elevated trust when running outside the browser, COM access, toast notifications, additional data binding capabilities, and more.

What Can Silverlight Bring to Your Business Application?

There are numerous platforms you could develop your business applications upon, so you may be wondering why you should develop business applications in Silverlight. The biggest benefits are undoubtedly the following:

  • The deployment model is easy—applications are delivered over the web via a web browser.

  • There's no need for the full .NET Framework—just a very small runtime.

  • Applications can be written in your favorite .NET language (C#, Visual Basic, etc.).

  • Applications can be run on multiple platforms (namely both Windows and Mac).

  • It's faster to develop and richer than HTML-based applications.

Combined, these benefits have created a platform that many developers hope can solve the various problems that they face every day. However, there are many more benefits that can be gained using Silverlight over other technologies.

If you are used to developing HTML-based applications (e.g., using ASP.NET), then you'll gain the following benefits:

  • You are no doubt used to having your applications render differently between browsers and operating systems. The big advantage of an RIA like Silverlight is that it renders in exactly the same way in each browser and operating system. This saves a lot of time testing and solving cross-browser issues.

  • Silverlight applications are richer than HTML applications and faster to develop.

  • Since Silverlight is a rich client, users don't have to deal with constant postbacks to the server, and the applications feel more responsive to the user.

  • With the CLR and a subset of the .NET Framework running on the client, you don't need to write in JavaScript, and you can reuse code from other projects in your Silverlight application.

  • Applications can be run offline.

  • Applications can be detached and run like a standard application.

If you are used to developing rich client applications (such as with Windows Forms), then you'll gain the following benefits:

  • Silverlight's use of XAML to define user interfaces enables you to create unique and flexible user interfaces that have extensive support for data binding, vector graphics, and animations.

  • RIA Services makes designing applications that communicate with a server clean and easy.

  • You will be able to share code with Windows Phone 7 and WPF applications.

  • Users can be set up with both the Silverlight runtime and your application with ease in a matter of minutes.

As you can see, Silverlight brings a lot to the table, and this book intends to demonstrate how it can lead to brilliant business applications. However, Silverlight is not always the best choice in some scenarios—let's take a look at those now.

When Should You Not Use Silverlight?

It's important to choose the technologies you use wisely and not just choose a technology because it's "cool" or for the sake of it—you need to justify your choice and ensure that the platform will meet the needs of the business, and that the business is obtaining a reasonable return on investment (ROI). As with any technology, Silverlight is not the perfect platform for all development projects. It has strengths and weaknesses that need to be weighed, evaluated, and considered in relation to the problem domain before you commit yourself (and your business!) to that technology. You need to select the most appropriate platform to suit the requirements of the project.

Comparing Silverlight Applications to HTML-Based Applications

In comparison to HTML-based applications, a big issue is Silverlight's reach. HTML-based applications can be run almost everywhere. Desktop PCs (including Linux) and Internet-capable mobile devices all display HTML (although a little differently at times), meaning that the reach of these applications is almost 100 percent. For machines that can run Silverlight (Windows, Macintosh, and to an extent Linux), the user is required to install a plug-in, which isn't always possible on locked-down machines in corporate settings (without getting the IT department involved).

Comparing Silverlight Applications to Rich Desktop Applications

In comparison to rich desktop applications, Silverlight applications have restricted functionality and features. Only a subset of the .NET Framework is available to Silverlight applications (although it is a fairly reasonable subset), and the capabilities and features of the controls are somewhat limited too, so you will find that some things tend to be more difficult (or impossible) to do in Silverlight than in the desktop counterpart. Silverlight is also not designed for integrating with other applications, the underlying operating system, or hardware devices. Silverlight applications are designed to be sandboxed, and although the elevated trust and COM features in Silverlight 4 enable you to get around many of these limitations, you do need to run the applications outside of the browser (with elevated trust) in order for those workarounds to be possible.

These are the primary considerations you will have to take into account before choosing Silverlight as a platform. Determine the current and potential future requirements of your application, identify any features that conflict with Silverlight's limitations, and then decide whether Silverlight is really the best-suited platform for your needs.

Comparing Silverlight to Other Microsoft Platforms

Let's compare Silverlight with the other core platforms Microsoft provides and identify the primary pros and cons of each.

WPF

Shared features:

  • Both use XAML for defining user interfaces (with Silverlight essentially a subset of WPF).

  • Both can be deployed via a browser as sandboxed applications.

Pros:

  • WPF has minimal restrictions, and it requires no sandbox (unless deployed via a browser as an XBAP without full trust turned on).

  • WPF has the power of the full .NET Framework behind it.

Cons:

  • WPF requires the full .NET Framework to be installed on the machine.

  • WPF is limited to running on Windows only.

  • WPF does not have the hype and community/Microsoft support that Silverlight has.

Windows Forms

Shared features:

  • Both can be downloaded via a browser (although Windows Forms applications cannot be run within the browser like Silverlight applications) and update themselves automatically when a new version is available (using click-once with Windows Forms applications).

Pros:

  • Windows Forms has minimal restrictions and no sandbox.

  • It has the power of the full .NET Framework behind it.

  • It is very easy to develop (no need to understand XAML).

  • It is mature, having been around since the inception of the .NET Framework.

  • It requires no graphical skills to design user interfaces with the standard style of Windows applications.

Cons:

  • It requires the full .NET Framework to be installed on the machine.

  • It is limited to running on Windows only.

  • It is an aging platform with waning support.

  • Data binding capabilities aren't as rich.

  • User interfaces aren't as flexible and don't have the same styling capabilities.

ASP.NET

Shared features:

  • Both ASP.NET and Silverlight are available via a web browser (for Silverlight applications, the Silverlight plug-in must be installed first).

Pros:

  • ASP.NET runs on almost all Internet-connected devices.

  • It does not require a plug-in to be installed.

  • It has the power of the full .NET Framework behind it (server side only).

  • It is mature, having been around since the inception of the .NET Framework.

Cons:

  • It requires the full .NET Framework to be installed on a Windows server running IIS (although the Mono project provides a potential alternative).

  • Client-side code must be written in JavaScript, requiring learning a different language.

  • It can render differently between browsers, requiring a lot of testing.

  • Data binding capabilities aren't as rich.

  • It isn't as rich or responsive (although Ajax and jQuery can help).

  • It cannot run with elevated trust to integrate with other software and hardware.

Comparison with Adobe Flash/Flex

With Adobe Flash/Flex being Silverlight's biggest competitor, it's worth comparing the two (at a high level).

Similar features:

  • Both are rich Internet application platforms deployed via a web browser.

  • Both can run applications outside of the browser (Silverlight with no additional requirements, Flash with Adobe Air).

  • Both use XML-based markup languages to define their user interfaces.

Pros:

  • The Flash runtime is almost ubiquitous, installed on almost all Internet-capable PCs.

  • The core Flash runtime is a smaller download (1.8MB vs. 6MB), although running outside the browser requires the Adobe Air runtime (which is 15MB).

Cons:

  • Code must be written using ActionScript, a JavaScript derivative, which is not as capable or popular as managed code languages (e.g., there's no equivalent to LINQ and generics), and is specific to Flash/Flex (i.e., code can't be reused with other applications).

  • It requires an additional download to run applications outside the browser (Adobe Air), and applications must be packaged separately to run in this runtime (whereas the same Silverlight application will run both within and outside the browser).

Business Applications Overview

Business applications—sometimes also referred to as line-of-business (LOB) applications—are core applications critical to the running of a business. Businesses the world over rely on software in order to function, much of which has been custom developed to capture and support the unique processes and needs of the business—without this software, they would not be able to function effectively, or even at all.

Business applications tend to have the following features in common:

  • The ability to access and maintain data in a central data store

  • Server/client communication

  • A workflow providing a structured means of entering/modifying that data

  • A user interface providing an efficient means of entering/modifying data

  • Business rules

  • Data validation rules

  • The ability to obtain information from the system (via reports)

  • Security

Each of these facets of business applications will be investigated in this book, with information on how to implement each in Silverlight to produce a complete end-to-end business application.

Summary

Now that we've discussed both Silverlight and business applications separately, it's time to put the two together and start learning how to build business applications in Silverlight.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset