|
1.
Architecture Lab |
|
Overview: You'll work with the
command-line tools of .NET to create and explore console-based
applications. First you'll create a monolithic application, then a
component-based one.
To earn a passing grade
for this lab, you must demonstrate both working programs to the instructor no
later than May 17.
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 2005
·
Visual
Studio Tools
·
Visual
Studio 2005 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 the current 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. The
message should include your name.
5.
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.
Next,
write a little OOP-based code. In a separate file, employee.cs,
create an Employee class with 3 string fields.
The strings should hold the first and last names of the employee and his
or her email address. Implement an appropriate constructor.
|
Employee |
|
- firstName
: string - lastName
: string - email : string |
|
+ Employee ( string,
string, string ) : void + ToString
( ) : string |
7.
Implement
a ToString()
method that returns the employee's first and last names in the format Lastname, Firstname. Use the "+" operator for string
concatenation. I suggest that you get this working first.
8.
Once
the constructor and ToString() methods are working, modify the main program to read from
the keyboard the employee’s names and email address. 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 fields individually.
9.
Once
the user has input the names and email, the main program should instantiate a
new Employee object, storing the input in the object's fields. The main program
should then output the object to the console using System.Console.WriteLine()
and the object's ToString() method. Compile the
main and employee files together using the csc
command.
csc /t:exe /out:app.exe main.cs
employee.cs
Once it compiles cleanly, run and
test.
10.
Once
that is working, modify the main program to input and create 3 different
Employee objects, output a blank line, and then output the 3 employees.
11.
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 Employee and
one for the class containing your main program).
1.
DEMONSTRATE THIS SOLUTION NOW OR SAVE A
COPY OF THIS VERSION AND DEMONSTRATE IT LATER
Part 2:
Component-based Application
1.
Before
you continue, be sure to make a copy of the solution for Part 1 so that you can
demonstrate it later.
2.
Without
changing the source code, rebuild the application as a component-based
one. First, compile the Employee class as a library assembly. Then rebuild the main program properly in the
presence of the DLL.
csc /t:library
/out:employee.dll employee.cs
csc /t:exe /out:app.exe
main.cs /r:employee.dll
3.
Run
and test, and convince yourself that the application performs exactly as
before.
4.
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 Employee assembly. Second, notice that the Employee
class is now missing from app.exe.
5.
Rename
the Employee assembly to something like "employee.xyz".
Now try to run the application, it should fail with an exception (answer No
when asked about JIT debugging or if asked to Send an error report). The
CLR is reporting that is was unable to find the required Employee
assembly. Rename the assembly back to "employee.dll", and the
application should work as before.
6.
One
of the advantages of component-based development is that components can be
updated independently of each other. Modify the Employee class by
changing what the ToString() method returns, for example by returning the employee’s
name and email address with the phrase in a phrase such as “First name”’s email address is “email”. Recompile only
the Employee assembly and rerun the application. The application should
now behave differently, given the modification you made to the Employee
class.
7.
DEMONSTRATE YOUR SOLUTION AND EXPLAIN WHAT YOU HAVE
JUST DONE
That's it, good work!