Inheritance in Visual Studio LightSwitch 2011


Introduction

Inheritance is a concept of Object Oriented Programming. Inheritance is a mechanism of deriving a new class from a base class. A derived class can inherit the properties of a base class.

In this article you will see inheritance among a tables. In this article I make a parent table (Vehicles) and their child tables Car, Bus, Bike. The child table inherits the properties of the parent table.

Step 1 : First of all open Visual Studio LightSwitch->New project->Select LightSwitch application->Write name of project->Ok.

first.png

Step 2 : Click on create new table.

create.png

Step 3 : Create a parent table with the name Vehicles. Go to properties of type->Click on choice list->Add value->ok.

1.png

Step 4 : Now we will create child tables like Bike, Car, Bus.

image3.png
image4.png
lats.png

Step 5 : Now we will make a relationship among these tables. Like as in the following images.

2.png

3.png

4.png

Step 6 : When we establish relationships among these tables then the vehicles tables will look like as in the following image.

5.png

Step 7 : Now we will add a screen. Right-click on screens-Add screen.

6.png

Step 8 : Select New Data Screen->Select screen data (Vehicles)->Ok.

7.png

Step 9 : Now we will a add details screen for Car, Bike, Bus.

Go to Solution Explorer->Right-click on screen->Add screen->Select Details screen->Change screen name->Unmark checkbox for default details screen->Ok.

In the same manner we will create a Bus details screen and a Bike details screen.

8.png

Step 10 : Now we will add a search screen. Go to Solution Explorer->Right click on screen->Add screen->Select search data screen->Select screen data (Vehicles)->Ok.

9.png

Step 11 : Now we will add a button. Right-click on Command bar->Add button.

10.png

Step 12 : Click on new method->Write name (edit)->Ok.

11.png

Step 13 : Expand edit->Select link.

12.png

Step 14 : Right click on edit button->Select edit execute code->Write code.

13.png

Code

using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
    public partial class SearchVehiclesSet
    {
        partial void Edit_Execute()
        {
            // Write your code here.\
            if (this.VehiclesSet.SelectedItem != null)
            {
                switch (this.VehiclesSet.SelectedItem.Type)
                {
                    case "Car":
                        this.Application.ShowCarDetail(this.VehiclesSet.SelectedItem.Id);
                        break;
                    case "Bus":
                        this.Application.ShowBusDetail(this.VehiclesSet.SelectedItem.Id);
                        break;
                    case "Bike":
                        this.Application.ShowBikeDetail(this.VehiclesSet.SelectedItem.Id);
                        break;
                }
             }
         }
    }
}

Step 15 : Go to CreateNewVehicles Screen->Click on write code->Select CreateNewVehicles_Saved->Write code.

14.png

Code

using System;
using System.Linq;
using System.IO;
using System.IO.IsolatedStorage;
using System.Collections.Generic;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Framework.Client;
using Microsoft.LightSwitch.Presentation;
using Microsoft.LightSwitch.Presentation.Extensions;
namespace LightSwitchApplication
{
    public partial class CreateNewVehicles
    {
        partial void CreateNewVehicles_InitializeDataWorkspace(global::System.Collections.Generic.List<global::Microsoft.LightSwitch.IDataService>
saveChangesTo)
        {
            // Write your code here.
            this.VehiclesProperty = new Vehicles();
        }
        partial void CreateNewVehicles_Saved()
        {
            // Write your code here.
            this.Close(false);
            Application.Current.ShowDefaultScreen(this.VehiclesProperty);
            switch (this.VehiclesProperty.Type) {
                   case "Car":
                             this.Application.ShowCarDetail(this.VehiclesProperty.Id);
                             break;
                   case "Bus":
                             this.Application.ShowBusDetail(this.VehiclesProperty.Id);
                             break;
                   case "Bike":
                             this.Application.ShowBikeDetail(this.VehiclesProperty.Id);
                             break;
        }
    }
}

Step 16 : Go to vehicles tables->Click on write code->Select VehiclesSet_Inserting->Write code.

15.png

Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LightSwitch;
using Microsoft.LightSwitch.Security.Server;
namespace LightSwitchApplication
{
    public partial class ApplicationDataService
    {
        partial void VehiclesSet_Inserting(Vehicles entity)
        {
            switch (entity.Type)
            {
                case "Car":
                    if (entity.Car == null)
                    {
                        entity.Car = new Car();
                    }
                    break;
                case "Bus":
                    if (entity.Bus == null)
                    {
                        entity.Bus = new Bus();
                    }
                    break;
                case "Bike":
                    if (entity.Bike == null)
                    {
                        entity.Bike = new Bike();
                    }
                    break;
            }
        }
    }
}

Step 17 : Run application (Press F5). Click on create new vehicles->Fill data->Save.

16.png

When we have filled data in the new vehicles then the search vehicles looks like in the following image.

17.png

Step 18 : Go to right corner of application->Click on design screen->Expand vehicles set->Select list->Save

18.png

Step 19 : Now we can edit any vehicles set. Click on edit button for edit any vehicles set.

19.png

Conclusion

So in this article you saw how to apply the inheritance concept to tables. Inheritance is a primary concept of the object oriented approach.

Some Helpful Resources

Up Next
    Ebook Download
    View all
    Learn
    View all