Wednesday, September 10, 2008

I will be delivering two presentations next Tuesday September 16 at the next Northwest Ohio .Net User Group beginning at 6PM.  The topics are:

Extending your Application with the Managed Extensibility Framework

Microsoft Managed Extensibility (MEF) framework allows developers to add “hooks” into their application to make it extensible at runtime.  These hooks allow you or a third party to extend your application dynamically in the future.  In this session, we will review the MEF tool set and build an extensible application and then extend that application using MEF.

Using Microsoft Distributed Cache to speed your application

Retrieving data from a disc or a database can be a time-consuming operation.  Data that is accessed frequently can be stored in an in-memory cache, which can speed up its retrieval considerably.  Microsoft Distributed Cache (aka “Velocity”) provides a framework for storing and managing cached data.  In this session, we will discuss how to use this framework in your application and demonstrate some code that implements this framework.

HCR Manorcare building at 333 North Summit St. in Toledo.  Click here to view a map.

I'm looking forward to my first visit to this user group in at least five years.

You can read more at http://www.nwnug.com/PermaLink,guid,1877615d-a53b-4b05-b6f6-5d650208af6f.aspx

Wednesday, September 10, 2008 9:47:20 AM (Eastern Standard Time, UTC-05:00)
 Wednesday, August 27, 2008

Microsoft recently released the ASP.Net Model View Controller framework (MVC).  It is currently available as Preview 3 and can be downloaded at http://www.microsoft.com/downloads/details.aspx?FamilyId=92F2A8F0-9243-4697-8F9A-FCF6BC9F66AB&displaylang=en

A new MVC project contains a couple sample views and controllers so you can get an idea of the proper syntax to use. 

This article builds on the application created in my last ASP.Net MVC tutorial.  If you have not already done so, please follow the brief states in the previous MVC tutorial before beginning this tutorial

In the last tutorial, we added a model, view and controller to display a list of customers that one can navigate to using a URL formatted as controller/action.  In this article, we will add a new view and controller to an existing MVC project and display details of a single customer using a URL formatted as controller/action/id.  The id is passed automatically to the action method and allows us to filter to a single customer.

1.       Open Visual Studio 2008 and open the TestMVC solution created in MVC Tutorial 2.

2.       Open the Solution Explorer (View | Solution Explorer) and select the Controllers\CustomerController.cs.   Double-click CustomerController to open it in the code editor.

a.       In the CustomerController class, we will create a new action to get the details of a single customer.   

                                                               i.      Add the following private GetCustomer method to the CustomerController class.  In the last tutorial, we wrote methods to retrieve customer 1 and customer 2.  For simplicity, we will get Customer 1 if ID 1 is passed in to our method and Customer 2 if any other ID is passed.

        private Customer GetCustomer(int custID)

        {

            if (custID == 1)

            {

                return GetCustomer1();

            }

            else

            {

                return GetCustomer2();

            }

        }

                                                             ii.      Add an Action method to the CustomerController class to get the details of a customer.  Paste the following code into CustomerController.cs.

        public ActionResult Details(int id)

        {

            Customer cust = GetCustomer(id);

            return View("Details", cust);

        }

                                                            iii.      In the GetCustomer method, we get details of a single customer and return a view.  Unlike the generated code, we explicitly specify which view to return (“Details”) and we pass in some extra data (cust) that the view will consume.

3.       Add a view to the project.

a.       In the Solution Explorer, right-click the Views\Customer folder and select Add | New Item.  The Add New Item dialog displays.

    Figure 1

                                                               i.      Under Categories, select Visual C#\Web\MVC.

                                                             ii.      Under Templates, select  MVC View Content Page.

                                                            iii.      In the Name textbox, enter “Details”.

                                                           iv.      The Select a Master Page dialog displays.  

   Figure 2

1.       Navigate to the Views\Shared folder and select Site.Master.

2.       Click the OK button to add this view content page to the project.

b.      Add visual elements to the View

                                                               i.      If it is not already open, open the Details view by double-clicking List.aspx in the Solution Explorer.  Click the Source tab at the bottom of the editor.

                                                             ii.      Replace the code in Details.aspx with the following

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" CodeBehind="Details.aspx.cs" Inherits="TestMVC.Views.Customers.Details" %>

<%@ Import Namespace="TestMVC.Views.Customers"%>

<%@ Import Namespace="TestMVC.Models" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">

    <div>

        CustID:

        <%=((Customer)ViewData.Model).CustID %><br />

        Name:

        <%=((Customer)ViewData.Model).FirstName %>

        <%=((Customer)ViewData.Model).LastName %>

        <br />

        Address:

        <%=((Customer)ViewData.Model).StreetAddress%><br />

        City:

        <%=((Customer)ViewData.Model).City %><br />

        State:

        <%=((Customer)ViewData.Model).State %><br />

        ZIP:

        <%=((Customer)ViewData.Model).PostalCode%><br />

    </div>

 

</asp:Content>


The above code displays details of a single Customer model object. 

4.       Modify the List view

a.       Open Views\Customers\List.aspx by double-clicking it in Solution Explorer.

b.      Find the <td>&nbsp;<\td> cell tag following the cust.PostalCode cell.  Replace this with the following code.  This generates a hyperlink displaying the text “Details”.  The URL of the hyperlink will have the current controller (“Customer”) the “Details” Action and the ID of the current customer.

<td>

<%=Html.ActionLink("Details", "Details", new { ID=cust.CustID})%>

</td>

5.       Test the application

a.       Save and compile the solution (Build | Build Solution).   Correct any errors you find.

