47
Tony Vitabile Portfolio August 5, 2010 Library Project, Phase 1 Introduction: This project required us to build a Windows Forms application to automate the principal day-to-day operations of a lending library. The application had to support the four (4) principal library functions: adding adult & juvenile members and checking items in & out. For this project, we were given DLLs containing the Entity and Data Access layers. These interfaced with the back-end database, running on SQL Server. Our task was to build a front-end for the application using Windows Forms and a business layer to isolate the front-end from the Data Access layer. Audience: .NET Project managers, .NET Developers Project Goals: The project goals were: Create a Windows Forms application that allows a librarian to create new adult & juvenile members and check books in & out. The application must validate user input to make sure data is entered for valid fields and all input meets business rules. Items already checked-out cannot be checked-out by someone else until they have been checked back in. Regular expressions were to be used to verify that all input met formatting requirements. 1

Tony Vitabile .Net Portfolio

Embed Size (px)

DESCRIPTION

Tony Vitabile\'s .NET Portfolio, containing screen shots and code from his .NET Masters Program projects

Citation preview

Tony Vitabile Portfolio August 5, 2010

Library Project, Phase 1

Introduction:

This project required us to build a Windows Forms application to automate the principal day-to-day operations of a lending library. The application had to support the four (4) principal library functions: adding adult & juvenile members and checking items in & out.

For this project, we were given DLLs containing the Entity and Data Access layers. These interfaced with the back-end database, running on SQL Server. Our task was to build a front-end for the application using Windows Forms and a business layer to isolate the front-end from the Data Access layer.

Audience:

.NET Project managers, .NET Developers

Project Goals:

The project goals were:

Create a Windows Forms application that allows a librarian to create new adult & juvenile members and check books in & out. The application must validate user input to make sure data is entered for valid fields and all input meets business rules. Items already checked-out cannot be checked-out by someone else until they have been checked back in. Regular expressions were to be used to verify that all input met formatting requirements.

1

Tony Vitabile Portfolio August 5, 2010

Sample Screen Shots & Code

The Main Page

The application used a tabbed interface to keep all fields and buttons for each function grouped together. The main page contains fields for checking out an item. This is a three step process:

Find the member who wishes to check out the book. To do this, the librarian enters the member’s member ID number in the field labeled Member & clicks the button labeled “Search” next to the Member ID field.

Find the Item the member wishes to check-out. The librarian enters the ISBN & Copy Number of the Item the member wishes to borrow, then clicks on the “Search” button next to the Copy No. field.

Check-out the item. This is done by clicking on the “Check-Out” button.

In the example shown above, the member’s membership has expired. As a result, they are not allowed to check-out any items until their membership is renewed.

Here is the front-end code used to search the database for a particular member.

2

Tony Vitabile Portfolio August 5, 2010

/// <summary>/// Handles the Search button on the Check-Out tab in the 1. Find a Member /// Groupbox./// </summary>/// <param name="sender">The object that sent the message. Ignored.</param>/// <param name="e">The arguments for the event. Ignored</param>private void searchMemberButton_Click( object sender, EventArgs e ) { // Set our private field so the Validating methods know how to validate validateControls = ValidateCtrls.Member;

// Validate the fields on the form if ( !this.ValidateChildren( ValidationConstraints.Enabled | ValidationConstraints.Visible ) ) { // If we get here, at least one field does not validate. // Tell the user there's a problem DisplayStatus( statusLabel.Text + " Please fix the errors before trying to search again." );

// Exit now return; }

// The form validates OK. Get the member ID from the memberIdTextBox short memberID = short.Parse( memberIdTextBox.Text );

// We need a try-catch block try { // Try to find the member with the given member ID from the database CurMember = BusinessLayer.GetMember( memberID );

} catch ( ArgumentOutOfRangeException ) { // . . .

} catch ( LibraryException ex ) { // If we get here, the GetMember operation failed. // Construct an error message string errMsg = string.Format( "Unable to find a Member with ID {0} because {1}.", memberID, BusinessLayer.GetReason( ex ) );

// Display the error message in the statusLabel control DisplayStatus( errMsg ); // Put the same error message in the errorProvider errorProvider.SetError( memberIdTextBox, errMsg );

// Make sure CurMember is null CurMember = null;

} catch ( Exception ex ) { // . . . }}

3

Tony Vitabile Portfolio August 5, 2010

Checking Out an Item

The figure above shows the main screen after an Item has been successfully checked out for the member. The Check-In button shown in this figure is enabled only when the Item shown is already checked-out. This button is on the screen because sometimes, in a real library, people returning books that are late put them on the shelves directly, in order to avoid paying a late fee. As a result, the next member to check-out the book finds it is already checked-in. This button is a convenience feature that allows the librarian to check the book back in before checking it out for the member without having to change screens.

Here is the front-end code for checking-out an Item.

4

Tony Vitabile Portfolio August 5, 2010

/// <summary>/// Called when the user clicks the "Check-Out" button. Displays/// the Check-Out Form using a pop-up window./// </summary>/// <param name="sender">The object that sent the message. Ignored.</param>/// <param name="e">The arguments for the event. Ignored</param>private void checkOutButton_Click( object sender, EventArgs e ) { // Build a string with the information on the member & book // we are going to check in string bookInfo = string.Format( "Title: {0}, ISBN {1}, Copy {2}", CheckOutItem.Title, CheckOutItem.ISBN, CheckOutItem.CopyNumber );

// We need to use a try-catch block for our check-in attempt try { // Try to check-in the current Item in the business layer BusinessLayer.CheckOut( CurMember, CheckOutItem );

} catch ( ArgumentOutOfRangeException ex ) {. . .

} catch ( Exception ex ) { // There was an error in the business layer. Construct a message string msg = string.Format(

"An unexpected error occurred while checking out {0}: {1}.",bookInfo, ex.Message );

