Upload
maurice-briggs
View
213
Download
0
Embed Size (px)
Citation preview
Lecture 5:
Component-based Programming
2MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-2
Objectives
“Components are another term for classes, DLLs, and assemblies. Component-based programming is the logical tendency to design our programs as a set of components instead of a large monolithic app. Given the design of .NET and its Framework Class Library, J# programming is inherently component-based programming... ”
In 3 Parts:
1. Component-based programming
2. Working with .NET components
3. Creating your own .NET components
3MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-3
Part 1
• Component-based programming…
4MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-4
Component-based programming
• OO programming is component-based programming– classes are components
• .NET programming is component-based programming– DLLs are components
• Other components you'll use:
– Supplemental UI Library
5MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-5
.NET components
• .NET components must be installed to run a .NET program– in fact, CLR + FxCL must be installed– these are known collectively as the .NET Framework
HardwareHardware
Operating SystemOperating System
Common Language Runtime (CLR)Common Language Runtime (CLR)
.NET program.NET program.NET Framework.NET Framework
Class Library (FxCL)Class Library (FxCL).NETFramework
6MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-6
Installing .NET Framework
• .NET Framework is installed as part of Visual Studio .NET
• How to install on systems without VS .NET?• Pre-installed on Windows 2003 and above• Otherwise you must install 2 pieces of software, in this order:
1. .NET Framework 1.1 Redistributable: dotnetfx.exe (20MB)
2. Visual J# .NET Version 1.1 Redistributable: vjredist.exe (7MB)
• Requirements:
– Windows 98 or above
7MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-7
Installing .NET Framework (cont'd)
• Installing .NET Framework 1.1 Redistributable:
– http://msdn.microsoft.com/netframework/technologyinfo/howtoget/
– also available via Windows Update (IE, Tools menu, recommended)
• Installing Visual J# .NET Version 1.1 Redistributable:
– http://msdn.microsoft.com/vjsharp/downloads/howtoget/default.aspx
• Other components are installed on an "as-needed" basisSupplemental UI Library:
– see http://msdn.microsoft.com/vjsharp/supui/
8MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-8
Part 2
• Working with .NET components…
9MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-9
Programming options
• J# programming can really be done at two levels:
1. using Visual Studio .NET
2. at the command-line, much like Sun's JDK
• Let's use the command-line tools since it's easier to understand how .NET component work…
10MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-10
Command-line development
• Main tools:– vjc: Visual J# compiler– notepad: simple Windows text editor (any editor will
do)
• How to get started?
– open Visual Studio .NET 2003 Command Prompt
• Start menu, • All Programs, • Microsoft Visual Studio .NET 2003, • Visual Studio .NET Tools,• Visual Studio .NET 2003
Command Prompt
11MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-11
Example
• Let's create a simple GUI app using the Java Class Library– in particular, using the SWING classes of the JCL
• GUIApp:– outputs "ouch!" to the console each time you click the button…
C:\GUIApp> guiapp.exeouch!ouch!ouch!
12MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-12
GUIApp
• GUIApp is a single source code file "GUIApp.jsl":
public class GUIApp{ public static void main(String[] args) { javax.swing.JFrame frame = new javax.swing.JFrame(); java.awt.Container c = frame.getContentPane(); javax.swing.JButton button1 = new javax.swing.JButton("Click Me");
frame.setSize(100, 100); frame.setLocation(100, 100); frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); c.add(button1); button1.addActionListener(new ButtonHandler());
frame.show(); }}//class
class ButtonHandler implements java.awt.event.ActionListener{ public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("ouch!"); }}//class
public class GUIApp{ public static void main(String[] args) { javax.swing.JFrame frame = new javax.swing.JFrame(); java.awt.Container c = frame.getContentPane(); javax.swing.JButton button1 = new javax.swing.JButton("Click Me");
frame.setSize(100, 100); frame.setLocation(100, 100); frame.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); c.add(button1); button1.addActionListener(new ButtonHandler());
frame.show(); }}//class
class ButtonHandler implements java.awt.event.ActionListener{ public void actionPerformed(java.awt.event.ActionEvent e) { System.out.println("ouch!"); }}//class
13MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-13
Compilation
• Compile using the Visual J# compiler "vjc"– reference Supplemental UI Library to use SWING
– compiler switches: /t: build a console-based .EXE /r: reference the specified .NET component (.DLL)
C:\GUIApp> dir04/20/2004 673 GUIApp.jsl
C:\GUIApp> vjc /t:exe GUIApp.jsl /r:vjssupuilib.dllMicrosoft (R) Visual J# .NET Compiler version 7.10.3077.0For Microsoft (R) .NET Framework version 1.1.4322Copyright...
C:\GUIApp> dir04/20/2004 20,480 GUIApp.exe
14MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-14
Run!
• Run the .EXE from the command-line:
C:\GUIApp> guiapp.exeouch!ouch!ouch!
15MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-15
.NET Framework SDK
• .NET Framework SDK contains the command-line tools
– SDK = Software Development Kit
– .NET equivalent of Sun's JDK
– installed as part of Visual Studio .NET
– available separately as a free download (100MB):• http://msdn.microsoft.com/netframework/technologyinfo/howtoget/
16MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-16
Issues with using Swing
• Advantage of using Swing:
You can recompile without modification usings Sun’s JDK and run on any Java platform.
• Disadvantage of using Swing:
Swing programming by hand is hard and tedious and does not let you take full advantage of the Visual Studio integrated IDE.
17MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-17
Example #2
• A better example of SWING– Visual Sorting demo…
public VisualSorting(){ super("VisualSorting"); URL u = VisualSorting.class.getResource("Random.jpg"); icon= new ImageIcon(u);
// create AlgorithmPanels, and the mainControlPanel populateAlgorithmsPane(); mainControlPanel = new MainControlPanel (this);
// add the algorithmPanel, and mainControlPanel to the contentPanel getContentPane().setLayout (new GridBagLayout()); GridBagConstraints gConstraints = new GridBagConstraints(); gConstraints.anchor = GridBagConstraints.CENTER; gConstraints.fill = GridBagConstraints.BOTH; gConstraints.gridx = GridBagConstraints.RELATIVE; gConstraints.gridy = GridBagConstraints.RELATIVE; gConstraints.gridwidth = GridBagConstraints.REMAINDER; gConstraints.gridheight = 650; . . .
public VisualSorting(){ super("VisualSorting"); URL u = VisualSorting.class.getResource("Random.jpg"); icon= new ImageIcon(u);
// create AlgorithmPanels, and the mainControlPanel populateAlgorithmsPane(); mainControlPanel = new MainControlPanel (this);
// add the algorithmPanel, and mainControlPanel to the contentPanel getContentPane().setLayout (new GridBagLayout()); GridBagConstraints gConstraints = new GridBagConstraints(); gConstraints.anchor = GridBagConstraints.CENTER; gConstraints.fill = GridBagConstraints.BOTH; gConstraints.gridx = GridBagConstraints.RELATIVE; gConstraints.gridy = GridBagConstraints.RELATIVE; gConstraints.gridwidth = GridBagConstraints.REMAINDER; gConstraints.gridheight = 650; . . .
public static void main(String args[]){ VisualSorting sd = new VisualSorting(); sd.setSize((int) (520 * 1.61), 520); sd.setResizable(false); sd.reset(); sd.show();}
public static void main(String args[]){ VisualSorting sd = new VisualSorting(); sd.setSize((int) (520 * 1.61), 520); sd.setResizable(false); sd.reset(); sd.show();}
18MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-18
Part 3
• Creating your own .NET components…
19MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-19
Motivation
• Professionals create their own components for many reasons:
– Team programming: each builds their own component and tests separately.
– Multi-language programming: components in different languages are built (e.g. VB, J#, C++) and then integrated.
– Component reuse– Component updating: Components can be updated
independently to fix bugs
20MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-20
Example: N-Tier design
• Business applications commonly follow an N-Tier design– application is logically divided by task areas– physically built as a set of components
Business.dll
Presentation
Tier
Data AccessTier
e.g. read and write from
customer files or database
DataTier
e.g. data source, like
files or database
Business LogicTier
e.g. purchase products or pay
employees
GUI.exe DataAccess.dll
21MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-21
Component-based BankingApp
• Let's rebuild the BankingApp as a set of components:
transactions.txt
customers.txt
App.jslApp.jslCustomer.jslCustomer.jsl
CustomersIO.jslCustomersIO.jslTransactions.jslTransactions.jsl
Transactions.dllBankingApp.exe Customers.dll
22MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-22
(1) Customers.dll
• First, build the lower-level data access tier, Customers.dll:– in this case we target (/t:) a library, not an exe
– Rather than /t:library we can abbreviate to /t:l
C:\BankingApp> dir *.jsl04/20/2004 1,039 App.jsl04/20/2004 743 Customer.jsl04/20/2004 1,603 CustomersIO.jsl04/20/2004 1,443 Transactions.jsl
C:\BankingApp> vjc /t:library Customer.jsl CustomersIO.jsl /out:Customers.dllMicrosoft (R) Visual J# .NET Compiler version 7.10.3077.0For Microsoft (R) .NET Framework version 1.1.4322Copyright...
C:\BankingApp> dir *.dll04/20/2004 20,480 Customers.dll
23MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-23
(2) Transactions.dll
• Second, build the middle-level business tier, Transactions.dll:– reference Customers.dll to gain access to definition of Customer
C:\BankingApp> dir *.jsl04/20/2004 1,039 App.jsl04/20/2004 743 Customer.jsl04/20/2004 1,603 CustomersIO.jsl04/20/2004 1,443 Transactions.jsl
C:\BankingApp> vjc /t:library Transactions.jsl /r:Customers.dll /out:Trans actions.dllMicrosoft (R) Visual J# .NET Compiler version 7.10.3077.0For Microsoft (R) .NET Framework version 1.1.4322Copyright...
C:\BankingApp> dir *.dll04/20/2004 20,480 Customers.dll04/20/2004 20.480 Transactions.dll
24MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-24
(3) BankingApp.exe
• Third, build the presentation tier .EXE, BankingApp.exe:– references both Customers.dll and Transactions.dll
C:\BankingApp> dir *.jsl04/20/2004 1,039 App.jsl04/20/2004 743 Customer.jsl04/20/2004 1,603 CustomersIO.jsl04/20/2004 1,443 Transactions.jsl
C:\BankingApp> vjc /t:exe App.jsl /r:Customers.dll /r:Transactions.dll /out:Ba nkingApp.exeMicrosoft (R) Visual J# .NET Compiler version 7.10.3077.0For Microsoft (R) .NET Framework version 1.1.4322Copyright...
C:\BankingApp> dir *.dll;*.exe04/20/2004 20,480 Customers.dll04/20/2004 20.480 Transactions.dll04/20/2004 20,480 BankingApp.exe
25MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-25
Run!
• BankingApp runs exactly as before…
26MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-26
If .NET can't find component?
• If a referenced .DLL cannot be found, program fails to run…• Example:
– move Customers.dll out of folder, and try running app…
move to desktop…
27MicrosoftIntroducing CS using .NETJ# in Visual Studio .NET 5-27
Summary
• .NET is built using component-based programming• J# programming is component-based programming• Professional programming is component-based programming