Thanks to all of you who attended my session at STL Day of Dotnet. Hopefully you found it useful. You may find a copy of my presentation here:
http://www.slideshare.net/kshaffar/stldodn-get-rid-of-crud-faster
I hope to create some follow up blog entries on this topic but in the meantime, you may download the sample project here
I have also included the script I used to build the demo.
- New Project - Silverlight Business Application AdventureWorksHR @ C:\Code Projects\DayOfDotNet2010\
- Build and Run. Explain what is there by default.
- In .Web Add New Item.
- From Data Category select ADO.Net Entity Model
- Name the file AWHRModel.edmx
- Leave Generate form Database Selected and Click Next
- Create a new Connection
- Server: SCS10L2259\SCS10L2259
- Windows Authentication
- Database: AdventureWorks
- Save entity connection settings in Web.config as AdventureWorksHREntities
- Choose All (Person) and (HumanResources) Tables
- Change Name to AdventureWorksHRModel
- Click Finish
- Review AWHRModel.edmx
- Build Solution (needed for step 6c)
- In .Web, Add New Item
- From Web Category select Domain Service Class
- Name the File AWHRDomainService.cs
- Select AdventureWorksHREntities(Entity Framework)
- Select All Entities and Enable Editing (overkill here, in practice you should pick only what you need)
- Click OK
- Review AWHRDomainService.cs
- Navigate to GetEmployees(), change it to: return this.ObjectContext.Employees.Include("Contact");
- Navigate to: AWHRDomainService.metadata.cs (USE CTRL+, and explain its wonderful)
- Add Include Attribute to Employee.Contact and add [Include]
- Build.
- Open up Home.xaml
- Delete ScrollViewer and Contents. (Just to clean things up a bit and start with a blank slate)
- Add a row to GRID by clicking in blue area on right
- Open and Pin Data Sources
- Click on Employee Drop Down
- Show them Options, including which query it is using by default, if there was more than one then it would show up.
- Be sure Datagrid is selected.
- Show them list of columns and Options for displaying them.
- Turn off everything but
- BirthDate
- CurrentFlag
- EmployeeId
- Gender
- LoginId
- MaritalStatus
- SalariedFlag
- SickLeaveHours
- Title
- VacationHours
- Drag and Drop Employee onto top Row of Home.xaml
- Size it appropriately, fixed left and top, realtive right ans bottom (click on circle)
- Build and Run. (notice all our columns are there as expected) READ DONE!
- Go back to visual studio, and end debugging
- Open Home.xaml(Now let's clean up those columns a bit.)
- Click the DataGrid and Open up Properties
- Find and Expand Columns Collection
- Reorder Columns so they are more Natural
- Change employeeIDColumn header to Employee Number
- Change loginIDColumn Layout.Width to SizeToCells
- Change titleColumn Layout.Width to SizeToCells
- Build an Run. (there that looks a bit better)
- Return To Visual Studio, Stop Debugging and Open Home.xaml
- Open up ToolBox, Find Scrollviewer, Drag it onto row 2 of Home.xaml
- Size it appropriately, Change Vertical and Horizontal alignment to Top and Left
- Open Data Sources Window and Expand Employee.
- From the Drop Down, select Details
- Change Contact to Details View
- Drag Employee inside Scrollviewer on Home.xaml and size appropriately.
- Build and RUN.
- Return to Visual Studio, End Debugging and open up Home.xaml
- Add a middle Row to the Root Grid.
- Open Toolbox and Drag a StackPanel to middle row.
- Size appropriately and change Orientation to Horizontal.
- Drag on 4 buttons into StackPanel
- Select all 4 using Ctrl+Click and change
- Width=100
- Margin=10,0,10,0
- Open up Properties For Button 1 and Change
- Name: SaveChangesButton (click next to Button at top of properties
- Content: Save Changes
- Open up Properties For Button 2 and Change
- Name: CancelChangesButton (click next to Button at top of properties
- Content: Cancel Changes
- Open up Properties For Button 3 and Change
- Name: AddEmployeeButton (click next to Button at top of properties
- Content: Add Employee
- Open up Properties For Button 4 and Change
- Name: DeleteEmployeeButton (click next to Button at top of properties
- Content: Delete Employee
- Double Click SaveChangesButton. This Generates a SaveChangesButton_Click event in Home.xaml.cs
- Add the following code to SaveChangesButton_Click
- employeeDomainDataSource.SubmitChanges();
- Return to Home.xaml and double click CancelChangesButton
- (Can anyone tell me what to type here?)
- employeeDomainDataSource.RejectChanges();
- Return to Home.xaml and double click AddEmployeeButton
Employee emp = new Employee();
emp.NationalIDNumber = "0";
emp.ModifiedDate = System.DateTime.Now;
emp.BirthDate = System.DateTime.Now.AddYears(-21);
emp.HireDate = System.DateTime.Now;
emp.rowguid=Guid.NewGuid();
emp.Contact = new Contact();
emp.Contact.rowguid = Guid.NewGuid();
emp.Contact.ModifiedDate = System.DateTime.Now;
emp.Contact.PasswordHash = "Hashed";
emp.Contact.PasswordSalt = "salty";
employeeDomainDataSource.DataView.Add(emp);
employeeDomainDataSource.DataView.MoveCurrentTo(emp);
- Return to Home.xaml and double click DeleteEmployeeButton(Note in order to Delete an employee must remove delete trigger on HumanResource.Employee)
if (System.Windows.Browser.HtmlPage.Window.Confirm("Are you sure? (Note employee will not be deleted until changes are saved.)"))
{
employeeDomainDataSource.DataView.Remove(employeeDomainDataSource.DataView.CurrentItem);
}
- (Now let's add a smidge of error handling)
- Open Home.xaml
- Navigate to employeeDomainDataSource
- Add SubmittedChanges event
- private void employeeDomainDataSource_SubmittedChanges(object sender, SubmittedChangesEventArgs e)
{
StringBuilder sb = new StringBuilder();
if (e.HasError)
sb.Append(e.Error.ToString());
foreach (Entity ent in e.EntitiesInError)
{
foreach (ValidationResult verr in ent.ValidationErrors)
{
sb.AppendLine(verr.ErrorMessage);
}
}
if (e.HasError)
{
System.Windows.MessageBox.Show(sb.ToString(), "Submit Changes Error",
System.Windows.MessageBoxButton.OK);
e.MarkErrorAsHandled();
}
}
- Build and Run
- Test Change
- Pick someone change their hours
- Submit Changes.
- Pick someone new
- Change title, Cancel Changes
- Refresh page.
- Validate Changes.
- Test Add Employee
- Add New Employee
- Fill Out all relevant fields.
- Submit Changes.
- Refresh
- Validate Changes
- Test Delete Employee
- Delete previously added employee.
- Cancel Changes.
- Delete previously added employee.
- Submit Changes
- Refresh and Validate
- Return to Visual Studio
- Navigate to AWHRDomainService.metadata.cs
- Add [RegularExpression("[M,F]")] to Gender Property of Employee
- Navigate to Home.xaml
- For Cancel and Save buttons, open up Properties, and Create a DataBinding for the IsEnabled Property ElementName=employeeDomainDataSource, Path=HasChanges
- Drag a BusyIndicator onto top Row of Home.xaml
- Bind IsBusy Property to ElementName=employeeDomainDataSource, Path=IsBusy
- Open Assets.Resources.ApplicationStrings.resx and change ApplicationName to AdventureWorks HR System
- Build, Run and Test