What's Lightswitch?
LightSwitch maps screens (forms) to database tables (data source entities). It's easy to query and edit database tables through the screens (GUI).
Data entity query
Data source entities can incorporate queries (filters, sort and parameters). A data source query parameter will be shown as an input text box at run time on the screen that maps to the data source.
Screen query
Screen can incorporate queries as well. A screen parameter is passed from a local property or a local data entity. Query parameter ->binds to -> a data item (local property). If a screen is configured with a parameter, it can not run by its own. It needs to be invoked from another screen. To pass a parameter value from one screen to another screen, create a command bar button, and put in Execute code for the button:
this.application.show[TargetScreenName](this.LocalDataEntity.XXX_ID)
A data item can be a query. The query can also incorporate parameters (from screen parameter / another data item/entity attribute). The data item can be shown as an independent table on the screen.
User permission:
Right click the project -> properties-> Access Control, put in authentication type (windows / form). With windows authentication it checks the current windows account, and with form authentication a user needs to type in username and password.
On the same pane, define permissions: e.g. CanRead, CanWrite, CanDelete, etc. For every data entity, there are XXX_CanRead(), XXX_CanInsert(), XXX_CanUpdate(), ... functions. These functions define the permission check for reading, inserting or updating the database table. So in the functions, check if the user has the correct permission, e.g.:
partial void Customers_CanUpdate(ref bool result)
{
result = this.Application.User.HasPermission(Permissions.CanWrite);
}
If a user doesn't have the permission required, the data rows on the screen will be grayed out and can not be updated.
Similarly, a screen has XXX_CanRun() function, If it checks user permission and a user doesn't have the right permission, the user can not see the screen.
A command button can define XXX_CanRun() function too.
A user is mapped to a Role, and a Role is mapped to permissions defined in the Access Control pane. This is done after the Lightswitch app published by the Administrator.
App deployment:
There are basically three types of deployment.
1. Desktop client, 2-tier. This deployment scenario creates an application that runs on the end-user’s Windows desktop. The database and server components run on a networked computer.
2. Desktop client, 3-tier. This deployment scenario creates an application that runs on the end-user’s Windows desktop. The database and server components run on a server that is running Internet Information Services (IIS) or on Windows Azure.
3. Browser client, 3-tier. This deployment scenario creates an application that runs in the end-user’s web browser. The database and server components run on a server that is running IIS or on Windows Azure.
Table must have primary key:
Every table used by LightSwtich has to have a primary key. I reckon Lightswitch rely on the primary key to update/insert/delete, etc. If a table doesn't have a primary key, the data source wouldn't detect the table as an available table.
Show data from a referenced table:
If table A references table B (reference constraint in database or virtual relationship defined in data source), the screen for A will include the 'Summary Property' of table B as a auto complete combo box (drop down list). E.g Employee table references to Department table, and the Department Name is the summary property of the Department entity. When create a Employee screen, the Department name property will be included in the screen as a drop down list.
Lightswitch detects the summary property automatically, but you can change it later from the Property pane of the entity. To show more attributes from the referenced table, e.g. Department.Location, simply "Add" "Other Screeen Data" to the Data Grid Row.
Hide an attribute from the screen.
You can uncheck the "Is Visible" for an attribute to hide it from the screen, but it still shows when you "+" add or edit a row (on a new pop up screen). Simply go to the data entity, and uncheck "Display by default" for the attribute then it won't appear on any screen.
Drop down list.
From the data entity, the attribute property pane has an option to provide a "Check List" of values. In the screen that uses the data entity, can change the attribute type to Auto Complete Combo (drop down list).
Cascade Delete.
Edit the property of the relationship between data entities. There is an option for On Delete Behavior, so one can choose to cascade delete or stop user from deleting if a record is referenced by others.
Notice that if a relationship is imported from the external database (the physical foreign key reference), the On Delete Behavior is not editable, which means it cannot cascade delete if that is prevented by the external database (e.g. MySql, SQL server). The user will be prompted an error message complaining the reference check. In this case, need to override the XXX_deleting method and/or the XXX_Saving method to implement cascade delete.