2/15/2013 7:10 PM
LightSwitch in Action
By Dan Beall and Greg Lutz
You tell LightSwitch what you want the data to look like and what you want the interface to do and it wires it all up and gives you a finished, professional-grade application. LightSwitch is, at its essence, a forms-over-data application. You describe the data store and design the forms and it does all the plumbing required to make them work together. This article, based on chapter 1 of LightSwitch in Action, explains why and how you would want to use LightSwitch.
What is LightSwitch?
LightSwitch’s official name is Microsoft Visual Studio LightSwitch. It is actually part of Visual Studio 2012; a project type, in fact. It provides visual designers to specify a data model and interface and then automatically generates code for you. You are not really developing software; you are modeling it. LightSwitch does the development for you. For example, you may need to connect to an accounting database to create a simple application that monitors sales information. LightSwitch will help you connect to the source, choose the data you need to view, and design an interface that formats it on the screen. You would do all of this in point-and-click menus, not code. When you are ready, LightSwitch generates the necessary code, wires up all the data interactions for you, and gives you an application. It is a very safe way to interact with critical corporate data.
LightSwitch is all about data. In fact, it creates CRUD (create, read, update, and delete) applications. These applications can CRUD in newly created or existing databases. LightSwitch gives you the tools to create new or connect to existing databases and to design the interfaces that interact with that data. Many people use LightSwitch to tap into corporate data created by other systems, as described in the accounting data example above. People also often need to create applications from scratch where there is no existing data. In this case, LightSwitch helps you build both the database and the application that interacts with that database.
Your applications are exactly what you want: you get them quick, and you get them cheap. Usually quick and inexpensive means low quality, but not in this case. LightSwitch uses the latest development standards and Microsoft’s extensive code library, so the applications you create are always based on best practices.
Wait a second! This violates the “fast, cheap, and good” triangle. You know the one. You can only pick two sides, can’t have all three. Well, LightSwitch does have its limitations, but they are few.
You now know basically what LightSwitch is; a development tool that helps you build business applications. But, you can’t truly be convinced of its benefit until you know what it outputs and what you can build with it. Once you understand that, the value to your daily life will become clear.
What is a LightSwitch application?
Let’s say it again: It is all about data. If it is data based, you can probably build it. LightSwitch helps you build tools to gather, manage, edit, store, track, and view data from various sources. A simple example would be an address book or an inventory list. A more complex (and more useful) example would be a complete work tracking system with one interface field technicians use on tablets and another that office workers access on PCs. Until now, the only way to generate applications like these was for a professional developer to build them using complex coding languages.
Before we make you think you can take over the world, let’s look at the other side of the coin. There are some things LightSwitch can’t or shouldn’t do. LightSwitch is designed with internal IT needs in mind. People typically build applications for their own use, not for commercial sale. LightSwitch isn’t good for building applications that aren’t based on data. For example, you won’t want to produce a word processor or a video game. LightSwitch also isn’t ideal for building large-scale enterprise applications. By large scale, we mean the types of applications that handle millions of transactions per day or need to process a million-record database on one screen. Don’t get the wrong idea. LightSwitch is based on Microsoft standards and is a secure as anything else, so it can be used in serious business settings. Use table 1 as a reference to decide when to use LightSwitch.
Table 1 When to use LightSwitch and when to use traditional Visual Studio projects
LightSwitch in Action
In late 2011 and early 2012, a popular developer’s forum called The Code Project teamed up with Microsoft to hold the LightSwitch Star contest (see http://www.codeproject.com/Competitions/524/LightSwitch-Star-Contest.aspx). This contest gathered a number of great entries, most of which are real applications in use by the developers. You can see the entries for an idea of what people have are using LightSwitch for.
LightSwitch doesn’t only produce finished applications. Its data modeling technology is so strong that many developers use it to create an OData service to be consumed by other applications. Some developers also use it to prototype larger applications. They either use it to create a mockup or the start in LightSwitch and then extend with a traditional Visual Studio project when they are ready.
It seems like LightSwitch can do a lot, huh? This is because its real strength lies in the data model and services it creates to connect to and interact with that data. Let’s take a look at the architecture to illustrate this further. Figure 1 shows that there are three distinct parts of a LightSwitch application. LightSwitch exposes data (that it can help you create) through WCF services and it creates clients that consume, or use, those services.
Figure 1 The anatomy of a LightSwitch application
Let’s define each layer just a little more:
- Data—This is the foundation of your application and it always one central repository. It is a network or Internet connected SQL server instance. LightSwitch can create this or you can use an existing one.
- Services—LightSwitch uses Windows Communication Foundation (WCF) services to handle the interactions between the application interface and the data. WCF is a standard set of APIs provided by Microsoft that developers use to connect interfaces to application servers. These services are normally hosted in a Web server, but can sometimes be installed on users’ desktops.
- Clients—Clients are the interfaces that end-users see. You can have multiple clients that use, or consume, the services. Clients can be installed on desktops or hosted on the Web.
In short, all LightSwitch projects produce (or connect to) a database and create standard services that make the data available for consumption by applications. Most LightSwitch projects also produce clients that end-users use. Some LightSwitch projects produce OData services instead of clients.
Desktop and HTML clients
Mobile devices get all the attention these days, but the majority of information work these days is still conducted on desktop and laptop computers. LightSwitch produces applications that can run on both Windows and Macs by building applications in Silverlight. If you’re not familiar with Silverlight, think of it as being similar to Adobe Flash or AIR. It’s a media-rich library that allows the same program to run on multiple operating systems by using a freely available plug-in viewer.
Silverlight can run over the Web in all major browsers as well as locally on the desktop. If it runs on the desktop, it runs in its own viewer (just like Flash) and if it runs on the web, it just runs in the browser. As a result, LightSwitch can generate applications for hosting on the Web or for installation on the desktop. In either case, they look and function very much the same (as you can see in figure 2). This is called the desktop client.
Figure 2 These two applications look and function identically except on is running in a browser and the other is running in the Silverlight desktop viewer.
In the Web hosted case, the application only needs to be installed once, on a Web server. Any number of users can access it with a browser. If you create a desktop application, it needs to be installed on each user’s machine. In either case, all the deployment work is done for you.
LightSwitch can also generate applications designed for use on mobile devices.These are actually Web applications designed to be accessed by mobile browsers. These applications are built using jQuery Mobile and HTML5, the latest and most popular Web technologies. If you’re not familiar with either of these technologies, you just need to know this: LightSwitch can build a Web application made up of pages styled for the smaller screens of tablets and smartphones and optimized for touch interactivity (figure 3 is an example). Because the LightSwitch mobile applications use the latest Web technologies, they will be compatible on virtually all mobile devices. This includes iPads, iPhones, Android devices, most Blackberries, Windows Phones, and Windows tablets. These mobile Web applications are called HTML clients.
Figure 3 Microsoft’s public mobile LightSwitch application example running on an iPad.
These applications are only built for hosting on the Web. LightSwitch won’t build native apps for installation on devices. In other words, you can’t use LightSwitch to put an iPad app on the iTunes App Store, but you can deploy a web application that the iPad (or virtually any other device) can run through a browser.
Take note that you can produce both desktop and mobile applications at the same time, from the same project, reading the same data. Remember the example above where you may need a desktop interface for office workers and another for field workers? In traditional development processes, you would have two projects, one for the technicians’ mobile use and another for desktops. LightSwitch enables you to single-source these applications and save a ton of time.
Like we hinted earlier, you don’t have to produce a client. You can tell LightSwitch to publish an OData Service. OData is a newer technology that makes it easy to read, write, and update data over the Web. LightSwitch’s data modeling technology is so powerful that some people use it to publish OData services to be consumed by other applications. Say you need pull data from a couple corporate systems, adds some of your own, and tie it up in a nice, package. LightSwitch’s data modeling tools will help you do that and you can take it a step further. You can then publish an OData service so other developers can easily consume it.
When you build an OData service, you are basically publishing an application without the clients, but giving other developers an easy and standard way to consume your data. Remember our three-tiered architecture we described earlier? When you create OData services, you are creating the bottom two tiers and leaving the client layer up to others (as illustrated in figure 4).
Figure 4 When creating OData services, you are creating a LightSwitch application without clients.
OData is one of the newest features of LightSwitch and it is very important to developers.
Now you know LightSwitch can produce two types of clients, Desktop and HTML, and OData services. There are two more things you can do: create SharePoint and Windows Azure applications.
SharePoint and Azure Applications
SharePoint is often used as a host for data-centric business applications, so it only makes sense that LightSwitch would help create them. SharePoint is actually a publishing target; you can create a SharePoint application by publishing the Desktop or HTML client to a SharePoint site. Similarly, you can also deploy a LightSwitch application to Windows Azure, which is Microsoft’s cloud version of Windows.
You now have the fundamentals you need to build a complete understanding of LightSwitch. The following are some takeaways from this article:
- LightSwitch generates applications with desktop and/or HTML clients or OData services.
- You use LightSwitch to describe your data and interface and LightSwitch generates the code to wire it all up.
- LightSwitch has benefit for users of all technical levels. Developers can use it to save time and still produce standards-based applications. Non-developers can use it to avoid having to code.
Here are some other Manning titles you might be interested in:
Last updated: February 14, 2013