Carpool Project: Part #10

I went back to the update of the Carpool Controller and changed the view to include the Driver like so:

Carpool62

I then updated the driverId to see if the changes propagate. The new value is in the form collection:

Carpool63

But after running UpdateView(), the new value is dropped.

Carpool64

Apparently, only the top-level properities are affected by UpdateView? Ugh – between the date/time conflict the nested properties, UpdateView only is useful for the most basic scenarios. I’ll replace UpdateView with a method that I write.

I thought about using Reflection like so (this is not right):

1 private void UpdateCarpoolFromFormCollection(Carpool.Domain.Carpool carpool, FormCollection collection) 2 { 3 4 PropertyInfo[] propertyInfos = carpool.GetType().GetProperties(); 5 foreach (PropertyInfo propertyInfo in propertyInfos) 6 { 7 for (int i = 0; i < collection.Count; i++) 8 { 9 if (propertyInfo.Name == collection[i].ToString()) 10 { 11 propertyInfo.SetValue(carpool,collection[i],null); 12 } 13 } 14 } 15 16 } 17  

I realized I really don’t understand the FormCollection object and how to enumation (or write some LINQ) to pull the correct value out of it. I then realized that I am being stupid. Of course the MVC creators would allow for next properities. I wrote a quick hello world app and sure enough, the changes are coming down:

Carpool65

The difference is the “.”. In my carpool solution, the Carpool.Drver is somehow turning into CarpoolDriver:

Carpool66

Changing this

1 <div class="editor-label"> 2 <%: Html.LabelFor(model => model.Driver.Id) %> 3 </div> 4 <div class="editor-field"> 5 <%= Html.TextBox("carpoolDriverId", Model.Driver.Id)%> 6 <%: Html.ValidationMessageFor(model => Model.Driver.Id)%> 7 </div> 8  

To This

1 2 <div class="editor-label"> 3 <%: Html.LabelFor(model => model.Driver.Id) %> 4 </div> 5 <div class="editor-field"> 6 <%: Html.TextBoxFor(model => model.Driver.Id)%> 7 <%: Html.ValidationMessageFor(model => model.Driver.Id)%> 8 </div> 9  

 

And boom goes the dynamite…

Carpool Project: Part #9

I started with the View a bit more and I ran into an issue that cuts to the heart of D3 and SOC. On my Carpool page, I want a grid of all practices that are associated with the car pool (usually 1, but could be more). In the view, I wrote this (headers omitted for brevity):

1 <% foreach (Com.Tff.Carpool.Domain.Practice practice in Model.Practices) 2 { %> 3 <tr> 4 <td> 5 <%: practice.Id%> 6 </td> 7 <td> 8 <%: practice.Date%> 9 </td> 10 <td> 11 <%: practice.DryLandIndicator%> 12 </td> 13 <td> 14 <%: practice.EndTime%> 15 </td> 16 <td> 17 <%: practice.StartTime%> 18 </td> 19 <td> 20 <% foreach (Com.Tff.Carpool.Domain.SwimGroup swimGroup in practice.SwimGroups) 21 { %> 22 <%: swimGroup.Name%> 23 <% } %> 24 </td> 25 <td> 26 <%: Html.ActionLink("Edit", "Edit", new { practiceId = practice.Id })%> 27 <%: Html.ActionLink("Delete", "Delete", new { PracticeId = practice.Id })%> 28 </td> 29 </tr> 30 31 <% } %> 32  

And it comes out with the expected HTML:

carpool32

Notice how the Swim Group is joined together using that secondary loop. This got me thinking, part of the reason I crated the ViewModel POCOs was to do this kind of flattening. If the UI can handle it easily, why not use EF-enabled POCOs and save the who Domain Layer step of mapping the EF Classes to the POCO ones? If all I had to worry about was the flattening of Swim Groups to practices, perhaps writing that join in the UI is better than the Domain Layer. Then I thought – NO. The domain layer should be responsible for serving objects, even if the UI can handle it. I would say that is analogous to “we can put the code in the code-behind in Web Forms.” Eventually, the code needs to be written – so I would rather have it in the Domain layer where I have exact control and can add in any custom logic. If any custom logic for the flattening happens, the UI will quickly become a morass of spaghetti. On a side note, I find writing C# is much much better than writing WebForms View Engine code – those gators drive me nuts.

