# Wednesday, May 13, 2009

I recently finished reading the first chapter of ASP.NET MVC 1.0, which deals strictly with the Nerd Dinner site that 3 of the 4 authors of the book put together.  This book has been a delight to read to this point and I highly recommend it.  In reading the first chapter I wrote  the code along with the book.  I wanted to make the code a source of reference for me in the future so while I was coding I added comments detailing points of interest from the book.  Along the way I thought making it available to the masses might be a good idea.  Since I wasn't sure on the legal ramifications of putting exact text from a book into my code comments I contacted one of the authors.  I received an email from said author a day later and he stated, and I quote “I say blog first, ask permission later.”  So that is exactly what I am doing.  You can download the full source here. Below is a sample...

        /// <summary>
        /// Attempts to perform an edit on the Dinner with the provided id using the provided formValues
        /// URL     :   /Dinners/Edit/[id]
        /// Verb    :   Post
        /// Purpose :   Save the form changes for a particular Dinner to the database
        /// </summary>
        /// <param name="id">Id of dinner to edit</param>
        /// <param name="formValues">Incoming form parameters</param>
        /// <returns>Details for view for the dinner that was edited</returns>
        /// <remarks>
        /// The "AcceptVerbs" attribute indicates it handles HTTP Post scenarios.
        /// When the attribute is applied to overloaded action methods ASP.NET MVC
        /// automatically handles dispatching to the appropriate action method depending
        /// on the incoming HTTP verb.
        /// For details on why MVC differentiates via HTTP verbs and doesn't use the 
        /// multiple URL approach see page 64.
        /// </remarks>
        public ActionResult Edit(int id, FormCollection formValues) {
            //Call method on our DinnerRepository to get the Dinner being edited
            var dinner = _dinnerRepository.GetDinner(id);
            //Verify that a dinner was found
            if (dinner == null)
                return View("NotFound");
            //Verify that the logged-in user matches the dinner host
            if (!dinner.IsHostedBy(User.Identity.Name))
                return View("InvalidOwner");
            //The "try" block will catch errors in calling UpdateModel or when we
            //try and save the DinnerRepository, which will throw if the Dinner object
            //we are tyring to save is invalid because of a rule violation.
            try {
                //The "UpdateModel" helper method on the Controller base class supports updating
                //the properties of the object being passed to it using the incoming form parameters.
                //It uses reflection to determine the property names on the object, and then automatically
                //converts and assigns values to them based on the input values submitted by the client.
                //The method will automatically update the "ModelState" collection when it encounters errors
                //while trying to assign form values to properties on the model object.  For example putting 
                //something other than a DateTime in the EventDate field.
                //Persist the changes
                //Return the Details view for the dinner being edited
                return RedirectToAction("Details", new{id = dinner.DinnerId});
            catch {
                //Handle edit errors
                //****** The code below was refactored to an extension method in the class ControllerHelpers
                ////Loop through the rule violations and add to ModelState
                //foreach (var issue in dinner.GetRuleViolations()){
                //    //The "ModelState" property collection provides a way to indicate that
                //    //errors exist with a model object being passed to a view.  Error entries
                //    //within the the "ModelState" collection identify the name of the model 
                //    //property with the issue and allow a human-friendly error message to be specified.
                //    //HTML helper methods like "HTML.TextBox" check the "ModelState" collection to see 
                //    //if there were any errors associated with the property on the model object the control
                //    //is associated with.  If it determines that there was an error it renders the submitted user
                //    //input as the value and adds a CSS error class to the markup it generates.
                //    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                //Example below uses ViewData...which is not strongly-typed
                //Add the SelectList to ViewData so the Countries drop down list is populated when rendered
                //ViewData["countries"] = new SelectList(PhoneValidator.Countries, dinner.Country);
                //Example using ViewModel which is strongly-typed
                return View(new DinnerFormViewModel(dinner));
                //Return the Dinner view
                //return View(dinner);

posted on Wednesday, May 13, 2009 5:17:26 PM (Central Daylight Time, UTC-05:00)  #    Comments [6]
# Sunday, May 10, 2009
An Alt.Net user group is being organized in the Green Bay/Fox Valley area.  Check out the Yahoo Group for more information!

posted on Sunday, May 10, 2009 6:51:24 PM (Central Daylight Time, UTC-05:00)  #    Comments [8]
# Tuesday, April 28, 2009

The 2nd annual Fox Valley Day of .Net is being held Saturday May 9th.  We are having some great speakers and topics this year and also a bunch of great prizes.  Visit the FVNUG website for more information and to register.
posted on Tuesday, April 28, 2009 7:19:39 PM (Central Daylight Time, UTC-05:00)  #    Comments [2]
# Sunday, April 26, 2009

I figured I was to the point where I was comfortable enough with the concept of mocking that I would start implementing it in one of the projects I am working on at my current client.  The data access layer of the application obtains data from two sources; a CIS system that is only reachable via web services and a data warehouse on a SQL Server that we are accessing directly using stored procedures and LINQ to SQL.  I started with mocking the web service piece and that went off without a hitch. 

The Linq To SQL piece did not go as smoothly.  I had a few issues creating the Interface for the DataContext and then ran into trying to mock System.Data.Linq.Table which is Sealed.  Instead of copying and pasting the code in the post and explaining it I think that I will let the code speak for itself.  If you are like me you find it much easier to understand complex code in the solution structure offered by Visual Studio.  So grab the code from here and dive in.

posted on Sunday, April 26, 2009 7:26:34 PM (Central Daylight Time, UTC-05:00)  #    Comments [6]
# Thursday, April 23, 2009

I have recently started researching the mocking framework RhinoMocks and finding good resources hasn’t been as easy as I was hoping it would be.  I decided to post the useful links I found to this point.  I am hoping this will become a living post for all RhinoMocks references worth tracking.

Reader Submitted
posted on Thursday, April 23, 2009 3:39:00 AM (Central Daylight Time, UTC-05:00)  #    Comments [4]