slides

basiliskcanoeSoftware and s/w Development

Nov 2, 2013 (3 years and 9 months ago)

70 views



Lecture 5:


Component
-
based Programming

2

Microsoft

Introducing CS using .NET

J# 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


3

Microsoft

Introducing CS using .NET

J# in Visual Studio .NET

5
-
3



Part 1


Component
-
based programming…

4

Microsoft

Introducing CS using .NET

J# 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

5

Microsoft

Introducing CS using .NET

J# 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


Hardware

Operating System

Common Language Runtime (CLR)

.NET program

.NET Framework

Class Library (FxCL)

.NET

Framework

6

Microsoft

Introducing CS using .NET

J# 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

7

Microsoft

Introducing CS using .NET

J# 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" basis

Supplemental UI Library:


see
http://msdn.microsoft.com/vjsharp/supui/


8

Microsoft

Introducing CS using .NET

J# in Visual Studio .NET

5
-
8



Part 2


Working with .NET components…

9

Microsoft

Introducing CS using .NET

J# 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…

10

Microsoft

Introducing CS using .NET

J# 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

11

Microsoft

Introducing CS using .NET

J# 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.exe

ouch!

ouch!

ouch!


12

Microsoft

Introducing CS using .NET

J# 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

13

Microsoft

Introducing CS using .NET

J# 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>
dir

04/20/2004

673

GUIApp.jsl


C:
\
GUIApp>
vjc /t:exe GUIApp.jsl /r:vjssupuilib.dll

Microsoft (R) Visual J# .NET Compiler version 7.10.3077.0

For Microsoft (R) .NET Framework version 1.1.4322

Copyright...


C:
\
GUIApp>
dir

04/20/2004

20,480

GUIApp.exe


14

Microsoft

Introducing CS using .NET

J# in Visual Studio .NET

5
-
14



Run!


Run the .EXE from the command
-
line:

C:
\
GUIApp>
guiapp.exe

ouch!

ouch!

ouch!


15

Microsoft

Introducing CS using .NET

J# 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/



16

Microsoft

Introducing CS using .NET

J# 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.


17

Microsoft

Introducing CS using .NET

J# 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 static void
main
(String args[])

{


VisualSorting sd = new
VisualSorting
();


sd.setSize((int) (520 * 1.61), 520);


sd.setResizable(false);


sd.reset();


sd.show();

}

18

Microsoft

Introducing CS using .NET

J# in Visual Studio .NET

5
-
18



Part 3


Creating your own .NET components…

19

Microsoft

Introducing CS using .NET

J# 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



20

Microsoft

Introducing CS using .NET

J# 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 Access

Tier

e.g. read and
write from
customer files or
database

Data

Tier

e.g. data
source, like
files or
database

Business Logic

Tier

e.g. purchase
products or pay
employees

GUI.exe

DataAccess.dll

21

Microsoft

Introducing CS using .NET

J# in Visual Studio .NET

5
-
21



Component
-
based BankingApp


Let's rebuild the BankingApp as a set of components:

transactions.txt

customers.txt

App.jsl

Customer.jsl

CustomersIO.jsl

Transactions.jsl

Transactions.dll

BankingApp.exe

Customers.dll

22

Microsoft

Introducing CS using .NET

J# 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 *.jsl

04/20/2004

1,039

App.jsl

04/20/2004


743

Customer.jsl

04/20/2004

1,603

CustomersIO.jsl

04/20/2004

1,443

Transactions.jsl


C:
\
BankingApp>
vjc /t:library Customer.jsl CustomersIO.jsl /out:Custo

mers.dll

Microsoft (R) Visual J# .NET Compiler version 7.10.3077.0

For Microsoft (R) .NET Framework version 1.1.4322

Copyright...


C:
\
BankingApp>
dir *.dll

04/20/2004

20,480

Customers.dll


23

Microsoft

Introducing CS using .NET

J# 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 *.jsl

04/20/2004

1,039

App.jsl

04/20/2004


743

Customer.jsl

04/20/2004

1,603

CustomersIO.jsl

04/20/2004

1,443

Transactions.jsl


C:
\
BankingApp>
vjc /t:library Transactions.jsl /r:Customers.dll /out:Trans
actions.dll

Microsoft (R) Visual J# .NET Compiler version 7.10.3077.0

For Microsoft (R) .NET Framework version 1.1.4322

Copyright...


C:
\
BankingApp>
dir *.dll

04/20/2004

20,480

Customers.dll

04/20/2004

20.480

Transactions.dll


24

Microsoft

Introducing CS using .NET

J# 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 *.jsl

04/20/2004

1,039

App.jsl

04/20/2004


743

Customer.jsl

04/20/2004

1,603

CustomersIO.jsl

04/20/2004

1,443

Transactions.jsl


C:
\
BankingApp>
vjc /t:exe App.jsl /r:Customers.dll /r:Transactions.dll /out:Ba
nkingApp.exe

Microsoft (R) Visual J# .NET Compiler version 7.10.3077.0

For Microsoft (R) .NET Framework version 1.1.4322

Copyright...


C:
\
BankingApp>
dir *.dll;*.exe

04/20/2004

20,480

Customers.dll

04/20/2004

20.480

Transactions.dll

04/20/2004

20,480

BankingApp.exe


25

Microsoft

Introducing CS using .NET

J# in Visual Studio .NET

5
-
25



Run!


BankingApp runs exactly as before…

26

Microsoft

Introducing CS using .NET

J# 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…

27

Microsoft

Introducing CS using .NET

J# 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