b.  Run the solution (Debug | Start Debugging).  Depending on the port number used by Casini, it should display in your browser with a URL such as
http://localhost:4152/Home

c.       Navigate to the List page by changing the URL to
http://localhost:4152/Customers/List
(Replace the port number if necessary)

d.      You should see a list of 2 customers in your browser.  Each customer should have a hyperlink labeled “Details”. 

    Figure 3

e.      Click the hyperlink next to customer 1.  The URL should change to
http://localhost:4152/Customers/Details/1
and the details of the first customer should display on screen.

    Figure 4

f.        Set a breakpoint in the Details method of CustomerController.cs and refresh the page to step through the code as it executes.

In this article, we added a view and controller to our application and used these to retrieve and display customer details by the Customer ID. 

.Net | ASP.NET | MVC | Tech
Wednesday, August 27, 2008 7:26:29 AM (Eastern Standard Time, UTC-05:00)
 Monday, August 25, 2008

People attend conferences for many different reasons.  Some come for the content of the lectures; some come to meet and hear well-known speakers; some come to meet and network with others in the industry; some come to see old friends.

Me, I come for all those reasons.  At DevLink last week in Murfreesboro, TN, I experienced all those things and more. 

But I also experienced something new.  I had heard of Open Spaces in the past but had not experienced them.  At DevLink, Open Spaces were promoted heavily as a different way of exchanging ideas.  I was curious and gave it a try.

An Open Space event consists of developers sitting together roughly in a circle in a room and they exchange ideas with one other.  A topic is picked in advance by the group but the conversation is not limited to that topic.  If the conversation drifts from the assigned topic and the group remains engaged, this is perfectly all right.  The important thing is that ideas are exchanged and the group remains passionate about the conversation.

And I heard a great deal of passion at the DevLink Open Spaces that I attended.

During the event, I attended 3 Open Spaces sessions plus the planning session (where topics were picked) and the wrap-up session (where the group reviewed the open spaces of the previous 2 days).  In each session I attended, I heard bright people sharing great ideas.  Sometimes we argued and sometimes we were in violent agreement but I enjoyed it all. 
In a session on Service Oriented Architecture, I argued earnestly that, due to the costs of SOA, support from the top was necessary for SOA to succeed within any organization.  Most of the other loud persons in the group insisted that newer tools such as WCF had lowered the cost of SOA sufficiently that a strong grass roots effort could drive SOA in an organization.  By the end of the session, I think we had all learned something and moved a little toward understanding the others' side.

I did attend a few traditional sessions in which a speaker stands in front of a classroom and delivers a lecture to an audience that is mostly passive.  Richard Campbell and Carl Franklin were two of the speakers at this conference and I have long been a fan of their .Net Rocks podcast, so I made a point to attend a lecture by each of them.  Both were good sessions but they were easily topped by Joe Wirtley who gave an excellent talk on WPF.  It was excellent because it focused on building a business application, rather than the eye candy that clutters so many WPF presentations.

Overall the conference was a great success.  It drained me of energy but it fired me up at the same time.

And I haven't even told you about the 28 hours I spent riding a bus with a few dozen techno-geeks.  Or the flat tire that left us stranded in Carrolton, KY for 3 hours at 2AM.  But that’s another story.

Note: Click here to see more photos from DevLink

Monday, August 25, 2008 10:49:27 PM (Eastern Standard Time, UTC-05:00)
 Friday, August 22, 2008

Microsoft recently released the ASP.Net Model View Controller framework (MVC).  It is currently available as Preview 3 and can be downloaded at http://www.microsoft.com/downloads/details.aspx?FamilyId=92F2A8F0-9243-4697-8F9A-FCF6BC9F66AB&displaylang=en

A new MVC project contains a couple sample views and controllers so you can get an idea of the proper syntax to use. 

In this article, we will add a new model, view and controller to an existing MVC project. 

1.       Open Visual Studio 2008 and create a new MVC project.  For information on how to create a new MVC project, see http://www.davidgiard.com/2008/08/18/TheASPNetMVCSampleAppDemystified.aspx

2.       Open the Solution Explorer (View | Solution Explorer) and select the Models folder.

3.       Add a Model to the project

a.       Right-click the Models folder and select Add | Class.  The Add New Item dialog displays.
Figure 1
    Figure 1

                                                               i.      At the Name textbox, enter “Customer”.

                                                             ii.      Click the OK button to create a Customer class.

b.      The Customer class opens in the class editor.  This class will contain a few public properties that help describe a customer object.  Add the following code to the Customer class.

    public class Customer

    {

        public int CustID { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string StreetAddress { get; set; }

        public string City { get; set; }

        public string State { get; set; }

        public string PostalCode { get; set; }

    }

4.       Add a Controller to the project

a.       In the Solution Explorer, right-click the Controllers folder and select Add | New Item.  The Add New Item dialog displays.
Figure 2
    Figure 2

                                                               i.      Under Categories, select Visual C#\Web\MVC.

                                                             ii.      Under Templates, select MVC Controller Class.

                                                            iii.      In the Name textbox, enter “CustomerController”.

                                                           iv.      Click the Add button to create the CustomerController class and open it in the class editor.

b.      In the CustomerController class, we will create some actions.  Each action will instantiate one or more Model objects and display them in a view object. 

                                                               i.      Add the following statement at the top of the CustomerController class. 

using TestMVC.Models;

                                                             ii.      Add the following private methods to the CustomerController class.  For now, we will create customers out of thin air (as if it were that easy).  In a real application, we would probably call a web server or query a database to get customers.

        #region private methods

        private Customer GetCustomer1()

        {