Sep 19

Written by: Michael Washington
9/19/2010 4:50 PM  RssIcon

This is part II to the article LightSwitch Student Information System.

image

In this installment, we will add Attendance to the system. We will use this opportunity to cover implementing Business Rules and Screen Permissions.

image

Eventually the Attendance form will look like the image above.

image

However, in this article ,the Attendance screen will look like the image above. We will cover creating a custom control for the Attendance in the next article.

Adding Attendance Table

image

Starting from the code posted in the last article, we add a new table…

image

We create an Attendance table with two fields.

image

In the Properties for the AttendanceCode field, click on Choice List.

image

Add the choices show above.

image

Click on the Relationship button.

image

Set the criteria you see above and click OK.

image

Click on the AttendanceDate and Enrollment fields, and click the Include in Unique Index box for each.

This will prevent duplicate attendance for accidently being entered.

image

Click the button to create a Screen for the table.

image

Create an Editable Grid Screen.

Hit F5 to run the project.

image

We are able to enter Attendance.

image

Notice that we are not able to enter duplicate attendance.

Stop the application, and return to the project in Visual Studio.

Business Rules

While it is an option to only enter valid records. We desire to implement Business Rules to assist users in not entering invalid data. For example, we already, do not allow duplicate Attendance (this is implemented by using the Include in Unique Index option)

LightSwitch automatically enforces referential integrity for most data (for example, you can only add Attendance to an Enrollment that already exists), but it cannot know what is valid when you have a table of transactions (records that represent changes to a single entity such as Attendance records for an Enrollment).

We will implement Business Rules for:

  • Enrollments
    • Do not allow orphan Attendance when changing the Start and Stop dates
  • Sections
    • Do not allow orphan Enrollments by changing the Start and Stop dates

(There are many more Business Rules that we could implement, but we want to keep this article simple, and show you sample code that you can re-use)

image

Open the Enrollment table.

image

Click the Write Code dropdown, and select the Validate method.

Enter the following code:

        #region Enrollments_Validate
        partial void Enrollments_Validate(Enrollment entity, EntitySetValidationResultsBuilder results)
        {
            // Cannot start Enrollment before Section start date
            if (entity.StartDate < entity.Section.StartDate)
            {
                results.AddEntityError(String.Format("Cannot start this Enrollent before {0}",
                    entity.Section.StartDate.ToShortDateString()));
            }
            // Cannot stop Enrollment after Section stop date
            if ((entity.StopDate != null) && (entity.Section.StopDate != null))
            {
                if (entity.StopDate > entity.Section.StopDate)
                {
                    results.AddEntityError(String.Format("Cannot stop this Enrollent after {0}",
                        entity.Section.StopDate.Value.ToShortDateString()));
                }
            }
            // Cannot stop Enrollment if it orphans attendance
            if ((entity.StopDate != null) && (entity.Attendances.Count() > 0))
            {
                var result = from Attendancs in entity.Attendances
                             where Attendancs.AttendanceDate > entity.StopDate
                             orderby Attendancs.AttendanceDate descending
                             select Attendancs;
                if (result != null)
                {
                    if (result.Count() != 0)
                    {
                        results.AddEntityError(String.Format("Cannot stop this Enrollent before {0}",
                            result.FirstOrDefault().AttendanceDate.ToShortDateString()));
                    }
                }
            }
        }
        #endregion

 

Enter the following code for the Validate method for Sections:

 

        #region Sections_Validate
        partial void Sections_Validate(Section entity, EntitySetValidationResultsBuilder results)
        {
            // Cannot start Section after any Enrollments
            foreach (var item in entity.Enrollments)
            {
                if (entity.StartDate > item.StartDate)
                {
                    results.AddEntityError(String.Format("Cannot start this Section because an Enrollment starts on {0}",
                    item.StartDate.ToShortDateString()));
                }
            }
            // Cannot stop Section if it will orphan Enrollments
            if ((entity.StopDate != null))
            {
                foreach (var item in entity.Enrollments)
                {
                    if ((item.StopDate.HasValue) && (entity.StopDate < item.StopDate))
                    {
                        results.AddEntityError(String.Format("Cannot stop this Section because an Enrollment ends on {0}",
                        item.StopDate.Value.ToShortDateString()));
                    }
                }
            }
        }
        #endregion

Save the code and close the code window (very important, otherwise you will get an error when you try to run the application).

 

image

When you run the application, you will see that the Business Rules are enforced.

WARNING: If you create a rule that makes existing data invalid the application may not start up. You have to remove the rule and fix the data.

Screen Permissions

image

You will recall from the previous article that we created permissions.

