Apr 11

Written by: Michael Washington
4/11/2011 5:49 AM  RssIcon

image

LightSwitch is designed for “forms over data”, entering data into tables, and retrieving that data. Procedural code is required when you need to manipulate data in “batches”. Normally, you want to put all your custom code on the Entity (table) level, but for procedural code, you want to use custom code on the Screen level.

The Inventory Program

To demonstrate procedural code in LightSwitch, we will consider a simple inventory management requirement. We want to specify sites and books, and we want to move those books, in batches, between the sites.

image

We enter sites into a table.

image

Each book is entered into the database.

image

We create one record for each book, and assign it to a site (yes a better interface to do this could be created using the techniques explained later).

Inventory Management (non-programming version)

image

Even procedural programming is simply moving data around. If you are not a programmer, you can simply make a screen that allows a user to change the site for each book. The problem with this approach is that it would be time-consuming to transfer a large amounts of books.

Inventory Management (using programming)

image

Using a little programming, we can create a screen that allows us to update the book locations in batches.

image

First we add a Screen.

image

We give the Screen a name, we select New Data Screen, and we do not select any Screen Data.

image

We Add Data Item.

image

We add a Property that will allow us to select a book.

image

It will show in the Screen designer.

image

We right-click on the top group, and select Add Group.

image

We are then able to add Selected Book to the screen as a drop down.

image

If we run the application at this point, we see that it allows us to select a book.

image

We add the various elements to the Screen.

image

 

Here is the code we use for the Get Existing Inventory button:

 

partial void GetExistingInventory_Execute()
{
    if (SiteFrom != null)
    {
        SiteOneBooks = SiteFrom.BookInventories.Where(x => x.Book == SelectedBook).Count();
    }
    if (SiteTo != null)
    {
        SiteTwoBooks = SiteTo.BookInventories.Where(x => x.Book == SelectedBook).Count();
    }
}

 

image

 

Here is the code for the Transfer Inventory button:

 

partial void TransfeerInventory_Execute()
{
    // Ensure that we have all needed values
    if ((SiteFrom != null) && (SiteTo != null) && (BooksToTransfeer > 0))
    {
        // Get the Books from SiteFrom
        // Loop based on the value entered in BooksToTransfeer
        for (int i = 0; i < BooksToTransfeer; i++)
        {
            // Get a Book from SiteFrom that matches the selected Book
            var BookFromSiteOne = (from objBook in SiteFrom.BookInventories
                                    where objBook.Book.BookName == SelectedBook.BookName
                                    select objBook).FirstOrDefault();
            // If we found a book at SiteFrom
            if (BookFromSiteOne != null)
            {
                // Transfeer Book to SiteTo
                BookFromSiteOne.Site = SiteTo;
            }
        }
        // Save changes on the ApplicationData data source
        this.DataWorkspace.ApplicationData.SaveChanges();
        // Set BooksToTransfeer to 0
        BooksToTransfeer = 0;
        // Refresh values
        GetExistingInventory_Execute();
    }
}

 

The download for the complete project is here:

http://silverlight.adefwebserver.com/files/LightSwitch/LightSwitchBookInventory.zip

1 comment(s) so far...


Gravatar

Re: LightSwitch Procedural Operations: “Do This, Then Do That”

Very good. Nice example of how to do some procedural code, and working through the screen designer with the download open really expanded my understanding of how to put a screen together with the controls and data of my choosing.

By Richard Waddell on   4/14/2011 6:32 PM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 
Microsoft Visual Studio is a registered trademark of Microsoft Corporation / LightSwitch is a registered trademark of Microsoft Corporation