Developing Windows Azure and
Web Services Lab @ home 2 Ans 1
In this exercise,
you will learn to host products on the Windows Azure cloud by using Windows
Azure SQL Database and Windows Azure Web Site.
To host products on the Windows Azure cloud by using Windows Azure SQL Database and Windows Azure Web Site, you need to perform the following steps:
1. Login into the Windows Azure account to access the Windows Azure Management Portal at https://manage.windowsazure.com.
2. Click WEB SITES in the navigation pane.
NEW
3. Click the NEW button.
4. Click QUICK CREATE.
5. Type BlueYonderLocations in the URL text box.
6. Select specific region from the REGION drop-down list.
7. Click the CREATE WEB SITE link, and wait until the Web Site is created and its status changes to Running.
8. Click the name of the new Web Site.
9. Click the DASHBOARD link.
10. Click the Download the publish profile link under the quick glance section on the right side. At the bottom of the Internet Explorer window, a prompt appears.
11. Click the arrow within the Save button.
12. Select the Save as option. The Save As dialog box is displayed.
13. Browse to the location where you want to save the publish profile.
14. Click the Save button.
15. Open Computer windows by pressing the Windows logo and E keys together.
16. Browse to the location where you have saved the Exercise 04.zip file.
17. Extract the Exercise 04.zip file.
18. Double-click the Exercise 04 folder.
19. Double-click the BlueYonder.Model folder.
20. Double-click the BlueYonder.Model.sin file. The BlueYonder.Model - Microsoft Visual Studio windows is displayed.
21. Ensure that the Solution Explorer window is opened.
22. Right-click the BlueYonder.MVC project, and then select Publish from the context menu. The Publish web application page of the Publish Web wizard is displayed.
23. Click the Import button. The Import Publish Settings dialog box is displayed.
24. Browse to the location where you have saved the publish settings.
25. Select the publish settings file.
26. Click the Open button.
27. Click the Publish button. Microsoft Visual Studio 2012 builds and publishes the application. Further. Microsoft Visual Studio 2012 opens Internet Explorer and browses to the Web Site after the deployment finishes.
28. Append api/locations to the address in the address bar. and then press the Enter key. At the bottom of the Internet Explorer window, a prompt appears.
29. Click the Open button.
30. Select Notepad from the list of available programs, if you are prompted to select a program to open the file. When Notepad opens, you should see a list of Location entities, encoded with the JSON format.
31. Close Notepad.
32. Switch to the Internet Explorer window in which Windows Azure Management Portal is opened.
33. Click SQL DATABASES in the navigation pane on the left side. The sql databases page is displayed.
34. Click the SERVERS link.
35. Click the STATUS column.
36. Click the DELETE button in the bottom pane. The DELETE SERVER CONFIRMATION dialog box is displayed. Type the name of the server in the CONFIRM SERVER NAME text box.
37. Click the OK (I_______ button. The database server is deleted.
38. Click the user icon in the upper-right corner of the Windows Azure page. A context menu is displayed. Select Sign out.
39. Close the Internet Explorer window.
Note: If Internet Explorer message box is displayed, click the Close all tabs button.
40. Switch to Microsoft Visual Studio 2012.
41. Close Microsoft Visual Studio 2012.
Developing Windows Azure and Web Services Lab
@ home 2 Ans 2
Create
data model classes to represent trips and reservations, implement a
DbContext-derived class, and create a new repository class for the Reservation
entity.
To implement the required functionality, you need to perform the following tasks:
1. Explore the existing Entity framework data model project.
2. Create new data model classes.
3. Implement a data context by deriving from the Dbcontext class.
4. Create a new repository for the Reservation entity.
Task 1: Exploring the Existing Entity Framework Data Model Project
1. Browse to the location where Exercise 01 .zip file is saved.
2. Extract the files.
3. Double-click the Exercise 01 folder.
4. Double-click the BlueYonder.Companion folder.
5. Double-click the BlueYonder.Companion.sln file. The BlueYonder.Companion - Microsoft Visual Studio window displayed.
6. Ensure that the Solution Explorer window is opened.
7. Ensure that the BlueYonder.Entities node is expanded.
8. Double-click the FlightSchedule.es file. The FlightSchedule.es file is displayed.
9. Explore the properties of the FlightSchedule class. Explore how the DatabaseGenerated and ForeignKey attributes used in this class.
10. Ensure that the BlueYonder.DataAccess node is expanded in the Solution Explorer window.
11. Double-click the TravelCompanionContext.es file. The TravelCompanionContext.es file is displayed.
12. Explore the properties and methods of the TravelCompanionContext class. Explore the DbSet properties it contains.
13. Expand the Repositories node in the Solution Explorer window.
14. Double-click the FlightRepository.es file. The FlightRepository.es file is displayed.
15. Explore the methods of the FlightRepository class.
Task 2: Creating New Data Model Classes
1. Ensure that the Solution Explorer window is opened.
2. Right-click the BlueYonder.Entities node, and then select Add—Class. The Add New Item dialog box is displayed.
3. Select and replace the existing text in the Name text box with Trip.
4. Click the Add button. The Trip.cs file is displayed.
5. Type the highlighted portions of the following code snippet in the Trip.cs file: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.ComponentModel.DataAnnotations.Schema; using BlueYonder.Entities.Enums; namespace BlueYonder.Entities { public class Trip { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Tripld { get; set; } public int FlightSchedulelD { get; set; } [ForeignKey("FlightSchedulelD")] public virtual FlightSchedule Flightlnfo { get; set; } public FlightStatus Status { get; set; } public SeatClass Class { get; set; } > >
6. Select FILE— Save All to save the changes.
7. Ensure that the Solution Explorer window is opened.
8. Right-click the BlueYonder.Entities node, and then select Add—Class. The Add New Item - BlueYonder.Entities
dialog box is displayed.
9. Select and replace the existing text in the Name text box with Reservation.
10. Click the Add button. The Reservation.es file is displayed.
11. Type the highlighted portions of the following code snippet in the Reservation.es file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations.Schema;
namespace BlueYonder.Entities
{
public class Reservation
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Reservations { get; set; } public int Travelerld { get; set; ) public DateTime ReservationDate { get; set; } public string ConfirmationCode { get; set; } public int DepartFlightSchedulelD { get; set; > public virtual Trip DepartureFlight { get; set; } public int? ReturnFlightSchedulelD { get; set; } public virtual Trip ReturnFlight { get; set; }
>
>
12. Select FILE—Save All to save the changes.
Task 3: Implementing a Data Context by Deriving from the DbContext Class
1. Ensure that the Solution Explorer window is opened.
2. Ensure that the BlueYonder.DataAccess node is expanded.
3. Double-click the TravelCompanionContext.es file. The TravelCompanionContext.es file is displayed.
4. Type the highlighted portion of the following code snippet in the TravelCompanionContext.es file:
public class TravelCompanionContext : DbContext
{
public DbSet<Location> Locations { get; set; }
public DbSet<Flight> Flights { get; set; }
public DbSet<FlightSchedule> FlightSchedules { get; set; }
public DbSet<Traveler> Travelers { get; set; }
public DbSet<Reservation> Reservations { get; set; }
5. Type the highlighted portions of the following code snippet inside the OnMcdelCreating() method:
protected override void 0nModelCreating(DbModel6uilder modelBuilder)
{
modelBuilder.Entity<FlightSchedule>()
.HasRequired(fs => fs.Flight)
.WithMany(f => f.Schedules)
.Map(m => m.MapKey("FlightID")); modelBuilder.Entity<Reservation>()
.HasRequired(r => r.DepartureFlight)
.WithMany()
.HasForeignKey(r => r.DepartFlightSchedulelD)j modelBuilder.Entity<Reservation>()
.HasOptional(r => r.ReturnFlight)
.WithMany()
.HasForeignKey(r => r.ReturnFlightSchedulelD)j
}
6. Select FILE— Save All to save the changes.
Task 4: Creating a New Repository for the Reservation Entity
1. Ensure that the Solution Explorer window is opened.
2. Ensure that the BlueYonder.DataAccess node is expanded.
3. Right-click the Repositories folder, and then select Add—Class. The Add New Item - BlueYonder.Access dialog box displayed.
4. Select and replace the existing text in the Name text box with ReservationRepository.
5. Click the Add button. The ReservationRepository.es file is displayed.
6. Type the highlighted portions of the following code snippet in the ReservationRepository.es file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.Linq.Expressions;
using BlueYonder.DataAccess.Interfaces;
using BlueYonder.Entities;
namespace BlueYonder.DataAccess.Repositories
{
public class ReservationRepository : ISingleKeyEntityRepository<Reservation, int>
{
}
}
7. Type the highlighted portions of the following code snippet inside the ReservationRepository class:
public class ReservationRepository : ISingleKeyEntityRepository<Reservation, int>
{
TravelCompanionContext context} public ReservationRepository()
{
context = new TravelCompanionContext()}
}
public ReservationRepository(string connectionName)
{
context = new TravelCompanionContext(connectionName)}
}
public ReservationRepository(TravelCompanionContext dbContext)
{
context = dbContext}
}
public Reservation GetSingle(int entityKey)
{
var query = from r in context.Reservations
where r.Reservationld == entityKey select r}
return query.SingleOrOefault()}
}
public void Delete(Reservation entity)
{
entity =
context.Reservations.Find(entity.Reservationld)} if (entity.DepartFlightSchedulelD != 0)
context.Entry(entity.DepartureFlight).State = System.Data.EntityState.Deleted}
if (entity.ReturnFlightSchedulelD != 0)
context.Entry(entity.ReturnFlight).State = System.Data.EntityState.Deleted;
context.Reservations.Remove(entity);
}
public void Dispose()
{
if (context != null)
{
context.Di spose()j context = null;
}
GC.SuppressFinalize(this);
}
public IQueryable<Reservation> GetAllQ
{
return context.Reservations.AsQueryable<Reservation>();
}
public IQueryable<Reservation> FindBy(Expression<Func<Reservation, bool» predicate)
{
return GetAllQ .Where(predicate);
}
public void Add(Reservation entity)
{
context.Reservations.Add(entity);
}
public void Edit(Reservation entity)
{
var originalEntity = context.Reservations.Find(entity.Reservationld); context.Entry(originalEntity).CurrentValues.SetValues(entity);
}
public void SaveQ
{
context.SaveChanges();
}
}
8. Select FILE—Save All to save the changes.
9. Close Microsoft Visual Studio 2012.
Developing Windows Azure and
Web Services Lab @ home 2 Ans 3
Create
tests for the Entity Framework data model by issuing queries, manipulating
data, and using transactions to test multiple repositories at once.
The main tasks for this exercise are:
1. Add existing test project to solution.
2. Explore the existing integration test project.
3. Create queries by using LINQ to Entities.
4. Create queries by using Entity SQL and Raw SQL.
5. Create a test for manipulating data.
6. Create cross-repositories integration tests with System.Transactions.
7. Run the tests, and explore the database created by Entity framework.
The main tasks for this exercise are:
1. Add existing test project to solution.
2. Explore the existing integration test project.
3. Create queries by using LINQ to Entities.
4. Create queries by using Entity SQL and Raw SQL.
5. Create a test for manipulating data.
6. Create cross-repositories integration tests with System.Transactions.
7. Run the tests, and explore the database created by Entity framework.
To implement the required functionality, you need to perform the following tasks:
1. Add existing test project to solution.
2. Explore the existing integration test project.
3. Create queries by using LINQ to entities.
4. Create queries by using entity SQL and raw SQL.
5. Create a test for manipulating data.
6. Create cross-repositories integration tests with System.Transactions.
7. Run the tests, and explore the database created by entity framework.
Task 1: Adding Existing Test Project to Solution
1. Browse to the location where Exercise 02.zip file is saved.
2. Extract the files.
3. Double-click the Exercise 02 folder.
4. Double-click the BlueYonder.Companion folder.
5. Double-click the BlueYonder.Companion.sin file. The BlueYonder.Companion - Microsoft Visual Studio window displayed.
6. Ensure that the Solution Explorer window is opened.
7. Right-click the Solution 'BlueYonder.Companion' (3 projects) node, and then select Add—Existing Project. The Add Existing Project dialog box is displayed.
8. Double-click the BlueYonder.Companion folder.
9. Double-click the BlueYonder.IntegrationTests folder.
10. Select the BlueYonder.IntegrationTests.csproj file.
11. Click the Open button.
Task 2: Exploring the Existing Integration Test Project
1. Ensure that the Solution Explorer window is opened.
2. Ensure that the BlueYonder.IntegrationTests node is expanded.
3. Double-click the TravelCompanionDatabaselnitializer.es file. The TravelCompanionDatabaselnitializer.es file is displayed.
4. Explore the database initialization code in the Seed() method within the TravelCompanicnDatabaselnitializer class.
5. Double-click the FlightQueries.es file in the Solution Explorer window.
6. Explore the query test methods in the FlightQueries class. The Testlnitialize() static method is responsible for initializing the database and the test data, and all the other methods are intended to test various queries with lazy load and eager load.
7. Double-click the FlightActions.es file.The FlightActions.es file is displayed.
8. Explore the insert, update, and delete test methods in the FlightActions class. Observe the use of the Assert static class to verify the results of the test.
Task 3: Creating Queries by Using LINQ to Entities
1. Ensure that the Solution Explorer window is opened.
2. Ensure that the BlueYonder.IntegrationT ests node is expanded.
3. Double-click the ReservationQueries.es file. The ReservationQueries.es file is displayed.
4. Type the highlighted portions of the following code snippet inside the GetSingleReservaticn() test method:
4. Type the highlighted portions of the following code snippet inside the GetSingleReservation() test method:
[TestMethod]
public void GetSingleReservation()
{
using (var repository * new ReservationRepository())
{
var query = from r in repository.GetAll()
where r.ConfirmationCode == "1234” select r;
var reservation = query. FirstOrDefault();
Assert.IsNotNull(reservation);
>
5. Type the highlighted portions of the following code snippet inside the GetReservationWithFlightsEagerLoad() test method:
[TestMethod]
public void GetReservationWithFlightsEagerLoad()
{
Reservation reservation;
using (var repository = new ReservationRepository())
{
var query = from r in repository.GetAll()
where r.ConfirmationCode == "1234" select r;
query = query.Include(r => r.OepartureFlight).Include(r =>
r.ReturnFlight);
reservation = query.FirstOrDefauIt();
}
Assert.IsNotNul1(reservation);
Assert.IsNotNull(reservation.DepartureFlight);
Assert. IsNotNull(reservation.RetumFlight);
}
6. Type the highlighted portions of the following code snippet inside the GetReservationWithFlights LazyLoad() test method:
[TestMethod]
public void GetReservationWithFlightsLazyLoad()
{
Reservation reservation}
using (var repository = new ReservationRepository())
{
var query = from r in repository.GetAll()
where r.ConfirmationCode == "1234" select r;
reservation = query.FirstOrDefault();
Assert.IsNotNull(reservation)}
Assert.IsNotNull(reservation.DepartureFlight)}
Assert.IsNotNull(reservation.ReturnFlight)}
}
}
7. Type the highlighted portions of the following code snippet inside the Get ReservationWithSingleF light Failed LazyLoad () test method:
[TestMethod]
[ExpectedException(typeof(ObjectDisposedException))] public void GetReservationsWithSingleFlightFailedl_azyLoad() {
Reservation reservation;
using (var repository = new ReservationRepository())
{
var query = from r in repository.GetAll()
where r.ConfirmationCode == "1234" select r;
reservation = query.FirstOrDefault();
}
Assert.IsNotNull(reservation);
// We haven't eager loaded the departure flights,
// and the context is disposed, so lazy load will fail Assert.IsNotNull(reservation.DepartureFlight);
}
8. Select FILE—Save All to save the changes
Task 4: Creating Queries by Using Entity SQL and Raw SQL
1. Ensure that the Solution Explorer window is opened 2 Ensure that the ReservationQueries.es file is opened
3. Type the highlighted portions of the following code snippet inside theGetOrderedReservations() method:
[TestMethod]
public void GetOrderedReservations()
{
using (TravelCompanionContext context = new TravelCompanionContext())
{
ObjectQuery<Reservation> query =
((IObjectContextAdapter)context).ObjectContext.CreateQuery<Reservation>(
^"SELECT value r
FROM reservations as r
ORDER BY r.con-firmationCode DESC”);
List<Reservation> reservations = query.ToListQj Assert.AreEqual(reservations.Count, 2);
Assert.AreEqual(reservations[0]-ConfirmationCode, "4321");
Assert.AreEqual(reservations[l].ConfirmationCode, "1234");
}
}
4. Type the highlighted portions of the following code snippet inside theGetReservationsForDepartFlightsBySeatClass() method:
[TestMethod]
public void GetReservationsForDepartFlightsBySeatClass()
{
using (TravelCompanionContext context = new TravelCompanionContext())
{
IEnumerable<Reservation> query = context.Database.SqlQuery<Reservation>( @"select r.* from Reservations r
inner join Trips t on r.DepartFlightSchedulelD = t.TripId where t.Class = 2");
List<Reservation> flights = query.ToList();
Assert.AreEqual(flights.Count, 1)5
Assert.AreEqual(flights[0].ConfirmationCode, "1234");
}
}
5. Select FILE—Save All to save the changes
Task 5: Creating a Test for Manipulating Data
1. Ensure that the Solution Explorer window is opened
2 Ensure that the BlueYonder.IntegrationTests node is expanded
3. Double-click the FlightActions.es file. The FlightAetions.es file is displayed
4 Type the highlighted portions of the following code snippet inside the UpdateFlightQ method
[TestMethod]
public void UpdateFlight()
{
FlightRepository repository;
using (repository = new FlightRepositoryO)
{
Flight flight = repository.FindBy(f => f.FlightNumber ”BY002").Single();
flight.FlightNumber = "BY002_updated"; repository.Edit(flight); repository.Save();
}
using (repository = new FlightRepositoryO)
{
Flight flight = repository.FindBy(f => f.FlightNumber "BY002_updated").FirstOrDefault();
Assert.IsNotNull(flight);
}
}
5. Select FILE—Save All to save the changes
Task 6: Creating Cross-repositories Integration Tests with System.Transactions
1. Ensure that the Solution Explorer window is opened 2 Ensure that the FlightActions.es file is opened
3. Type the highlighted portions of the following code snippet inside the UpdateUsingTwoRepositories() method [TestMethod]
public void UpdateUsingTwoRepositories()
{
LocationRepository locationRepository = new LocationRepository(); FlightRepository flight Repository = new FlightRepository();
Flight flight, flightFromDbj Location location;
using (TransactionScope scope = nav TransactionScope())
{
// Update flight and location
flight = flightRepository.FindBy(f => f.FlightNuraber ==
"BY001") .Single();
flight.FlightNumber = "BY001_updated”i flightRepository.Edit(flight); flightRepository.Save();
location - locationRepository.FindBy(l => l.City == "Rome").SingleQj location.City = ”Rome_updated";
locationRepository.Edit(location)j locationRepository.Save();
// Verify the flight and location were updated in the current transaction flightFromDb = (from f in flightRepository.GetAllQ
where f.Source.City == "Rome_updated'' select f).FirstOrDefault();
Assert.IsNotNull(flightFromDb);
Assert.AreEqual(flightFromDb.FlightNumber, "BY001_updated")j // Do not commit the transaction //scope.Complete();
}
// Transaction was rollbacked, so the flight
// and location did not get updated
flightFromDb = (from f in flightRepository.GetAll()
where f.Source.City == "Rome_updated"
select f).FirstOrDefault();
Assert.IsNull(flightFromDb);
locationRepository.Dispose();
flightRepository.Dispose();
}
4. Select FILE—Save All to save the changes.
Task 7: Running the Tests and Exploring the Database Created by Entity Framework
1. Select Test—Windows—Test Explorer. The Test Explorer window is displayed.
2. Click Run All, and then wait for all the tests to complete.
3. Open the SQL Server Management Studio window.The Connect to Server dialog box is displayed.
4. Select and replace the text in the Server name text box with ASQLExpress
5. Select Windows Authentication from the Authentictaion drop-down list.
6. Click the Connect button.The Microsoft SQL Server Management Studio window is displayed.
7. Expand the Databases node in the Object Explorer window, and then expand the BlueYonder.Companion.Lab02 database.
8. Expand the Tables nodes
9. Notice that the Reservations and Trips tables are created.
10. Close the Microsoft SQL Server Management Studio window.
11. Switch to Microsoft Visual Studio 2012, and close it.
No comments:
Post a Comment