Saying that, I realized I don’t need a carpool controller, I need a CarpoolSummary controller and view -> for the simple reason that Carpools don’t have PracticeSummaries – CarpoolSummaries do. Time to do some re-writing!

Carpool Project: Part #8

I am working on the Update features of the Carpool Factory. The biggest problem with using POCOs that are not automatically hooked up with EF classes is that you have to do the mapping back to the EF classes. Going from EF to POCO is straight forward – putting them back in is a different story.

I had 3 different challenges

#1) Assign properties to the main class. That was straight forward:

1 private static void MapCarpool_Carpool(Carpool_Carpool carpool_carpool, Carpool carpool) 2 { 3 carpool_carpool.CarPoolDate = carpool.Date; 4 carpool_carpool.SwimmerCapacity = carpool.SwimerCapacity; 5 carpool_carpool.TimeBack = carpool.ReturnTime; 6 carpool_carpool.TimeLeaving = carpool.LeaveTime; 7 } 8  

#2) Assign properties to another one-to-one class in the graph. The was also straight forward, with the caveat that you need to use the same EF conext for the entire graph so you need to pass that into the function:

1 private static void MapCarpool_CarpoolDriver(Carpool_Carpool carpool_carpool, Carpool carpool, CarpoolEntities carpoolEntity) 2 { 3 var selectedDriver = (from driver in carpoolEntity.Carpool_Driver 4 where driver.DriverID == carpool.Driver.Id 5 select driver).First(); 6 carpool_carpool.Carpool_Driver = selectedDriver; 7 } 8

#3) Assign properties to a collection of one-to-many classes in the graph. I tried a couple of different ways to do this with the least amount of code. The problem is that the collection could have had adds and deletes on the client that need to be synched up to the server:

Client   Server
    SwimmerA
SwimmerB   SwimmerB
SwimmerC   SwimmerC
SwimmerD    

My first thought was to clear the EF Collection and then then add in the POCOs like so:

1 carpoolToUpdate.Carpool_CarpoolPractice.Clear(); 2 3 Carpool_CarpoolPractice carpool_carpoolPractice = null; 4 5 foreach (Practice practice in carpool.Practices) 6 { 7 carpool_carpoolPractice = new Carpool_CarpoolPractice(); 8 carpool_carpoolPractice.CarpoolId = carpool.Id; 9 carpool_carpoolPractice.PracticeId = practice.Id; 10 carpoolToUpdate.Carpool_CarpoolPractice.Add(carpool_carpoolPractice); 11 } 12

 

The problem is that I could not clear the collection b/c of a referential constraint, even though I immediately re-added new objects before the Update method to the server.

1System.InvalidOperationException was unhandled by user code 2 Message=The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted. 3 Source=System.Data.Entity 4

Some digging found this article which explained why.

So then I thought to enumerate though the collection and remove each one and then the POCOs in. A couple more lines of code but that did the trick for adding the practice. Here is the code:

1 //Remove 2 foreach (Carpool_CarpoolPractice carpool_carpoolPracticeToDelete in carpoolEntity.Carpool_CarpoolPractice) 3 { 4 carpoolEntity.DeleteObject(carpool_carpoolPracticeToDelete); 5 } 6 7 //Add 8 Carpool_CarpoolPractice carpool_carpoolPracticeToAdd = null; 9 foreach (Practice practice in carpool.Practices) 10 { 11 carpool_carpoolPracticeToAdd = new Carpool_CarpoolPractice(); 12 carpool_carpoolPracticeToAdd.CarpoolId = carpool.Id; 13 carpool_carpoolPracticeToAdd.PracticeId = practice.Id; 14 carpoolToUpdate.Carpool_CarpoolPractice.Add(carpool_carpoolPracticeToAdd); 15 } 16

