Post on 24-Jun-2020
transcript
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
1
Chair of Software Engineering
Last update: 2 November 2004
Trusted Components
Reuse, Contracts and Patterns
Prof. Dr. Bertrand MeyerDr. Karine Arnout
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
2
Chair of Software Engineering
Lecture 26: Component model:The .NET example
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
3
Chair of Software Engineering
Agenda for today
What is .NET?C# basicsEiffel for .NET
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
4
Chair of Software Engineering
What .NET is not?
NOT a programming language:“C# is not .NET and .NET is not C#”
NOT an operating system (yet).
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
5
Chair of Software Engineering
What .NET is…
An open-language platform for enterprise and Web development.
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
6
Chair of Software Engineering
The scope of .NET
.NET addresses almost everyone:The general public
New user-friendly servicesBusinesses
Help improve “B2B” relationships.Developers
SecurityProgramming-language interoperabilityComponent-based developmentVersioningMemory management
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
7
Chair of Software Engineering
The .NET architecture
UDDI, WSDL, PassportWeb services
XML & SOAPOpen interchange formats
ASP.NET, Windows Forms, Remoting…Frameworks & libraries
C#, Visual Basic.Net, Managed C++, Cobol, Eiffel for .NET…Specific language compilers
Common Language Specification (CLS)Language interoperability
Visual Studio.NetDevelopment environment
Common Language Runtime (CLR)Compilation, execution…
Hardware, Operating system, database systemUnderlying platform
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
8
Chair of Software Engineering
The Common Language Runtime
Basic set of mechanisms to execute .NET programs.Virtual machine based on internal code: MSIL.MSIL: not interpreted but “jitted” to native platform.Built-in security mechanisms.
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
9
Chair of Software Engineering
The .NET execution model
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
10
Chair of Software Engineering
.NET specific vocabulary
Managed code: Native code for the target platform, intended to run under the control and with the help of the CLR.
Ex: C#, Eiffel for .NET, Managed C++
Unmanaged code: Native code that doesn’t rely on the CLR.
Ex: “Classic” Eiffel, Unmanaged C++
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
11
Chair of Software Engineering
The assembly
Compiled form of a set of classesThe .NET unit of reuse (“.exe” or “.dll”)May be shared or private.
Notion of Global Assembly Cache Contain:
MSIL code (not binary code)Metadata (interface information)
Can be viewed with ILDasm.
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
12
Chair of Software Engineering
The Global Assembly Cache
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
13
Chair of Software Engineering
.NET core assembly (mscorlib.dll)
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
14
Chair of Software Engineering
.NET libraries (1/2)
Namespace What for?System Basic types: Int32, String …
System.Collections Collection types: ArrayList…
System.Data Relational database management
System.Net Network communications
System.Security Security features
System.Runtime.Remoting Distributed computing
System.Drawing GDI graphics
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
15
Chair of Software Engineering
.NET libraries (2/2)
Namespace What for?System.Text ASCII, Unicode, etc. encoding
System.Threading Thread support
System.Timers Raising time-controlled events
System.Text.RegularExpressions Regular expressions handling
System.Web.UI.WebControls Graphical Web controls
System.Windows.Forms Graphical controls: Button…
→ See the Reference documentation for more information.
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
16
Chair of Software Engineering
Towards new services…
A (demo) conference registration page
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
17
Chair of Software Engineering
Successful registration
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
18
Chair of Software Engineering
Passing erroneous data
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
19
Chair of Software Engineering
About this demo…
Some of what this demo illustrates:ASP.NET (Active Server Pages .NET)Server-side mechanisms, server controlsDebugging Web applications like traditional onesOur first C# example…Multi-language development (C#, Eiffel for .NET)
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
20
Chair of Software Engineering
The ASP.NET source
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
21
Chair of Software Engineering
Multi-language support
C++ C# Eiffel
Framework
...
Windows
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
22
Chair of Software Engineering
Languages on .NET (1/3)
“Microsoft languages”Visual Basic.NetC#Managed C++Jscript
Commercial languages (see next)Research languages (see next)
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
23
Chair of Software Engineering
Languages on .NET (2/3)
Commercial offerings from 3rd parties:Cobol (Fujitsu)Eiffel for .NET (Eiffel Software)Smalltalk (QKS)Java (JUMP program)Perl, Python (ActiveState)APL (Dyadic)Fortran (Fujitsu)Fortran (Salford Software)
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
24
Chair of Software Engineering
Languages on .NET (3/3)
Research languages:CAML (INRIA)Mercury (Melbourne Uni.)SchemeOberon (ETH, Zürich)Component Pascal (QUT)Haskell (Universities, Microsoft Research UK)
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
25
Chair of Software Engineering
Full interoperability
Cross-language…Inheritance
Classes can inherit from each other, regardless of language of origin.No need for wrappersNo IDL
Debugging sessions In Visual Studio.Net
Exceptions
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
26
Chair of Software Engineering
Full interoperability: the price to pay…
Must conform to the .NET object model (VOS, Virtual Object System):
The type systemObject-oriented principles (type, inheritance)
Too much for some (non-O-O languages)Too little for some: multiple inheritance (Eiffel)Difficult features: overloading
Must observe the Common Language Specification (CLS)
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
27
Chair of Software Engineering
Common Language Specification
Set of rules – more restrictive than the .NET object model – to determine compatibility.
Part of the ECMA standard
Three levels:Producer (“framework”)ConsumerExtender
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
28
Chair of Software Engineering
CLS rules example
(Overloading) “Methods and events that have the same name must be distinguished by at least one argument type.”
Producer: Must mark any offending methods/events as non-CLS-compliant.Consumer: Need not accept types that violate these rules, except if marked non-compliant.Extender: Need not provide syntax for defining types that violate this rule.
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
29
Chair of Software Engineering
The .NET type system
ReferenceValue
Built-in
Floatingtype
Typedref
Integertype
User-defined
Enums
Pointer
ManagedUnmanaged
Function
Interface
Type
Class
Structure-equivalent
Boxed enum
Array
Delegate
Name-equivalent
Boxed value type
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
30
Chair of Software Engineering
Value vs. reference types
Value types denote values (simple value or objects).
Reference types denote location of values.
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
31
Chair of Software Engineering
Simple value vs. object
A simple value cannotby itself identify its type.
Ex: int32, float 32
An object carries its own type description. Type info
Fields342
"A string"
342
An object
A simple value
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
32
Chair of Software Engineering
Value vs. reference types
Every value type has a corresponding reference type: its “boxed” version.
Some reference types have a corresponding value type: an “unboxed” version.
Limitation: Inheritance and interfaces apply only to reference types.
Type info342
Boxed
342Unboxed
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
33
Chair of Software Engineering
Built-in types
boolcharint8, int16, int32, int64unsigned int8, unsigned int16, unsigned int32, unsigned int64native int, native unsigned intfloat32, float64System.object, System.stringtypedref
*not in CLS
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
34
Chair of Software Engineering
Agenda for today
What is .NET?C# basicsEiffel for .NET
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
35
Chair of Software Engineering
The C# language
“Simplified version of C++”Benefits from the Java experienceThe native language of the .NET framework
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
36
Chair of Software Engineering
C# and Java
Object-oriented: classes, objects, dynamic allocation, inheritance, polymorphism, dynamic binding…Type-safe by default (no C++ casts)Support for overloadingSupport for garbage collectionNo multiple inheritance from classesNo genericity
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
37
Chair of Software Engineering
C# vs. Java
Coherent type systemClose integration with other languagesCompiler directives Every piece of software is in a classVariables initialized automaticallyEvent-delegate modelPropertiesException handling
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
38
Chair of Software Engineering
“Hello World!” in C#
using System;
public class Hello {static void Main() {
Console.WriteLine( “Hello World!” );}
}
Compiler command line: csc Hello.cs
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
39
Chair of Software Engineering
Classes
A class consists of members:FieldsMethodsPropertiesEvents
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
40
Chair of Software Engineering
Methods
Algorithms associated with the classWork on an instance (unless static).Instance calls or virtual callsA virtual method may be overridden in a descendant class.A virtual method may be marked final.
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
41
Chair of Software Engineering
Information hiding (1/2)
Access every object through the officialoperations of its interface.Possible member status:
PublicPrivateFamily: descendant types only AssemblyFamily and assemblyFamily or assembly
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
42
Chair of Software Engineering
Information hiding (2/2)
!Direct field modification (e.g. C++, Java):heater.temperature := 19
Proper O-O technique:set_temperature (t: INTEGER) is
doif not in_range (t) then
-- Error …else
temperature := tnotify_observers
endend
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
43
Chair of Software Engineering
The .NET solution: Properties
public class Heater {
private int TemperatureInternal;
public int Temperature {
get {return TemperatureInternal;}
set {if (! InRange (value)) {
throw new ArgumentException(“Temperature out of range");
}TemperatureInternal = value;NotifyObservers;
}}
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
44
Chair of Software Engineering
Events and Delegates
Purpose: Event-driven programming
A delegate is an object representing a method of a particular class (like function pointer, but typed).
Basic methods on delegates: InvokeBeginInvokeEndInvoke
Associate one or more delegates with each event.
No way to specify “closed” arguments
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
45
Chair of Software Engineering
Setting an event handler
Button ClickMe = new Button ();ClickMe.Text = "Click me!";
ClickMe.Location = new System.Drawing.Point (200, 30);ClickMe.Size = new System.Drawing.Size (30, 10);
EventHandler MyHandler = new EventHandler (this.ButtonClicked);ClickMe.Click += MyHandler;
void ButtonClicked (Object sender, EventArgs args) {…}
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
46
Chair of Software Engineering
The event definition
public class Button{
public event EventHandler Click;
protected void OnClick (EventArgs e) {if (Click != null)
Click (this, e);}
}
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
47
Chair of Software Engineering
Exception handling
Similar to the Java model:try {
// code that may cause an exception}catch (ExceptionType exc) {
if CanFix() {FixIt();
}else throw new Exception();
}catch (OtherExceptionType exc) {…}
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
48
Chair of Software Engineering
Agenda for today
What is .NET?C# basicsEiffel for .NET
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
49
Chair of Software Engineering
Eiffel for .NET
Full implementation of Eiffel on .NET:Design by ContractSeamlessness of software developmentMultiple inheritanceGenericity…
Large set of libraries:Eiffel libraries + .NET libraries
Directly available in EiffelStudio
Integrated into Visual Studio.NET: Eiffel ENViSioN!™Smart editorProject clusters browsing…
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
50
Chair of Software Engineering
Design by Contract on .NET
Like Eiffel, Eiffel for .NET directly enforces Design by Contract, which addresses:
CorrectnessDocumentationDebugging and testingInheritance controlManagement
Eiffel for .NET is the sole language on .NET to support contracts.
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
51
Chair of Software Engineering
Seamlessness of software development
Like Eiffel, Eiffel for .NET covers the entire software lifecycle.
EiffelStudio provides complete reversibilitybetween class text and diagrams.
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
52
Chair of Software Engineering
ISE EiffelStudio
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
53
Chair of Software Engineering
Multiple inheritance and genericity…
… Or how to map non-CLS compliantmechanisms on .NET while preserving languageinteroperability?
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
54
Chair of Software Engineering
Multiple inheritance on .NET (1/2)
How Eiffel for .NET does it: Taking full advantage of interfaces and namespaces.
A
Impl.A
B
Impl.B
C
Impl.C
Shadowing classes with interfaces.
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
55
Chair of Software Engineering
Multiple inheritance on .NET (2/2)
What about class instantiation?
A
Impl.A
B
Impl.B
C
Impl.C
Create.A
Create.C
Create.B
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
56
Chair of Software Engineering
Genericity and covariance on .NET
Eiffel for .NET:Supports genericity.Implements a safe variant of covariance (with type checking) detecting catcalls at run-time.
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
57
Chair of Software Engineering
“Hello World!” with Eiffel for .NET
class
HELLO_WORLD
create
make
feature {NONE} -- Initialization
make is-- Print Hello_world_message.
doio.put_string (Hello_world_message)
end
feature -- Constant
Hello_world_message: STRING is “Hello World!”-- Hello World! message
end
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
58
Chair of Software Engineering
Full interoperability on .NET (1/4)
With C#:
public class HelloWorld1{
public static void Main() {HelloWorld sample;sample = Create.HelloWorld.Make();
}}
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
59
Chair of Software Engineering
Full interoperability on .NET (2/4)
With VB.NET:
Public Class HelloWorld1
Public Sub Main ()Dim sample as HelloWorldsample = Create.HelloWorld.Make()
End Sub
End Class
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
60
Chair of Software Engineering
Full interoperability on .NET (3/4)
With C#:
using System;
public class HelloWorld2: Impl.HelloWorld{
public static void Main() {HelloWorld sample;sample = new Impl.HelloWorld();Console.WriteLine (
sample.HelloWorldMessage());}
}
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
61
Chair of Software Engineering
Full interoperability on .NET (4/4)
With VB.NET:
Imports System
Public Class HelloWorld2Inherits Impl.HelloWorld
Public Shared Sub Main() Dim h2 as HelloWorld2h2 = new HelloWorld2()Console.WriteLine (h2.HelloWorldMessage())
End SubEnd Class
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
62
Chair of Software Engineering
Eiffel for .NET in Visual Studio.NET
EiffelStudio and Visual Studio.NET, two complementary development environments:
EiffelStudio for possibly multi-platformdevelopment in Eiffel only.
Visual Studio.NET for multi-languagedevelopment.
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
63
Chair of Software Engineering
Windows Forms and Eiffel for .NET
… Or how to take advantage from .NET libraries from an Eiffel for .NET project.
Demo
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
64
Chair of Software Engineering
Web services with Eiffel for .NET
It is possible to write ASP.NET pages directly in Eiffel for .NET.
<%@ Page Language = “Eiffel” %>
The registration page demo with Eiffel for .NET
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
65
Chair of Software Engineering
References
Karine Arnout: Introduction to Eiffel for .NET. CoDe Magazine, Sept/Oct 2002.
Bertrand Meyer: The .NET Training Course. Prentice Hall, 2001.
Bertrand Meyer, Raphaël Simon, Emmanuel Stapf: Instant .NET. Prentice Hall, 2002 (in preparation).
Microsoft: .NET Reference documentation. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/cpref_start.asp.
Raphaël Simon, Emmanuel Stapf, Bertrand Meyer: Full Eiffel on .NET. MSDN, 2002.
Trusted Components: Reuse, Contracts and Patterns - Lecture 26
66
Chair of Software Engineering
End of lecture 26