// Display the error message DisplayStatus( msg );

// Exit now return; }

// Display a success message in the statusLabel DisplayStatus( string.Format( "{0} was successfully checked out by {1}", bookInfo, BusinessLayer.GetMemberName( CurMember ) ) );

// Reload the CurMember property CurMember = CurMember;

// Reload the CheckOutItem property CheckOutItem = GetItem( CheckOutItem.ISBN, CheckOutItem.CopyNumber );}

5

Tony Vitabile Portfolio August 5, 2010

Library Project, Phase 2

Introduction:

This project required us to build new entity and data access layers for the library application we built in phase 1. We were also required to build our own stored procedures for the data access layer to call using ADO.NET objects in order to implement all of the required functionality. In addition to supporting the four operations (add adult & juvenile members, check in & out items), we were required to make sure an item being checked-out was marked as loanable in the database.

There were also a number of extra-credit tasks for this project. These included: Displaying a report of overdue items generated using Linq to SQL functionality. Convert a juvenile member to an adult member if the date is after their 18th birthday. Allow a librarian to add a new item to the database.

Audience:

.NET Project managers, .NET Developers

Project Goals:

The project goals were: Develop the stored procedures needed to implement the add adult member, add

juvenile member, check-out item and check-in item functionality. Use transactions in the stored procedures where required. Write test scripts for each of the stored procedures Use ADO.NET objects to call the stored procedures & read the data returned into the

entity objects. The data access layer had to be stateless, meaning it could not remember any

information between calls.

6

Tony Vitabile Portfolio August 5, 2010

Check-Out Item Stored Procedure

The Check-Out Item stored procedure, shown below, follows a standard sequence of events: Check all procedures for validity. If any errors are found, an error is raised, using a

coded value to indicate what the problem was. If all arguments are valid, the stored procedure then performs whatever steps are

necessary to implement the required functionality.

In addition to checking the validity of its arguments, the Check-Out stored procedure must also verify that the member’s membership hasn’t expired and that the Item that the member wishes to check-out is not already checked out. If so, an error is raised. But if the member is in good standing & the item is not checked out, it must update two tables to complete the checkout. In this case, a transaction is started & if either operation fails, the entire operation is rolled-back.

CREATE PROCEDURE [dbo].[CheckOutItem] -- Add the parameters for the stored procedure here @ISBN int, @CopyNo smallint, @MemberID smallint, @OtherMemberID smallint OUTPUTASBEGIN

. . . -- Try to get the tile number for the Item out of the Copy table DECLARE @TitleNo int; SELECT @TitleNo = title_no FROM copy WHERE isbn = @ISBN AND copy_no = @CopyNo; -- Declare an error message string DECLARE @error varchar; -- Everything is OK. Check out the Item. Begin a transaction BEGIN TRANSACTION; -- Trap any errors BEGIN TRY -- Create a row in the Loan table for this loan INSERT INTO loan ( isbn, copy_no, member_no, title_no, out_date, due_date ) VALUES ( @ISBN, @CopyNo, @MemberID, @TitleNo, GETDATE(),

DATEADD( dd, 14, GETDATE() ) ) END TRY BEGIN CATCH -- The insert into the Loan table failed. Rollback ROLLBACK TRANSACTION

, , , -- Raise an error (send a SqlException to C#) RAISERROR( @error, 11, 12 ); END CATCH

7

Tony Vitabile Portfolio August 5, 2010

-- The Item is now in the Loan table. Set the on_loan field to 'Y' BEGIN TRY -- Update the Copy table UPDATE Copy SET on_loan = 'Y' WHERE isbn = @ISBN AND copy_no = @CopyNo; END TRY BEGIN CATCH -- The Copy table Update failed. Rollback the transaction ROLLBACK TRANSACTION

. . . -- Raise an error (send a SqlException to C#) RAISERROR( @error, 11, 13 ); END CATCH COMMIT TRANSACTION; -- Return 1 to show it worked RETURN 1;END

8

Tony Vitabile Portfolio August 5, 2010

Check-Out Item Data Access Layer Method

The Check-Out Item method in the Data Access Layer uses ADO.NET object to obtain a connection to the database server and then execute it. It throws a custom LibraryException if any expected errors occur in the stored procedures. It re-throws the original exception if an unexpected error occurs.

public void CheckOutItem( short memberNumber, int ISBN, short copyNumber ) { // Trap any errors that occur in this routine. try { // Get a connection to the database using ( SqlConnection connection = new SqlConnection(

ConnectionString ) ) { // Create a Command that uses the AddAdultMember stored procedure using ( SqlCommand command = new SqlCommand( "CheckOutItem",

connection ) ) { // Open the connection now connection.Open();

// Tell the Command it's a stored procedure command.CommandType = CommandType.StoredProcedure;

// Create our parameters for the stored procedure command.Parameters.AddWithValue( "@ISBN", ISBN ); command.Parameters.AddWithValue( "@CopyNo", copyNumber ); command.Parameters.AddWithValue( "@MemberID", memberNumber );

// Need an output parameter for the OtherMemberID SqlParameter omi = new SqlParameter( "@OtherMemberID",

DbType.Int16 ); omi.Direction = ParameterDirection.Output;

