Great SL Competency Test Part 1 (Answer)

On August 23rd I published part 1 of The Great Silverlight Competency TestThe key tasks were to…

Running Project

…write a Silverlight program mimicking a problem report system,  that allows you to go to your db and call up a record by phone number and view and/or edit all of the following fields. Do not implement membership or roles feel free to prepopulate the db with randomized data. 

  • Full name
  • Address
  • Email(s)
  • Phone(s)
  • Date of first contact
  • Date of last contact
  • Reason for calling (1 of 5 possible)
  • Product (1 of 20 possible)
  • Computer(s) running on
  • Description of problem

The idea was to be able to do this in under an hour.  This posting will review one solution. 

The Database

Customer Tables To begin, I created three tables and a view in a new database called Customers

  • Customer
  • Product
  • Reason
  • CustomerView

The customerView joins the three tables, allowing the tables to be normalized, but simplifying the data retrieval.

The Application

I chose to create a Business Application as they are born with significant database support.  This created two projects, BusinessApplication1 and BusinessApplication1.web (okay, not the best naming). 

Within the .web project I chose Project->Add New and selected Data in the templates and then ADO.NET Entity Data Model.  I chose to base it on my database and picked the CustomerView, generating an Entity Data Model named CustomerView.edmx and with it CustomerView.Designer.cs.

I then returned to Add->New and added a WCF Data Service.  With this in hand, I turned to BusinessApplication1 (the client) and added a Service Reference to the new WCF Data Service.  Great, I can get the data.

The UI

To keep the UI very simple, I created a grid with three rows, the bottom of which houses a grid with the rows for the form, itself having two columns.  Each item in the second column is bound to a property of the page,

 <TextBlock
     Text="{Binding Address1}"
     VerticalAlignment="Center"
     Grid.Row = "1"
     Grid.Column="1" />

 <TextBlock
     Text="{Binding Address2}"
     VerticalAlignment="Center"
     Grid.Row = "2"
     Grid.Column="1" />

The Code

When the user enters a phone number and presses the button, a query is created and executed asynchronously.  [Note, this code is insufficiently bullet-proofed]

private void ExecuteQuery( object o, object routedEventArgs )
{

    _customers = new DataServiceCollection< CustomerView >();
    _customers.LoadCompleted += CustomersLoadCompleted;
    var svc =
        new CustomerViewEntities(
            new Uri( "http://localhost:52878/CustomerViewService.svc" ) );

    IOrderedQueryable< CustomerView > query = from c in svc.CustomerViews
                                         where c.phone == Phone.Text
                                         orderby c.last , c.phone
                                         select c;
    _customers.LoadAsync( query );
}

When the data is loaded I extract the first (and only) record and assign values to the properties,

void CustomersLoadCompleted( object sender, LoadCompletedEventArgs e )
{
    if ( _customers.Continuation != null )
    {
        _customers.LoadNextPartialSetAsync();
    }
    else
    {
        CustomerView cust = _customers[ 0 ];
        First = cust.first;
        Last = cust.last;
        Address1 = cust.address1;
        Address2 = cust.address2;
        City = cust.city;
        State = cust.state;
        Zip = cust.zip;
        PhoneNumber = cust.phone;
        FirstContact = cust.firstContact;
        LastContact = cust.lastContact;
        Reason = cust.reason;
        ProductName = cust.productName;
        Computer = cust.computer;
        Problem = cust.problem;
    }
}

Each property has a backing variable and calls the PropertyChangedEventHandler to notify the UI that it has been updated. For example,

 public string PhoneNumber
 {

     get { return _phoneNumber; }
     set { _phoneNumber = value; OnPropertyChanged("PhoneNumber"); }

 }

Score Yourself

Created a database 10 points
Relational db with 2 or more tables and normalized +5 points
Accessed the db through a web service of any kind or through another sustainable approach 10 points
Mapped the UI to bound data 20 points
Selected data in the db with a where clause that used the phone number using LINQ or a SPROC 15 points
Display worked as you intended 20 points
Your program works and does what it should reliably, handling missing records and entry errors 20 points

 

I made up these numbers and criteria, so please remember that it is the journey, not the destination that counts. 

Note, I’ve not made the source available as I’ll be tinkering with it and then releasing it with a video.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in Community, Data, Essentials, Patterns & Skills and tagged , . Bookmark the permalink.

2 Responses to Great SL Competency Test Part 1 (Answer)

Comments are closed.