Pages

Friday, 11 March 2011

Creating/Developing Business Application in 15 Minutes using lightswitch

From Zero to Business Application in 15 Minutes
Visual Studio LightSwitch is a new product in the Visual Studio family aimed at developers who want to quickly create data-centric business applications for the desktop and the cloud.
L
> >ightSwitch is an extensible development environment and application framework that simplifies the development process because it lets you concentrate on the business logic and does a lot of the remaining work for you. LightSwitch is perfect for small business or departmental productivity applications that need to get done fast. In this article I will walk you through creating a business application from beginning to end using Visual Studio LightSwitch.
A few years ago we had a lot of construction work done on our house to modernize the exterior. We updated all the siding, roof, windows, built a deck and did a bunch of landscaping. Our contractor would take tons of digital pictures for the before and after shots; so many that he would have mini SD cards lying all over his truck. He had a huge problem organizing all the pictures for all the construction projects he was managing. I told him I could write him an application to manage all the photos on each of his construction projects. At the time I used Windows Forms but this would have been a perfect small business application for Visual Studio LightSwitch. In this article I will quickly rebuild the application using LightSwitch. Let’s get started!
Creating a LightSwitch Project
When you open Visual Studio LightSwitch and go to the File menu and choose New Project (or click “New Project…” on the start page), you will see that the environment is streamlined and the only decision you need to make up front is what programming language you want to use, Visual Basic or Visual C#. For this project I will use Visual Basic and name the project ContosoConstruction as shown in Figure 1. This sets up the necessary project folders and files on disk, populates the Solution Explorer project system and opens up the LightSwitch designer.
Click for a larger version of this image.
Figure 1: When you create a new LightSwitch project, the only decision you need to make up front is what programming language you want to use. Under the covers, LightSwitch projects are always logically three-tier applications and use n-tier best practices and patterns as well as familiar .NET technologies like Entity Framework, Silverlight and RIA services.
Defining Your Data
The first step to building any LightSwitch application is to define the data tables, fields and relationships. After you create the project, you decide whether you want to start by creating a new table or if you want to connect to an existing data source, as shown in Figure 2. You can connect to databases like SQL Server or SQL Azure or any other database in which you have an Entity Framework provider installed. You can also connect to SharePoint or a custom RIA service. You can add more data sources later and even relate them together. This is a compelling feature of LightSwitch; you can federate multiple data stores, relate them together, and LightSwitch will handle selecting and updating the multiple data sources for you.
Click for a larger version of this image.
Figure 2: Once the project is created you need to define your data by creating new tables and/or attaching to an existing data source. For this project I will need to create a new database so I will choose “Create New Table.” This opens up the Entity (table) Designer which allows you to define the properties (fields) as well as relationships between entities. When you select the data type of a property you will see that there are the basic storage data types you would expect like string, decimal, integer, datetime, etc. However, you’ll also notice that there are data types like Image, PhoneNumber and EmailAddress in the list. These are called custom business types and even though they are stored in the database using the basic storage types, they come with additional validation, formatting and editors for free. You can also create business types of your own through the LightSwitch extensibility model.
Figure 3 shows the designer with a Customer entity open. Notice that you only see one entity at a time in the designer with its direct relationships. To open another entity, click on the entity end of the relationship. You can also click on any table name in the Solution Explorer.
Click for a larger version of this image.
Figure 3: The Entity Designer is where you describe the tables, fields, business types and relationships as well as a variety of formatting and validation information. The Entity Designer is also where you write any validation rules or specify computed fields. Depending on the data type, the properties window will also display declarative validation rules that you can specify without writing any code. You can specify things like whether a field is required, its maximum length, if it should be unique, whether it is searchable, as well as numerical and date ranges and other formatting and rule enforcements.
For this application I am going to need three entities; Customer, Project and Picture. Tables 1, 2 and 3 describe the properties, data types and any non-default maximum lengths we need.
For the Customer’s State property I want to create a choice list. A choice list lets you specify a fixed set of values that the user must select from. For this construction business in California I will limit the number of valid values to CA and bordering states, AZ, NV, OR.
Another alternative to using a choice list here would be to add some custom validation that uses a regular expression to check valid state codes. I will show you how to add custom validation rules later; for now, just enter the choices by clicking the “Choice List” link in the properties window and entering value and display name pairs.
Summary properties are also specified here in the Entity Designer by selecting the entity itself (click the name in the blue title bar of the entity) and indicating the Summary Property in the appearance section of the properties window. Summary properties are used to describe the entity type. This determines what to display when a row of data is represented on a screen using the summary control. By default the first string property is automatically set as the summary property for you. This works nicely for the Project table but for Picture I will set the summary property to LastUpdated.
For Customer, I will add a computed field for the summary property called FullName by clicking on the “Computed Property” button at the top of the designer. This sets the Is Computed property in the properties window for you and an “Edit Method” link appears. This method is where you return the value of the computed field. Computed fields are not stored in the underlying database; they are computed on the entity and only live in the data model. For FullName, I will return “LastName, FirstName”.
Public Class Customer

    Private Sub FullName_Compute(
                ByRef result As String)

       ' Set result to the desired field value
        result = Me.LastName + ", " + Me.FirstName
    End Sub
End Class
I also need to set up relationships so that a Customer can have many Projects and a Project can have many Pictures. To set up the relationships, first select the Project entity and click the “Relationship…” button at the top of the designer to open the Add New Relationship dialog box. Select Customer in the To column and specify the multiplicity and delete behavior as shown in Figure 4.
Click for a larger version of this image.
Figure 4: Setting up relationships between tables is easy - just specify the multiplicity and delete behavior you want. Repeat the same process for Picture by selecting the Picture entity and add a new many-to-one relationship to Project.
Hope this helps,happy coding.

0 comments:

Post a Comment

 
Powered by Blogger