// Need a parameter for the return value SqlParameter rv = new SqlParameter(); rv.ParameterName = "@Return"; rv.Direction = ParameterDirection.ReturnValue;

// Add the two parameters we created outside this try block command.Parameters.Add( omi ); command.Parameters.Add( rv );

// Execute the Stored Procedure. command.ExecuteNonQuery();

// Is the return value 0 or is it 1? if ( ( (int) rv.Value ) == 0 ) // The Item was already on loan. throw new LibraryException( (short) omi.Value,

"The Item is already on loan to another Member." ); } }

9

Tony Vitabile Portfolio August 5, 2010

} catch ( SqlException ex ) { // Determine which ErrorCode to return switch ( ex.State ) { case 1: // ISBN is null throw new ArgumentNullException( "ISBN", "The ISBN is NULL." );

case 2: // ISBN is invalid throw new ArgumentOutOfRangeException( "ISBN", ISBN, "The ISBN is out of the allowed range." );

case 3: // The ISBN does not exist case 7: // The Copy Number does not exist throw new LibraryException( ErrorCode.ItemNotFound,

ex.Message );

. . . } } }

10

Tony Vitabile Portfolio August 5, 2010

Check-In Item Stored Procedure

The Check-In Item stored procedure, shown below, is like the check-out stored procedure in that it performs the usual argument checks. In addition to checking the validity of its arguments, the Check-In stored procedure must also verify that the Item is already checked out. If so, the Item is checked in by copying the row in the Loan table into the LoanHistory table and then deleting the row from the Loan table.

CREATE PROCEDURE [dbo].[CheckInItem] -- Add the parameters for the stored procedure here @ISBN int, @CopyNo smallintASBEGIN -- Declare an error message string DECLARE @error varchar;

. . . -- The Copy is on loan. Begin a transaction BEGIN TRANSACTION; -- Trap any errors BEGIN TRY -- Create a row in the LoanHist table for this loan INSERT INTO loanhist ( isbn, copy_no, out_date, title_no, member_no, due_date,

in_date ) SELECT isbn, copy_no, out_date, title_no, member_no, due_date,

GETDATE() FROM loan WHERE isbn = @ISBN AND copy_no = @CopyNo; END TRY BEGIN CATCH -- The insert into the Loan table failed. ROLLBACK TRANSACTION

. . . -- Raise an error (send a SqlException to C#) RAISERROR( @error, 11, 8 ); END CATCH -- We have to set the on_loan field in the Copy table to 'N' BEGIN TRY -- Update the Copy table UPDATE Copy SET on_loan = 'N' WHERE isbn = @ISBN AND copy_no = @CopyNo; END TRY BEGIN CATCH

. . . END CATCH -- Delete the row from the Loan table BEGIN TRY DELETE FROM loan WHERE isbn = @ISBN AND copy_no = @CopyNo; END TRY

11

Tony Vitabile Portfolio August 5, 2010

BEGIN CATCH. . .

END CATCH

-- If we get here, everything worked. Commit the transaction COMMIT TRANSACTION; -- Return 1 to show it worked RETURN 1;END

12

Tony Vitabile Portfolio August 5, 2010

Get Items Data Access Layer Method

The requirements called for the display of all Items a member has on loan when they are looked-up and displayed. To find all of the Items a member has on loan, a stored procedure was written. Then, a strongly typed DataSet called ItemsDataSet was created that connected to the stored procedure. The code shown below is the Data Access Layer method that queries the database using the stored procedure and returns an ItemsDataSet to the caller with all of the Items that member has on loan.

public ItemsDataSet GetItems( short memberNumber ) { // Declare an ItemsDataSet object to return to the caller ItemsDataSet dataSet = new ItemsDataSet();

// Create a try-catch block try { // Get a TableAdapter using ( ItemsTableAdapter adapter = new ItemsTableAdapter() ) { // Load the table with the data adapter.Fill( dataSet.Items, memberNumber ); } } catch ( SqlException ex ) { // The cause of the exception is in the exception's State property switch ( ex.State ) { case 1: // The MemberID is null throw new ArgumentNullException( "memberNumber",

"The Member Number is NULL." );

case 2: // The MemberID is invalid throw new ArgumentOutOfRangeException( "memberNumber",

memberNumber, "The Member Number is out of the allowed range." );