And here is the test:

1 [TestMethod()] 2 public void AddCarpoolPracticeTest() 3 { 4 Carpool carpool = CarpoolFactory.GetCarpool(1); 5 int notExpcted = carpool.Practices.Count; 6 7 Practice practice = PracticeFactory.GetPractice(2); 8 carpool.Practices.Add(practice); 9 CarpoolFactory.UpdateCarpool(carpool); 10 11 carpool = CarpoolFactory.GetCarpool(1); 12 int actual = carpool.Practices.Count; 13 14 Assert.AreNotEqual(notExpcted, actual); 15 } 16

 

The problem is the removal of the practice. I wrote this test:

1 [TestMethod] 2 public void DeleteCarpoolPracticeTest() 3 { 4 Carpool carpool = CarpoolFactory.GetCarpool(1); 5 int notExpcted = carpool.Practices.Count; 6 7 Practice practice = PracticeFactory.GetPractice(2); 8 carpool.Practices.Remove(practice); 9 10 CarpoolFactory.UpdateCarpool(carpool); 11 12 carpool = CarpoolFactory.GetCarpool(1); 13 int actual = carpool.Practices.Count; 14 15 Assert.AreNotEqual(notExpcted, actual); 16 } 17

 

And got the following fail:

carpool29

I need to add a comparable to the practice so that the remove recognize practices with the same PracticeId as the same.

1 public class Practice : ValidationBase, IEquatable<Practice> 2 { 3 public int Id { get; set; } 4 public DateTime Date { get; set; } 5 public DateTime StartTime { get; set; } 6 public DateTime EndTime { get; set; } 7 public bool DryLandIndicator { get; set; } 8 public List<SwimGroup> SwimGroups { get; set; } 9 10 public bool Equals(Practice other) 11 { 12 if (other.Id == this.Id) 13 return true; 14 else 15 return false; 16 17 } 18 } 19

 

And then the remove worked like a champ. This led me to my next (and last of the morning) thought – should I implement IEquiatable on the base abstract class (Validation Base) since all of the derived classes have a PK. If so, how would I implement that? I’ll do some research. Until then, my Carpool class now looks like it is ready for Updates. Unfortunately, looks are deceiving…

I started making my UI a bit more user-friendly. I put on a jQuery date picker for the carpool start date. I verified that the new date was being passed into the controller via the FormCollection (it was), but then the UpdateModel is reverting the change:

carpool30

And

carpool31

Oddly, the Update work for date/time values. I guess it cannot handle having a date without a time. I added a new line of code to handle this case:

1 try 2 { 3 Carpool.Domain.Carpool carpool = CarpoolFactory.GetCarpool(carpoolId); 4 UpdateModel(carpool); 5 carpool.Date = DateTime.Parse(collection[2].ToString()); 6 CarpoolFactory.UpdateCarpool(carpool); 7 return RedirectToAction("Index"); 8 } 9

 

Yuck, but it is the easiest way. Perhaps in the UI I can format the input?

Carpool Project: Part #7

I am now moving into the site navigation of the carpool site. It was my 1st real foray into routing and after a couple of minutes, I realized it is straightforward. I had 2 controllers:

1 for the main page that shows 2 grids (current carpools and uncovered practices)

1 for the carpool update, insert, and delete pages

Here is an example of the Update method (so far):

1 public ActionResult Edit(int carpoolId) 2 { 3 Carpool.Domain.Carpool carpool = CarpoolFactory.GetCarpool(carpoolId); 4 return View(carpool); 5 } 6 7 [HttpPost] 8 public ActionResult Edit(int carpoolId, FormCollection collection) 9 { 10 try 11 { 12 Carpool.Domain.Carpool carpool = CarpoolFactory.GetCarpool(carpoolId); 13 UpdateModel(carpool); 14 CarpoolFactory.UpdateCarpool(carpool); 15 return RedirectToAction("Index"); 16 } 17 catch 18 { 19 return View(); 20 } 21 } 22  

 

