CRUD LightSwitch in five Minutes (or less)

Summary:
Objective: Create usable CRUD with LightSwitch in few minutes.

Methods: With a few changes to the design of the screen and some quick snippets can configure a Detail List screen for use as custom CRUD.

Results Allow create usable CRUDS.

Conclusions:LightSwitch automatically generates screens to edit or add records, initially say wow!, But minutes later just thinking it would be better to have more control over this.
The acronym CRUD. (C) reate, (R) ead, (U) pdate, (D) elete tables allow us to maintain our database.
We can make custom screens (Details Screen) or (New Screen) to override the automatic behavior, but this for me almost always has disadvantages:

Disadvantages basic technique.
– We must create additional screens to update and add many screens with few content of use that fill our screens list.
– We generate screens are generated as tabs rather than modal windows. It’s pretty ugly to see a tab with just one or two fields.
– For example, 50 data tables 150 equivalent to maintenance screens.
– It’s convoluted and I think you lose a lot of productivity LightSwitch
– We should only do so if it were justifiable.

What is known?
Here you can see other ways to solve the problem:

http://msdn.microsoft.com/en-us/vstudio/ff945359
http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/33/LightSwitch-ndash-Modal-Add-and-Edit-Windows.aspx
http://msdn.microsoft.com/en-us/vstudio/ff949859.aspx

Minute 1
Create a LightSwitch Application



Minute 2
Create Table

Create Screen List Details

Minute 3
Customize Screen:












Minute 4
Add constant string

private const string ADD_POPUP_WINDOW = "addNew";
 

Copy this snippet without changes to class

 /* Copy without changes */    
        private void CloseAddSubscriberWindow(){
            this.CloseModalWindow(ADD_POPUP_WINDOW);            
            this.FindControl(ADD_POPUP_WINDOW).IsVisible = false;
        }
        void AddSubscriber_WindowAvailable(object sender, ControlAvailableEventArgs e)
        {
            this.FindControl(ADD_POPUP_WINDOW).ControlAvailable -= new EventHandler<ControlAvailableEventArgs>(AddSubscriber_WindowAvailable);
            ((ChildWindow)e.Control).Closed += new EventHandler(AddSubscriber_Closed);
 
         }
        void AddSubscriber_Closed(object sender, EventArgs e)
        {
 
             ((ChildWindow)sender).Closed -= new EventHandler(AddSubscriber_Closed);
 
             if (!((ChildWindow)sender).DialogResult.HasValue)
                CancelAddSubscriberWindow();
                CloseAddSubscriberWindow();
        }

Add reference to Windows.Controls

Copy or modify this snippet.

         partial void PersonsListEditSelected_Execute()
        {            
            this.OpenModalWindow(ADD_POPUP_WINDOW);
            this.FindControl(ADD_POPUP_WINDOW).DisplayName = "Editar Persona";
            this.FindControl(ADD_POPUP_WINDOW).ControlAvailable += new EventHandler<ControlAvailableEventArgs>(AddSubscriber_WindowAvailable);
       

        }
        partial void PersonsListAddAndEditNew_Execute()
        {
            this.PersonsSet.AddNew();
            this.OpenModalWindow(ADD_POPUP_WINDOW);
            this.FindControl(ADD_POPUP_WINDOW).DisplayName = "Nueva Persona";
            this.FindControl(ADD_POPUP_WINDOW).ControlAvailable += new EventHandler<ControlAvailableEventArgs>(AddSubscriber_WindowAvailable);
       
        }

Minute 5
Copy or modify this snippet.

  private void CancelAddSubscriberWindow()
        {
            PersonsSet.SelectedItem.Details.DiscardChanges();
        }

Copy or modify this snippet.
Replace btnCancel & btnSave with this:

 partial void btnCancel_Execute()
        {
            CancelAddSubscriberWindow();
            CloseAddSubscriberWindow();
        }
 
         partial void btnSave_Execute()
        {
            this.Save();
            CloseAddSubscriberWindow();
        }
 

Now we have a window to add or edit, without altering the other functions.



Known Issues.
Currently there are no known issues.

Discussions
I think it’s an easy way to control simple screens. Do not think this is the best method but for now it works for me.

Acknowledgements
Special thanks to all the LightSwitch community, especially to Richard Waddell and his post LightSwitch–Modal Add and Edit Windows this have been very helpful to write this post.
Thanks to Ramón Barrena for the review of this article.

Deja un comentario