default: // An unexpected error occurred. Throw a LibraryException throw new LibraryException( ErrorCode.GenericException,

"A database error occurred", ex ); } }

return dataSet;}

13

Tony Vitabile Portfolio August 5, 2010

Overdue Items Report

One of the extra-credit requirements we had was to implement a report that retrieved a list of all overdue Items using Linq to SQL. My implementation consisted of the following parts:

A .dbml file that queried a view of all Items on loan from the database A method in the Data Access Layer that returned the ItemOnLoanContext object created

by the .dbml file. A method in the Business Layer that performed a Linq query against the

ItemOnLoadContext returned by the DataAccessLayer & returned the results of that query.

The front end consisted of a tab page for the report that contained a DataGridView control. The Business Layer method is called when the tab is displayed and its return value is data bound to the DataGridView control.

Below is the Data Access layer method:

public ItemsOnLoanDataContext GetItemsOnLoan() { // Declare a DataContext that we will return ItemsOnLoanDataContext context = null;

// Start a try-catch block try { // Create the DataContext context = new ItemsOnLoanDataContext( ConnectionString );

} catch ( SqlException ex ) { // An unxpected SQL error occurred. Throw a LibraryException throw new LibraryException( . . . ); }

// Return the DataContext to the caller return context;}

And here is the Business Layer method:

public static IEnumerable<ItemOnLoan> GetOverDueItems() { // Create Get a LibraryDataAccess instance LibraryDataAccess lda = new LibraryDataAccess();

// Get a DataContext for our BooksOnLoan database view ItemsOnLoanDataContext context = lda.GetItemsOnLoan();

// Get all of the over due items var overDueItems = from item in context.ItemsOnLoan where item.due_date < DateTime.Now orderby item.due_date descending select item;

return overDueItems;}

14

Tony Vitabile Portfolio August 5, 2010

Library Project, Phase 3

Introduction:

This project required us to replace the Windows front end which we had built & refined in Phases 1 & 2 with an ASP.NET front end. In addition, new functionality had to be implemented.

Audience:

.NET Project managers, .NET Developers

Project Goals:

The project goals were: Build a new front-end using ASP.NET The previous Entities, Business & Data Access Layers were used. Flag adults whose memberships have expired when looking them up and give the

librarian the option to renew their memberships. Detect when a juvenile member’s 18th birthday has passed and automatically promote

them into adult members. The librarian must be notified when this promotion is done. Overdue items displayed on any page must be highlighted The librarian must be able to enter new Items into the database The Member look-up page had to use an AJAX UpdatePanel and an Update Progress

control. The user has to be able to select books that the member has checked-out and check them in from the Member Look-up page. The Update Progress control must be displayed as the check-ins are done & the page must refresh showing all Items the member still has on loan.

Hyperlinks must be used to navigate between pages. The application has to use form-based authentication and authorization. Only users

with a Librarian role are allowed to use any of the library functionality.

15

Tony Vitabile Portfolio August 5, 2010

Member Look-up Page

The Member Look-up page allows the librarian to look-up a member who wants to borrow or return books from the database. The librarian enters their Member ID number into the field for it and clicks the “Search” button. The database is searched for the Member Number. If the member is not found, or if the member number is not valid, an error message is displayed in the message area at the bottom of the screen. If the member is found, their information, including the list of all items they have on loan, is retrieved & displayed.

In the above figure, you will notice the member’s membership has expired & is displayed in red. Further, the member has four (4) Items on loan, which is the maximum. All four books are also overdue, as their due dates are displayed in red.

The message area is displaying two error messages, one indicating that the member’s membership has expired, and the other that the member cannot check out any more Items. This second message is displayed for 2 reasons:

16

Tony Vitabile Portfolio August 5, 2010

1. The member cannot have more than four (4) items out on loan at one time2. The member’s membership is expired.

If the librarian clicks the “Renew Membership” button, the member’s membership expiration date is changed to one year from today & they are permitted to borrow books again, so long as they have less than four (4) items on loan.

The librarian can check off any or all of the books displayed in the Items Borrowed grid and click on the Check-In button. At that time, the books that are checked-off will be checked in while the Update Progress control displays a graphic indicating it is processing. Once all of the selected Items are checked-in, the grid is reloaded with the list of Items still on loan & the Update Progress control is no longer displayed. As this is all done in an AJAX Update Panel control, the operation appears to happen without a round trip or postback to the server.

Once the member has been looked-up, the librarian can navigate to the Check-Out page and check out any items they wish to borrow, provided the member is allowed to do so. The Check-Out page is shown below.

17

Tony Vitabile Portfolio August 5, 2010

Member Look-up Page Mark-Up

Below is an excerpt of the Member Look-up page’s mark-up. This code is automatically translated into HTML & downloaded to the user’s browser when they navigate to this page. This excerpt shows the Update Panel, Grid View, and the UpdateProgress controls.

<asp:UpdatePanel ID="ContentUpdatePanel" runat="server"> <ContentTemplate> <asp:GridView ID="LoansDataGrid" runat="server" AutoGenerateColumns="False" . . . EmptyDataText="This Member does not have any Items on loan." onrowdatabound="LoansDataGrid_RowDataBound"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="ItemSelectedCheckBox"

runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="out_date" HeaderText="Out" DataFormatString="{0:d}" ReadOnly="True" /> <asp:BoundField DataField="due_date" HeaderText="Due" DataFormatString="{0:d}" ReadOnly="True" /> <asp:BoundField DataField="isbn" HeaderText="ISBN" ReadOnly="True" /> <asp:BoundField DataField="copy_no" HeaderText="Copy" SortExpression="copy_no" ReadOnly="True" /> <asp:BoundField DataField="title" HeaderText="Title" ReadOnly="True" /> <asp:BoundField DataField="author" HeaderText="Author" ReadOnly="True" /> </Columns> . . . </asp:GridView> <div style="text-align: center"> <br /> <asp:Button ID="CheckInSelectedButton" runat="server"

Text="Check-In" Enabled="False" onclick="CheckInSelectedButton_Click" /> <br /> <asp:UpdateProgress ID="UpdateProgress" runat="server"

DisplayAfter="250"> <ProgressTemplate> <asp:Image ID="ProgressImage" runat="server"

ImageUrl="~/images/ajax-loader.gif" /> </ProgressTemplate> </asp:UpdateProgress> </div> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="CheckInSelectedButton"

EventName="Click" /> </Triggers></asp:UpdatePanel>

18

Tony Vitabile Portfolio August 5, 2010

Check-In Selected Items Method

Once the librarian checks off Items in the Items Borrowed Grid View control and clicks the “Check-In” button, the code below is executed.

protected void CheckInSelectedButton_Click( object sender, EventArgs e ) { // Sleep for 1 seconds, to make the UpdateProgress control show Thread.Sleep( 1000 );

. . . // Loop through all of the rows in the LoansDataGrid for ( int r = 0; r < LoansDataGrid.Rows.Count; r++ ) { // Get a reference to this Row GridViewRow row = LoansDataGrid.Rows[ r ];

// Get a reference to the CheckBox control on this row CheckBox cb = (CheckBox) row.FindControl( "ItemSelectedCheckBox" );

// Is the checkbox checked? if ( cb.Checked ) { // Yes it is. Get this Item's ISBN & Copy Number int isbn = int.Parse( row.Cells[ colISBN ].Text ); short copy = short.Parse( row.Cells[ colCopy ].Text );

// Try to Check-In this Item if ( helper.CheckIn( isbn, copy ) ) { // We checked it in. Increment our counter checkedIn++;

. . . // Do we have a CheckInItem? if ( CheckInItem != null ) { // Yes, we do. Is this Item the current Check-In item? if ( CheckInItem.ISBN == isbn && CheckInItem.CopyNumber == copy ) { // It is. Reload it! CheckInItem = helper.GetItem( isbn, copy ); } }

. . . } } }