I had to do some redirecting – there is no Index controller/page for the carpool as it is all handled out of Main.Index. I created the follwoign routes to allow me to redirect from one controller to the next:

1 routes.MapRoute( 2 "EditCarpool", 3 "Home/Edit", 4 new { controller = "Carpool", action = "Edit" } 5 ); 6 7 routes.MapRoute( 8 "CreateCarpool", 9 "Home/Create", 10 new { controller = "Carpool", action = "Create" } 11 ); 12 13 routes.MapRoute( 14 "DeleteCarpool", 15 "Home/Delete", 16 new { controller = "Carpool", action = "Delete" } 17 ); 18 19 routes.MapRoute( 20 "ReturnHome", 21 "Carpool", 22 new { controller = "Home", action = "Index", id = UrlParameter.Optional } 23 ); 24 25 routes.MapRoute( 26 "Default", // Route name 27   "{controller}/{action}/{id}", // URL with parameters 28 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 29 ); 30

My only question is how to add in the parameter so it looks like Carpool/Update/2 and not Carpool/Update?carpoolId = 2

Carpool Project: Part #6

I finished the factories and the POCOs. I made some changes that simplified the model some – I set up 7 factories for the non-flattened POCOs and 1 factory for a flattened POCO:

carpool21

I will see if the other flattened POCOs are needed. With enough factories to make me dangerous, I then thought about how to organize the UI. Since this exercise is to help me work though the Architectual features of VS2010, I created an activity diagram based on some of the Use Cases:

carpool22

Note how I started documenting business rules. I went back to the Task list for each activity and added both the data elements and the business rules:

carpool23 

I then have enough to launch a tracer bullet that matches how I want the home page to be. The challenge is that the home page will actually show 2 independent grids that use different data elements:

· A list of all practices without a carpool

· A list of all carpools

I don’t know the best way to implement this in MVC. In prior projects, I had 1 controller for the page that handles the “primary” data elements and then some AJAX calls to dynamically populate the remaining grids. Another possible way is to have the page be a composite of 2 independent User Controls. A final way is to have the page controller’s model collection just include both data element sets and refer to each in the collection.

I had already tried Option #1 on a different project and found that it requires too much JavaScript code. I then explored Option #2. I set up my MVC’s project controllers to match the domain model like so:

carpool24

I then tried to wire up some User Controls for each of the controllers that I could then combine on the Default page. I quickly learned that MVC2 User Controls Are not well supported in the MVC community (30 minutes of Bing/Google/MSDN searches did not give me a specific example of MVC2 User Controls tied together in 1 page). I also realized that I was taking a “bottom-up” approach.

I then revamped my project to take a “top-down” approach. Incidentally, this option seems to be the “happy path” for MVC2. I removed all of the POCO controllers:

carpool25

And then added the POCOs that I want on the main page to the ViewModel Collection:

1 public ActionResult Index() 2 { 3 ViewData["Message"] = "Welcome to ASP.NET MVC!"; 4 ViewData["CurrentCarpoolSummaries"] = CarpoolSummaryFactory.GetCurrentWeekCarpoolSummaries(); 5 ViewData["UncoveredPraciceSummaries"] = PracticeSummaryFactory.GetCurrentWekUncoveredPracticeSummaries(); 6 7 return View(); 8 } 9  

And then wired up the UI:

1 <table> 2 <tr> 3 <th> 4 Id 5 </th> 6 <th> 7 Day 8 </th> 9 <th> 10 Date 11 </th> 12 <th> 13 Description 14 </th> 15 <th> 16 Pracitce(s) 17 </th> 18 <th> 19 Driver 20 </th> 21 <th> 22 Swimmer(s) 23 </th> 24 <th> 25 Remaining Seats 26 </th> 27 </tr> 28 29 <% foreach (Com.Tff.Carpool.Domain.CarpoolSummary carpoolSummary in ViewData["CurrentCarpoolSummaries"]) 30 { %> 31 32 <tr> 33 <td> 34 <%: carpoolSummary.Id%> 35 </td> 36 <td> 37 <%: carpoolSummary.DayDescription%> 38 </td> 39 <td> 40 <%: carpoolSummary.CarpoolDate%> 41 </td> 42 <td> 43 <%: carpoolSummary.CarpoolDescription%> 44 </td> 45 <td> 46 <%: carpoolSummary.PracticeDescription%> 47 </td> 48 <td> 49 <%: carpoolSummary.DriverDescription%> 50 </td> 51 <td> 52 <%: carpoolSummary.SwimmerDescription%> 53 </td> 54 <td> 55 <%: carpoolSummary.RemainingSeats%> 56 </td> 57 </tr> 58 59 <% } %> 60 </table> 61  

I ran into this problem:

carpool26

It looks like I need to cast it via the page Inherits method – but I have multiple datasets. I tried ignoring the error – hoping that the object resolves at run time. No such luck:

carpool27

So I went back and casted the model into the proper type:

1 <% foreach (Com.Tff.Carpool.Domain.CarpoolSummary carpoolSummary in (System.Collections.Generic.List<Com.Tff.Carpool.Domain.CarpoolSummary>)ViewData["CurrentCarpoolSummaries"])

And

1 <% foreach (Com.Tff.Carpool.Domain.PracticeSummary uncoveredPracticeSummaries in (System.Collections.Generic.List<Com.Tff.Carpool.Domain.PracticeSummary>)ViewData["UncoveredPraciceSummaries"])

And boom goes the dynamite…

carpool28

Carpool Project: Part #5

I am now building factories for my POCOs. I have a design question. Should POCOs hold the key to another object or an actual reference? DDD says that I should a reference and EF does that, but POCOs do not seem to pattern that way.

The choice is this:

1 public class Swimmer : ValidationBase 2 { 3 public int Id { get; set; } 4 public string Name { get; set; } 5 public SwimGroup SwimGroup { get; set; } 6 } 7  

or this (flattened class):

1 public class Swimmer : ValidationBase 2 { 3 public int Id { get; set; } 4 public string Name { get; set; } 5 public int SwimGroupId { get; set; } 6 public string SwimGroupName { get; set; } 7 } 8  

I see the benefits to both sides. On one hand, the flattened class (option #2) makes it very easy to code up the UI. On the other hand, option #2 is much more rigid and brittle – you can have lots of properties and a change to 1 entity class means that every value object that depends on the entity object has to change. You can add interfaces for each of the permutations of the flattened classes to account for change, but that becomes unwieldy.

After going back and forth a couple of times, I settled on doing both. I left my base class (Swimmer) as option #1 and then build a SwimmerSummary class that is option #2. With both, I will see what is easier to work with on the UI.

Starting on Option #1, I created a Swimmer Factory with a mapper function like this:

Carpool17

The problem is that the SwimGroup reference is expected my Domain-Defined class, not the EF class. I need to wire up a factory for the SwimGroup and pass a Domain-Driven class like this:

1 Swimmer swimmer = new Swimmer { 2 Id = carpool_swimmer.SwimmerId, 3 Name = carpool_swimmer.SwimmerName}; 4 5 SwimGroupFactory swimGroupFactory = new SwimGroupFactory(); 6 swimmer.SwimGroup = swimGroupFactory.GetSwimGroup(carpool_swimmer.Carpool_SwimGroup.SwimGroupId); 7 8 return swimmer; 9  

Which leads me to my next problem – how to get both the Swimmer and the SwimGroup in the same database call? As this is written, each factory has a unique reference to the EF class, so getting the swimmer and his/her SwimGroup means 2 round trips. I can put them all into a single call (Unit of work) by using the include statement, but then I will need to change the factory to do the mapping WITHOUT the database call.

I started implanting this, but then I ran into references two deep – Swimmer needs SwimGroup which needs SwimTeam – so the SwimmerFactory has to know to call for SwimGroup and SwimTeam in it’s EF call. I wonder if there are guidelines about how deep to make the references?

So I have this:

1 public static SwimGroup MapSwimGroup(Carpool_SwimGroup carpool_swimgroup) 2 { 3 SwimGroup swimGroup = new SwimGroup { 4 Id = carpool_swimgroup.SwimGroupId, 5 Name = carpool_swimgroup.SwimGroupName, 6 SwimTeam = SwimTeamFactory.MapSwimTeam(carpool_swimgroup.Carpool_SwimTeam) }; 7 8 return swimGroup; 9 } 10  

and this:

1 public static Swimmer MapSwimmer(Carpool_Swimmer carpool_swimmer) 2 { 3 Swimmer swimmer = new Swimmer { 4 Id = carpool_swimmer.SwimmerId, 5 Name = carpool_swimmer.SwimmerName, 6 SwimGroup = SwimGroupFactory.MapSwimGroup(carpool_swimmer.Carpool_SwimGroup) }; 7 8 return swimmer; 9  

and then a EF call that does this:

1 public Swimmer GetSwimmer(int swimmerId) 2 { 3 var selectedSwimmer = (from swimmer in carpoolEntity.Carpool_Swimmer 4 .Include("Carpool_SwimGroup") 5 .Include("Carpool_SwimGroup.Carpool_SwimTeam") 6 where swimmer.SwimmerId == swimmerId 7 select swimmer).First(); 8 9 return MapSwimmer(selectedSwimmer); 10 } 11  

The 2 things I need to be mindful of are:

· No intellisense on the include statement – so the name has to match

· I need to check for null in the mapping in case the calling function forgets the include statement. My mapping cannot handle lazy loading.

This got me thinking about the Factory pattern in general. My Factory class is actually a plant with 2 factories. Factory 1 maps EF classes to POCO classes. Factory 2 class calls EF and returns POCOs. I wonder if I need to divide these 2 functions into separate classes? The examples of the repository pattern that I have seen keep them together – but I have only seen them in a “hello nerd dinner” context.

I then thought harder about the subject and realized that I missing the point of the factory. Conceptually, I picture a window with Alot behind it. I can either hand him an int or a EF class. No matter what I pass in, I get a POCO class out. In this sense, the factory spits out POCOs and I can use overloaded parameters to handle the different permutations that come in. The factory class then looks like this:

1 public class SwimGroupFactory 2 { 3 CarpoolEntities carpoolEntity = null; 4 5 public SwimGroupFactory() 6 { 7 carpoolEntity = new CarpoolEntities(); 8 } 9 10 public SwimGroup GetSwimGroup(int swimGroupId) 11 { 12 var selectedSwimGroup = (from swimGroup in carpoolEntity.Carpool_SwimGroup 13 where swimGroup.SwimGroupId == swimGroupId 14 select swimGroup).First(); 15 return MapSwimGroup(selectedSwimGroup); 16 } 17 18 public SwimGroup GetSwimGroup(Carpool_SwimGroup carpool_swimgroup) 19 { 20 return MapSwimGroup(carpool_swimgroup); 21 } 22 23 24 private static SwimGroup MapSwimGroup(Carpool_SwimGroup carpool_swimgroup) 25 { 26 SwimGroup swimGroup = new SwimGroup { 27 Id = carpool_swimgroup.SwimGroupId, 28 Name = carpool_swimgroup.SwimGroupName, 29 SwimTeam = SwimTeamFactory.MapSwimTeam(carpool_swimgroup.Carpool_SwimTeam) }; 30 31 return swimGroup; 32 } 33 } 34  

Which is great – but then the new class uses a new EF. I have 2 EF classes, though only 1 is used for an active connection. Perhaps the EF class should be moved from the constructor and placed in the methods that actually use it…

So I refactored and got all green – making my factories static and overloaded methods passing in ints or EF classes. For ints, the methods created a new EF context and queries the db. For EF classes, it just maps and send back…

For example:

1 public static SwimGroup GetSwimGroup(int swimGroupId) 2 { 3 using (CarpoolEntities carpoolEntity = new CarpoolEntities()) 4 { 5 var selectedSwimGroup = (from swimGroup in carpoolEntity.Carpool_SwimGroup 6 where swimGroup.SwimGroupId == swimGroupId 7 select swimGroup).First(); 8 return MapSwimGroup(selectedSwimGroup); 9 } 10 } 11 12 public static SwimGroup GetSwimGroup(Carpool_SwimGroup carpool_swimgroup) 13 { 14 return MapSwimGroup(carpool_swimgroup); 15 } 16 17 private static SwimGroup MapSwimGroup(Carpool_SwimGroup carpool_swimgroup) 18 { 19 SwimGroup swimGroup = new SwimGroup { 20 Id = carpool_swimgroup.SwimGroupId, 21 Name = carpool_swimgroup.SwimGroupName, 22 SwimTeam = SwimTeamFactory.GetSwimTeam(carpool_swimgroup.Carpool_SwimTeam) }; 23 24 return swimGroup; 25 } 26 } 27  

I then used this pattern for the rest of my factories…

Carpool Project: Part #4

I wrote this factory code:

1 public SwimTeam GetSwimTeam(int swimTeamId) 2 { 3 var selectedSwimTeam = (from swimTeam in carpoolEntity.Carpool_SwimTeam 4 where swimTeam.SwimTeamId == swimTeamId 5 select swimTeam).First(); 6 7 return MapSwimTeam(selectedSwimTeam); 8 9 } 10 11 public List<SwimTeam> GetAllSwimTeams() 12 { 13 List<SwimTeam> swimTeams = new List<SwimTeam>(); 14 15 var swimTeamsQuery = (from swimTeam in carpoolEntity.Carpool_SwimTeam 16 select swimTeam); 17 18 foreach(Carpool_SwimTeam carpool_Swimteam in swimTeamsQuery) 19 { 20 swimTeams.Add(MapSwimTeam(carpool_Swimteam)); 21 } 22 23 return swimTeams; 24 } 25  

How do I know that I wrote it right?

Unit Test? Integration Test? I don’t care what you call it – I want to make sure it is right BEFORE I use this as a pattern for the rest of the project.

So I created a couple of unit tests (and remembering to add the app.config to my unit test project)

1 [TestMethod()] 2 public void GetSwimTeamTest() 3 { 4 string expected = "RSA"; 5 string actual = SwimTeamFactory.GetSwimTeam(1).Name; 6 Assert.AreEqual(expected, actual); 7 } 8 9 /// <summary> 10 ///A test for GetAllSwimTeams 11 ///</summary> 12   [TestMethod()] 13 public void GetAllSwimTeamsTest() 14 { 15 int expected = 3; 16 int actual = SwimTeamFactory.GetAllSwimTeams().Count; 17 Assert.AreEqual(expected, actual); 18 } 19  

And got green:

Carpool14

I then wired up the UI

I added a controller to my MVC application

1 public class SwimTeamController : Controller 2 { 3 SwimTeamFactory swimTeamFactory = null; 4 5 public SwimTeamController() 6 { 7 swimTeamFactory = new SwimTeamFactory(); 8 } 9 10 public ActionResult Index() 11 { 12 return View(swimTeamFactory.GetAllSwimTeams()); 13 } 14 15 } 16

Added the connection string to the Web.Config

Added an auto generated View

Carpool15

 

And boom goes the dynamite…

Carpool16