1. Architecture Lab

 

 

Overview:  You'll work with the command-line tools of .NET to create and explore console-based applications.  First we'll create a monolithic application, then a component-based one.


To earn a passing grade, have the instructor check off on any items marked CHECKPOINT.

Part 1:  Monolithic Application

1.      First, open a command window that has the path variables to the .NET Framework SDK set appropriately.  The best way is via

·        Start

·        All Programs

·        Microsoft Visual Studio .NET 2003

·        Visual Studio .NET Tools

·        Visual Studio .NET 2003 Command Prompt

If you haven't already, set the properties of the command window so that it's easy to read: 

·        Single-click top-left corner

·        Select Properties to display a tabbed dialog

·        Set the font

·        Set the colors

·        Click OK and apply changes to shortcut that created the window

2.      Change the drive to H:

3.      Change directory (cd) to the subdirectory you created for this class.

4.      In another window, use your favorite editor (I recommend TextPad) to create a text file called "main.cs".  Write a C# program that outputs a simple message to the console. 

5.      (CHECKPOINT) Compile main.cs with csc, run and test.  Recall that you can leave TextPad open and use ALT-Tab to switch back and forth between the source code and command windows.  Also recall that in the command window, the up-arrow and down-arrow keys can be used to repeat previous commands.

csc t:/exe /out:app.exe main.cs

6.      Let's write a little OOP-based code.  Create a Pet class with 2 string fields, one for their first name and one for their breed.  Implement a ToString() method that returns the pet's name in breed in the format "name is a breed"; use the "+" operator for string concatenation.  Modify the main program to read from the keyboard the pet's name and breed.  Use System.Console.Write("...") for prompting, and System.Console.ReadLine() for inputting.  Note that ReadLine() returns the entire line of input as a single string, so you'll want to input the name and breed individually. 

7.      (CHECKPOINT) Once you have input the first and last names from the user, the main program should now create a new Pet object, store the input in the object's fields, and then output the object to the console using System.Console.WriteLine() and the object's ToString() method.  Compile the main and pet files together using the csc command.

 csc t:/exe /out:app.exe main.cs pet.cs

Once it compiles cleanly, run and test.

8.      (CHECKPOINT) Once that is working, modify the main program to create 3 different Pet objects, output a blank line, and then output the 3 pets. 

9.      Run the .NET disassembler tool ILDasm on your application, "ildasm app.exe", and browse the compiled assembly.  Open the manifest, and then open each of the types (classes) in your program (there should be two, one for Pet and one for the class containing your main program).


Part 2:  Component-based Application

1.      Without changing the source code, rebuild the application as a component-based one. First, compile the Pet class as a library assembly.  Then rebuild the main program properly in the presence of the DLL. 

csc /t:library /out:pet.dll pet.cs

csc /t:exe /out:app.exe main.cs /r:pet.dll

2.      (CHECKPOINT)  Run and test, and convince yourself that the application performs exactly as before. 

3.      Of course, internally the application has been built quite differently than before.  To convince yourself of the difference, reopen the application file using ILDasm ("ildasm app.exe").  First, notice that the manifest now contains an external reference to the Pet assembly.  Second, notice that the Pet class is now missing from app.exe. 

4.      Rename the Pet assembly to something like "pet.xyz".  Now try to run the application, it should fail with an exception (answer No when asked about JIT debugging).  The CLR is reporting that is was unable to find the required Pet assembly.  Rename the assembly back to "pet.dll", and the application should work as before.

5.      One of the advantages of component-based development is that components can be updated independently of each other.  Modify the Pet class by changing what the ToString() method returns, for example by returning the pet's name with the phrase " and is happy" appended to the end.  Recompile only the Pet assembly and rerun the application.  The application should now behave differently, given the modification you made to the Pet class. 

6.      (CHECKPOINT) Add a method to the Pet class which returns the length of the pet's name.  Then have the main program output the length as well as each pet's name.  The length of a string is easily computed through a member of the string class.  To access Help,

·        Open the Microsoft Visual Studio .NET Documentation (available via Start menu, Programs, Microsoft Visual Studio .NET)

·        Switch to the Index tab

·        "Look for" the String class

·        You can narrow the search results if you "Filter by" the .NET Framework. 

That's it, good work!