// Redisplay the Current Member CurMember = CurMember;

// Reload the ItemsOnLoan and update the GridView ItemsOnLoan = helper.GetItemsOnLoan( CurMember );

// Display a message indicating which Items have been checked-in helper.DisplayStatus( string.Format( "Checked in {0} Items: {1}",

checkedIn, items ) );}

19

Tony Vitabile Portfolio August 5, 2010

Library Project, Phase 4

Introduction:

This project required us to transform our ASP.NET applications into a WCF service back-end and a WCF client front-end application.

Audience:

.NET Project managers, .NET Developers

Project Goals:

The project goals were: Create a WCF service that calls into the Business Layer. Update the front-end so it calls the WCF service. The service must support the following:

o A WCF Service Library project calls the Business Layer methods.o The service runs as a WCF Service Website.o The service should use the WsHttpBinding.

Transport with Message Credential security.o Authentication using ASP.NET membership.o Authorization using ASP.NET roles, using the Principle Permission property to

secure the service operations. Use DataContracts for the entities. Support all previous developed functionality. The project had to use strongly-typed FaultException objects to communicate errors

back to the client

In order to convert the Phase 3 project into a WCF service & client, we followed the following steps:

1. We built a WCF Service Library that contained methods that called the Business Layer methods. As WCF does not support method overloading or passing by reference, we had to give all methods unique names & return any objects that were modified by a call, such as the Item passed to the Check-In & Check-Out methods.

2. Code all error handling in the WCF Service Library so it passed strongly typed FaultException objects to the client when an unrecoverable error occurred.

3. Host the WCF Service Library in a WCF Web Host website.4. Convert the front-end so it would work with the new WCF Web Service.5. Confirm all functionality works as designed.6. Host the web service in IIS.7. Configure security for the service as specified.8. Modify the client to pass the proper credentials to the service.9. Test everything.

20

Tony Vitabile Portfolio August 5, 2010

Web Service Library Interface

Below is an excerpt from the Interface that was developed for the project. The interface defines the service contract, which is the pattern of requests and responses used by the service and the client to communicate with each other.