We will now use those permissions to only allow Teachers access to the Home page and the Attendance page. We will do that by limiting access to all other pages to only those in the AdministrativeAccess role.

image

Open the CourseListDetail screen, and click the Write Code dropdown, and select the CoursesListDetail _CanRun method.

Enter the following code:

        partial void CoursesListDetail_CanRun(ref bool result)
        {
            if (Current.User.HasPermission(Permissions.AdministrativeAccess))
            {
                result = true;
            }
            else
            {
                result = false;
            }
        }

Do the same for all the other screens except the Home screen and the Attendance screen.

image

In Access Control, you can uncheck the box next to AdministrativeAccess and Security Administration, so you will not have those permissions when you are debugging as the Test User

image

When you run the application, you will only have access to the Home screen and the Attendance screen.

You can download the source code here:

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

Further Reading

The LightSwitch Student Information System Series

12 comment(s) so far...


Gravatar

Re: LightSwitch Student Information System (Part 2): Business Rules and Screen Permissions

Michael

Great articles. In the image showing how the attendance form will be, are you going to first generate attendance records from the enrollment records and mark everyone as present?

By Ronald Walcott on   9/19/2010 7:51 PM
Gravatar

Re: LightSwitch Student Information System (Part 2): Business Rules and Screen Permissions

@Ronald Walcott - I create in memory records without anything marked for Attendance code. The records will not save unless you mark them. I am working on the article today and will post soon.

By Michael Washington on   9/19/2010 7:52 PM
Gravatar

Re: LightSwitch Student Information System (Part 2): Business Rules and Screen Permissions

I just love lightSwitch, and I'm learning it using lightSwitch site your blogs.
I wish if you can write sample project to show how to use the computed field with LINQ in C#.
I'm building a prohect where I'm trying to show the sub total of an orderDetail next the customer name (I have two entities: customers and ordersDetails). Your help is very appreciated

By Rachida Dukes on   9/20/2010 5:11 PM
Gravatar

Re: LightSwitch Student Information System (Part 2): Business Rules and Screen Permissions

For my request, I got the answer through the LightSwitch Forum, so you don't need to respond to me. I still enjoy your blog.
Please keep the good work.
Rachida

By Rachida Dukes on   9/22/2010 4:27 AM
Gravatar

Re: LightSwitch Student Information System (Part 2): Business Rules and Screen Permissions

hi
what about switch the user when i working on the system how can i define it, i dont want to re-run the application
plz any advice..

By joemilehm on   10/21/2010 2:59 AM
Gravatar

Re: LightSwitch Student Information System (Part 2): Business Rules and Screen Permissions

@joemilehm - I do not know if that is possible.

By Michael Washington on   10/21/2010 4:08 AM
Gravatar

Re: LightSwitch Student Information System (Part 2): Business Rules and Screen Permissions

Is it possible to use SterlingDB in LightSwitch?

By TB on   3/20/2011 9:30 PM
Gravatar

Re: LightSwitch Student Information System (Part 2): Business Rules and Screen Permissions

@TB - I do not know, I havn't tried it.

By Michael Washington on   3/20/2011 9:30 PM
Gravatar

Re: LightSwitch Student Information System (Part 2): Business Rules and Screen Permissions

Have found your other articles on Silverlight and Sterling DB very useful… I am not sure of the best approach but I need a simple admin interface to populate a Sterling DB which I would like to deploy to WP7. Any ideas appreciated..

By TB on   3/21/2011 5:48 AM
Gravatar

Re: LightSwitch Student Information System (Part 2): Business Rules and Screen Permissions

@TB - It may be possible, but LightSwitch is probably not the best tool to work with Isolated Storage data.

By Michael Washington on   3/21/2011 5:48 AM
Gravatar

Re: LightSwitch Student Information System (Part 2): Business Rules and Screen Permissions

partial void CoursesListDetail_CanRun(ref bool result)
{
if (Current.User.HasPermission(Permissions.AdministrativeAccess))
{
result = true;
}
else
{
result = false;
}
}


...


i have always used:

result = User.HasPermission(Permissions.AdministrativeAccess);


or its its different level..

result = Application.User.HasPermission(Permissions.AdministrativeAccess);


1. Is this the same thing? from what i can see it works the same.
2. Is there a reason for doing it your way or is it just your style?

thanks :D loving the guides by the way

By oli on   6/28/2012 7:55 AM
Gravatar

Re: LightSwitch Student Information System (Part 2): Business Rules and Screen Permissions

@oli - They produce the same result. No reason to prefer one over the other.

By Michael Washington on   6/28/2012 7:55 AM

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