[ServiceContract( Namespace = "http://www.setfocus.com/AV/Library/2010/07/" )]public interface IAVLibraryService {

[FaultContract( typeof( ErrorDetail) )] [OperationContract] Item AddItem( int isbn, string title, string author, string synopsis, string translation, string cover, bool loanable );

[FaultContract( typeof( ErrorDetail ) )] [OperationContract] Member AddAdultMember( string firstName, string middle, string lastName, string streetAddress, string city, string state, string zipcode, string phone );

[FaultContract( typeof( ErrorDetail ) )] [OperationContract] Member AddJuvenileMember( string firstName, string middle,

string lastName, DateTime birthday, short adultMemberID, DateTime expirationDate, string streetAddress, string city, string state, string zipcode, string phone );

[OperationContract] bool CanPromoteToAdult( Member member );

[FaultContract( typeof( ErrorDetail ) )] [OperationContract] void CheckInItem( Item item );

[FaultContract( typeof( ErrorDetail ) )] [OperationContract] void CheckInItemById( int isbn, short copy );

[FaultContract( typeof( ErrorDetail ) )] [OperationContract] void CheckOutItem( Member member, Item item );

[FaultContract( typeof( ErrorDetail ) )] [OperationContract] void CheckOutItemById( short memberId, int isbn, short copy );

[FaultContract( typeof( ErrorDetail ) )] [OperationContract] List<string> GetCovers();

21

Tony Vitabile Portfolio August 5, 2010

Web Service Implementation

Below is an excerpt from the class that implements the interface shown above. The methods of this class take care of calling the proper Business Layer method and processing any errors thrown by those methods for the service. All unhandled errors are thrown to the client as strongly typed faults, as shown.

[PrincipalPermission( SecurityAction.Demand, Role = "LibraryPartner" )]public Item AddItem( int isbn, string title, string author, string synopsis, string translation, string cover, bool loanable ) {

// We need a variable to return the newly created Item Item newItem = null;

// Need to do this in a try-catch block try { // Call the business layer to do the work newItem = BusinessLayer.AddItem( isbn, title, author,

synopsis, translation, cover, loanable );

// Catch any ArgumentNullException } catch ( ArgumentNullException ex ) {

. . . // Catch any ArgumentOutOfRangeException } catch ( ArgumentOutOfRangeException ex ) {

... // Catch any LibraryException } catch ( LibraryException ex ) { // We Create an ErrorDetail object ErrorDetail detail = new ErrorDetail { ErrorCode = ex.LibraryErrorCode, Details = "An error occurred in the database while processing the operation", OtherMemberId = ex.OtherMemberID }; throw new FaultException<ErrorDetail>( detail,

BusinessLayer.GetReason( ex ) );

// Catch any other error that might occur } catch ( Exception ex ) {

. . . }

// Return the newItem to the caller return newItem;}

22

Tony Vitabile Portfolio August 5, 2010

Web Service Mark-Up

Below is the mark-up in the Service.svc file that is used to connect the service contract to the WCF Web host. This one line of mark-up code is all it takes to connect the service logic, implemented in this case in the class named “AVLibraryServiceLibrary.AVLibraryService” to the web host in WCF/ASP.NET. The rest of the details are all in the Web.config file.

<%@ ServiceHost Service="AVLibraryServiceLibrary.AVLibraryService" %>

Web Service Configuration File (Web.config)

Below is the Service Model section of the Web Host’s Web.config file. Included are: The definition of the binding used by the service (the <wsHttpBinding> element). This

includes the type of security used by the transport, which in this case is TransportWithMessageCredential. Also specified is the type of security used by the messages, which is UserName.

The definition of the service behavior (in the <service> element). This specifies the fully-qualified name of the class that implements the service, and is the linkage between the Service.svc file and the configuration information.

The definition of the endpoint for the service, which includes the service’s endpoint, binding and contract in the <endpoint> element.

Additional information about the security configuration is included in the <behaviors> element of the web configuration file. Things defined here include:

o The type of service credentials to use, in this case user name credentials, and the name of the provider class to use to verify the user, in this case AspNetSqlmembershipProvider.

o The type of service authorization to use, in the <serviceAuthorizaton> tag. The tag has attributes that specify Principal Permission mode (UseAspNetRoles) and the role provider class name (AspNetSqlRoleProvider).

o The mex endpoint is used by clients to discover the various contracts used by the service, including the service contract & data contracts.

23

Tony Vitabile Portfolio August 5, 2010

<system.serviceModel> <bindings> <wsHttpBinding> <binding name="WsHttpBindingConfig"> <security mode="TransportWithMessageCredential"> <transport clientCredentialType="None" /> <message clientCredentialType="UserName" /> </security> </binding> </wsHttpBinding> </bindings> <services> <service behaviorConfiguration="ServiceBehavior" name="AVLibraryServiceLibrary.AVLibraryService"> <endpoint address="" binding="wsHttpBinding" bindingConfiguration="WsHttpBindingConfig" contract="AVLibraryServiceLibrary.IAVLibraryService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="mex" binding="mexHttpsBinding" bindingConfiguration="" contract="IMetadataExchange" /> </service> </services> <behaviors> <serviceBehaviors> <behavior name="ServiceBehavior"> <serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> <serviceCredentials> <userNameAuthentication

userNamePasswordValidationMode="MembershipProvider" membershipProviderName="AspNetSqlMembershipProvider" /> </serviceCredentials> <serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="AspNetSqlRoleProvider" /> </behavior> </serviceBehaviors> </behaviors></system.serviceModel>

24

Tony Vitabile Portfolio August 5, 2010

XML Contracts: Final Project

Introduction:

The final project in the .NET Masters Program was to build a system for maintaining information used to track contracts SetFocus generates for their training services. The system SetFocus uses is XML based. There is a configuration XML file that contains information about the types of training sessions and variables that specify and alter the base contract. Each session of a course that is offered appears as a separate tag in the file.

This file is currently maintained manually by SetFocus employees and our task was to put together system to automate the maintenance of this file.

Audience:

.NET Project managers, .NET Developers

Project Goals:

The project goals were: Read information about a session from the configuration XML file Allow the user to create a new session of an existing course of study Allow the user to edit an existing session Allow the user to add new session types Allow the user to add or remove variables Allow the user to edit the XML containing contract terms

SetFocus uses a system that generates contract PDF files from the information in the various XML files, which was web based. While we weren’t required to produce an ASP.NET application, the finished application would have to share the XML files with this contract generator application.

The members of the class were divided into two teams and each team worked together to build a solution. At the end of the week allotted to the work, the teams had to present their solutions to the rest of the class and other guests.

The team decided to implement the application using an ASP.NET interface. Screen shots are shown below.

25

Tony Vitabile Portfolio August 5, 2010

Main Page

The main page is where the user can choose what they want to do. It consists of a tabbed interface where the user can choose between Session Maintenance actions or Session Type Maintenance options. Operations provided on the Session Maintenance tab are: adding a new session, editing an existing session, or deleting an existing session. Operations provided on the Session Type maintenance tab are: adding a new session type or editing an existing session type.

In addition to these options, the user can choose to navigate to a Remove Sessions page using the left-hand navigation panel. On this page, the user can choose to remove any or all of the sessions in the configuration file in one operation. This option is provided for removing older sessions from several years ago that no longer need to be in the configuration file.

The user can also choose to go to the Update Contract page, which will be discussed later.

26

Tony Vitabile Portfolio August 5, 2010

Session Editor

The Session Editor page, shown above, is used for all major functions of the application. It is used whenever a session must be edited, whether it is a new session type or an existing session. In the figure above, the page is shown editing an existing session.

The page uses a Grid View control to display the variables and the controls for editing their values. There are three different types of variables defined by the XML:

Cost: The variable contains a numeric value formatted with thousands separators & 2 decimal places.

File: The variable contains the name of an XML file in the XML folder

27

Tony Vitabile Portfolio August 5, 2010

Text: The variable contains text that is substituted into the contract XML in place of its place holder.

An ASP.NET User Control was created that inspects each variable and determines what type of control to display. The middle column of the Grid View contains nothing but these User Controls. This control decides which control to use based on the following criteria:

For variables whose name contains the string “Date”, a Text Box with a Calendar Extender control from the MS Ajax Toolkit is displayed. This allows the user to pick a date from a pop-up calendar.

For variables known to contain at least one paragraph of text (as opposed to a couple of words), a multi-line Text Box is displayed.

For variables of type Cost, a plain Text Box is used, but the value is formatted properly when the Text Changed event is fired.

For variables of type File, a Drop Down List is displayed. The choices provided are a list of all of the XML files in the XML folder, minus the configuration file.

For all other variables, a plain, single line, default sized Text Box is displayed.

Below is the control’s Bind method, which is where the logic described above is implemented.

/// <summary>/// The user calls this method to have it display /// </summary>/// <param name="variable">The Variable object to associate with this /// control.</param>/// The list of values to bind the DropDownList's DataSource topublic void Bind( Variable variable, List<string> contracts ) { // Hide all of the child controls ddlFileValue.Visible = txtDateValue.Visible = txtTextValue.Visible = false;

// Does this variable's name contain the word "Date" in it? if ( variable.Name.ToLower().Contains( "date" ) ) { // It does. Set the VariableType to Date VariableType = VariableTypes.Date;

// Show the txtDateValue control txtDateValue.Visible = true;

// The name doesn't have "Date" in it. Is it "template"? } else if ( variable.Name.ToLower() == "template" ) { // It is. Set the VariableType to File VariableType = VariableTypes.File;

// Show the ddlFileValue control ddlFileValue.Visible = true;

} else {

28

Tony Vitabile Portfolio August 5, 2010

// It isn't template, either. Switch on the variable's Type switch ( variable.Type.ToLower() ) { case "cost": // Set the VariableType to Cost VariableType = VariableTypes.Cost;

// Show the txtCostValue control txtTextValue.Visible = true; break;

case "file": // Set the VariableType to File VariableType = VariableTypes.File;

// Show the ddlFileValue control ddlFileValue.Visible = true; break;

default: // Set the VariableType to Text VariableType = VariableTypes.Text;

// Does the variable's name contain "paragraph"? if ( variable.Name.ToLower().Contains( "paragraph" ) || variable.Name.ToLower() == "classtimes" ) { // It does. Make the txtTextValue control multiline txtTextValue.TextMode = TextBoxMode.MultiLine; txtTextValue.Rows = 5; txtTextValue.Columns = 35; }

// Show the txtTextValue control txtTextValue.Visible = true; break; } }

// Save the VariableType in the ViewState ViewState[ "VariableType" ] = VariableType;

// Is the list of contracts null or empty? if ( contracts != null && contracts.Count > 0 ) { // It is not. Set it up FileList = contracts; }

// Now set the proper child control's Text or Value property Text = variable.Value;}

29

Tony Vitabile Portfolio August 5, 2010

Updating a Contract

One of the optional requirements we had was to provide a means for the users to update a contract. The issue is that the contracts are stored as XML files and are translated into PDF files by a custom program, called the Generator. The Generator is an ASP.NET application that is accessible over the web & is used by students to print a copy of the contract for the class they are going to take. They then sign & return the contract to SetFocus before the class starts.

We decided to implement this functionality by using an XML Stylesheet Transforms, or XSLTs. The page we designed to accomplish this task is shown below.

The user chooses one of the XML files in the XML folder from the drop down & clicks the “Download” button. At that time, the XML file is transformed into a Word 2007 format document and downloaded to the user’s PC. The user may then edit the document and make any changes required.

When the user is done editing the file, they return to the above page and click the “Browse” button. This displays a dialog showing all files & folders on their local hard drive. The user locates the file they have edited & select it. Next, the user clicks the “Upload” button. At that time, the file is uploaded back to the server & transformed back into XML.

Below is the code that transforms the original XML file into Word 2007 format.

30

Tony Vitabile Portfolio August 5, 2010

public static string Transform( string XmlFile, string TransformXslt, string TemplateFile ) {

// Is the XmlFileName parameter null or empty? if ( String.IsNullOrEmpty( XmlFile ) ) { // It is. Throw an ArgumentException throw new ArgumentException(

"Must specify the fully-qualified XML file to transform", "XmlFile" );

}

// Does the file specified by XmlFile really exist? FileInfo xmlFile = new FileInfo( XmlFile); if ( !xmlFile.Exists ) { // It does not. Throw a FileNotFoundException throw new FileNotFoundException(

"Cannot find the XmlFile at the specified path", XmlFile ); }

// Is the TransformXslt parameter null or empty? if ( String.IsNullOrEmpty( TransformXslt ) ) { // It is. Throw an ArgumentException throw new ArgumentException(

"Must specify a fully-qualified XML file for the transform", "TransformXslt" );

}

// Does the file specified by TransforXslt really exist? FileInfo transformXslt = new FileInfo( TransformXslt ); if ( !transformXslt.Exists ) { // It does not. Throw a FileNotFoundException throw new FileNotFoundException(

"Cannot find the XSLT file at the specified path", TransformXslt ); }

// Is the TemplateFile parameter null or empty? if ( String.IsNullOrEmpty( TemplateFile ) ) { // It is. Throw an ArgumentException throw new ArgumentException(

"Must specify a fully-qualified Word Template for the transform", "TemplateFile" );

}

// Does the file specified by TemplateFile really exist? FileInfo templateFile = new FileInfo( TemplateFile ); if ( !templateFile.Exists ) { // It does not. Throw a FileNotFoundException throw new FileNotFoundException(

"Cannot find the Word template file at the specified path", TemplateFile );

}

31

Tony Vitabile Portfolio August 5, 2010

// Get a temporary file name to use for the output file string outputDocument = Path.GetTempFileName(); outputDocument = Path.ChangeExtension( outputDocument, "docx" ); // Create a writer for the output of the Xsl Transformation. StringWriter stringWriter = new StringWriter(); XmlWriter xmlWriter = XmlWriter.Create( stringWriter );

// Create the Xsl Transformation object. XslCompiledTransform transform = new XslCompiledTransform(); transform.Load( TransformXslt );

// Transform the xml data into Open XML 2.0 Wordprocessing format. transform.Transform( XmlFile, xmlWriter );

// Create an Xml Document of the new content. XmlDocument newWordContent = new XmlDocument(); newWordContent.LoadXml( stringWriter.ToString() );

// Copy the Word 2007 template document to the output file. File.Copy( TemplateFile, outputDocument, true);

// Use the Open XML SDK version 2.0 to open the output document in edit mode. using ( WordprocessingDocument output =

WordprocessingDocument.Open( outputDocument, true ) ) { // Using the body element within the new content XmlDocument, // create a new Open Xml Body object. Body updatedBodyContent =

new Body( newWordContent.DocumentElement.InnerXml );

// Replace the existing Document Body with the new content. output.MainDocumentPart.Document.Body = updatedBodyContent;

// Save the updated output document. output.MainDocumentPart.Document.Save(); }

// Return the output file's name return outputDocument;}

32