• Non ci sono risultati.

13.1 Overview of the .NET Framework

N/A
N/A
Protected

Academic year: 2021

Condividi "13.1 Overview of the .NET Framework"

Copied!
22
0
0

Testo completo

(1)

13.1 Overview of the .NET Framework

- A component is an encapsulation of software that can stand by itself and be used by other components

- .NET is based in in part its predecessor, COM - .NET Framework is a collection of technologies for the development and deployment of .NET software systems

- The .NET Common Language Runtime (CLR) - Assembly integrity checking – using a hash code

- JIT compilation

- Garbage collection – can be forced - Exception handling

- .NET languages from Microsoft:

VB .NET

Managed C++ .NET JScript .NET

J# .NET C#

+ many more from other sources

(2)

13.1 Overview of the .NET Framework

(continued)

- .NET languages from Microsoft:

VB .NET

Managed C++ .NET JScript .NET

J# .NET C#

- There are now >20 .NET languages, including COBOL, Fortran, Perl, and Python

- Every .NET language has a compiler that

produces IL, which is JIT compiled to machine code

- There is no IL interpreter

- Advantage of multi-language systems:

- Can use old components - Easy transition to .NET

- Disadvantage of multi-language systems:

- Maintenance is difficult

(3)

13.1 Overview of the .NET Framework

(continued)

- Common Language Infrastructure (CLI) - Common Type System (CTS)

- Minimum type set for .NET

- All .NET languages must support them

- e.g., Int32, which corresponds to int in C#

- All CTS types are derived from System.object

- Two categories of data types: value and reference

- Common Language System (CLS)

- Minimum language constructs and rules - e.g., no operator overloading, no pointers, identifiers are not case sensitive, etc.

- Framework Class Libraries (FCL) - > 4000 classes

(4)

13.1 Overview of the .NET Framework

(continued)

- Aim of CLI and CLR: interoperability

- A component in any .NET language can:

- Inherit from any other .NET language class - Call the methods of any other .NET language class

- Subclass any class from any .NET language

(5)

13.2 Introduction to C#

- C# heritage:

- From Java:

- Single inheritance - Interfaces

- Garbage collection

- No global types or variables - Level of coercion

- From C++:

- Pointers

- Operator overloading - Preprocessor

- structs, enums, … - From Delphi and VB:

- Properties

- From J# (actually, J++):

- Delegates

(6)

13.2 Introduction to C#

(continued)

- C# heritage (continued):

- New Features:

- Indexes - Attributes - Events

- A safer switch statement - A new kind of struct

- Primitive Types and Expressions

- Similar to Java, except C# has unsigned integers, a 16-byte decimal type, and pointers

- Data Structures

- Similar to Java and C++: class library support for Array, ArrayList, Queue, and Stack

- Array class has many methods and a Length

property

- An enumeration type, similar to that of C++, except no coercions to or from other types

(7)

13.2 Introduction to C#

(continued) - Control Statements

- Like Java, except:

1. There is a goto,

2. There is a foreach statement

foreach (int myInt in myIntArray) { … }

3. The switch has a static semantics rule that requires each selectable segment to end in an unconditional transfer (either break or goto) case 0:

Zeros++;

goto case 1;

case 1:

- Classes, Methods, and Structures - Like Java, except:

1. Parameters can be passed by value (default), passed by reference, or passed by result

- Pass by reference - ref

- Pass by result - out

(8)

13.2 Introduction to C#

(continued)

2. A method that can be overriden must be marked virtual

A method that overrides must be marked override

A method that has the same protocol as an inherited method but is NOT to override it is marked new

3. A method can take a variable number of parameters, if they are the same type

void SumInts(params int[] intValues) { … } int [] myIntArray = new int[6] {2, 4, 6, 8, 10, 12};

...

sum1 = SumInts(myArray);

sum2 = SumInts(10, i, 17, k);

4. A C# struct is a lightweight class

- Supports constructors and can implement interfaces

- Does not support inheritance or subclasses - Is allocated from the stack

(9)

13.2 Introduction to C#

(continued) - Properties

A property is a special data field of a class that can provide implicitly called get and set accessors

public class Weather {

public int DegreeDays { get {

return degreeDays;

}

set {

degreeDays = value;

}

private int degreeDays;

} //** end of property DegreeDays ...

} //** end of class Weather ...

Weather w = new Weather();

.. w.degreeDays += degreeDaysToday;

- Delegates – object-oriented method pointers public delegate void AHandler (

object o, System.EventArgs e);

AHandler can reference any method with this protocol

(10)

13.2 Introduction to C#

(continued) - Program Structure

- System is a namespace of FCL that provides input & output, string manipulation, threading, and collections

- Abbreviations of elements of System are premitted by:

using System;

Complete program example:

using System;

public class myTester {

public static void Main() { Console.WriteLine(″Howdy!″);

} }

- File Storage for Programs

- A file can store any number of public classes and/or interfaces

- Every class can define a Main method, but if there are more than one in a file, you must tell the system where to begin

(11)

13.3 Introduction to ASP.NET

- The Basics of ASP.NET

- Based on ASP, but revolutionarily different

- ASP documents could have embedded scripts in either Jscript or VB – both purely interpreted - Disadvantages:

1. Inefficient

2. Mixing script and markup is confusing 3. Scripting languages are unreliable

- ASP.NET differs from JSP in two ways:

1. Any .NET language can be used 2. All ASP.NET code is compiled

- Code can be embedded in ASP.NET documents, or can be separate in a code-behind file

- Every ASP.NET document is compiled into a class

- Base class is System.Web.UI.Page, unless there

(12)

13.3 Introduction to ASP.NET

(continued) - ASP.NET documents

- Can include:

1. XHTML markup

2. Directives – appear in <% … %> blocks 3. Render blocks <% … %>

- No method definitions

- Put into a function in the document class 4. Declaration blocks

- Script elements - method definitions 5. Server-side comments <%-- … --%>

- The only directive covered here is @Page

- The only necessary attribute is Language  SHOW ex1.aspx

- Code-behind Files

- The @Page directive must specify the code-behind file in a Inherits attribute

- If you want the code-behind file implicitly compiled, include a Src attribute

 SHOW ex2.aspx and ex2.aspx.cs

(13)

13.4 ASP.NET Controls

- Two collections of server controls:

HTML controls and Web controls - HTML Controls

- One-to-one correspondence with the XHTML elements

HtmlInputButton - <input type = ″submit″ …/>

HtmlInputRadioButton - <input type = ″radio″

- Difference between XHTML elements and their corresponding HTML controls:

- Server-side code can interact with HTML controls

- Many HTML controls can raise events - ServerClick and ServerChange

- Any element that will be used by server-side code must include the runat = ″server″ attribute

- A form that has server-side elements must also include the runat = ″server″ attribute

- Some HTML controls are not form elements

(14)

13.4 ASP.NET Controls

(continued) <form runat = ″server″>

<input type = ″text″ id = ″address″

runat = ″server″ />

</form>

- The form has no action attribute

- The server-side code (the document class) would have:

protected HtmlInputText address;

- All HTML controls are converted to objects in the document class

- An ASP.NET document with a form has two purposes:

1. Describe the form to be displayed by the browser

2. Process the form when its data is submitted - Each of these has its own kind of request – initial and postback

 SHOW ex3.aspx (after the form is filled)

(15)

13.4 ASP.NET Controls

(continued) - Document classes maintain form data state between postbacks

- In the ViewState hidden element of the form - The life of an ASP.NET document:

1. The client requests the document

2. A document class is created by compiling the requested document; then its constructor is called

3. The control state of the document is initialized with ViewState

4. Request form data is used to set the control state 5. The current control state is saved in ViewState

6. The instance is executed and the results are returned to the client

7. The class and its instance are deleted on the server

8. The client interacts with the form 9. The client causes a postback

10. A document class is compiled and its constructor is called

11. The control state is initialized from ViewState

12. The control state is set with the form data 13. The current state is saved in ViewState

14. The instance is executed and the results are returned to the client

 SHOW ex3.aspx

(16)

13.4 ASP.NET Controls

(continued)

- There are two levels of events – control events and page-level events

- Four page-level events are implicitly raised during the process of processing a request

Load, Unload, PreRender, and Init

- There are two ways to write and register handlers for these events

1. Write handlers with preassigned names and a specific protocol – implicitly registered

public void Page_Init(System.EventArgs e) { … }

- Called auto event wireup

2. Overload virtual methods and manually register them

- Control Events

- ServerClick and ServerChange

- Two ways to write and register handlers

(17)

13.4 ASP.NET Controls

(continued)

1. Write them as functions and register them in the XHTML

(OnServerClick and OnServerChange) - The protocol for control event handlers is protected void TextBoxHandler (

object src, System.EventArgs e) { ... } <input type = ″text″ id = ″Name″

OnServerChange = ″TextBoxHandler″

runat = ″server″ />

2. The second way to register a control event handler is to use the standard CLR approach - Write the handler, as before

- Create a handler delegate instance, passing the name of the handler to the constructor - Subscribe the new delegate instance to the control and the event name

protected void Page_Init(

object src, EventArgs e) { Name.ServerChange +=

new EventHandler(TextboxHandler);

}

(18)

13.4 ASP.NET Controls

(continued) - Revised list of what happens:

1. Client requests the document

2. A document class is compiled and its constructor is called

3. The Page event Init is raised

4. The control state of the instance is initialized with ViewState

5. The form data is used to initialize the control state 6. The Page event Load is raised

7. Server-side control events are raised 8. The Page event PreRender is raised

9. The current control state of the instance is saved in ViewState

10. The instance is executed and the results returned to the client

11. The Page event Unload is raised

12. The class and its instance are deleted on the server

- Web Controls

- A larger and richer collection than the HTML controls – based on those of VB

- A weaker connection to the XHTML elements

(19)

13.4 ASP.NET Controls

(continued) - A more consistent programming interface

- Web controls include:

- Xml – allows the inclusion of XSL transformations

- Panel – allows collections of elements to be handled together (placement, etc.)

- AdRotator – Easy way to have different content appear on different requests

- Validator controls – later

- Controls can be created by either markup or by programming code

- For example,

<asp.button id = ″helpButton″ Text = ″help″

OnClick = ″OnClickHandler″

runat = “server” />

-

(20)

13.4 ASP.NET Controls

(continued)

protected Button helpButton = new Button();

helpButton.Text = ″help″;

helpButton.id = ″helpButton″;

helpButton.OnClick = ″OnClickHandler″;

helpButton.runat = ″server″;

- There are two problems with doing it this way:

1. It required more typing

2. Placement of the control in the document is cumbersome

- Can use a placeholder

<asp:placeholder id = buttonPlace runat = server />

buttonPlace.Controls.Add(helpButton);

- Although creating elements is easier with

markup, modifying them is a good use of code - Example: put the list items in a drop down list with code

(21)

13.4 ASP.NET Controls

(continued) - Response output from controls

- Can’t use Response.Write, because the output goes to the beginning of the buffer, rather than close to the controls

- Alternative:

- Create a label element where you want the output to appear

- Set the content of the label by assigning to its Text property

- Use string.Format for output with text and values

<asp:label id = ″output″

runat = ″server″ />

<% string msg = string.Format{

″The result is {0} <br />″, result);

output.Text = msg; %>

 SHOW ex4.aspx and ex4.aspx.cs

(22)

13.4 ASP.NET Controls

(continued) - Validation Controls

- There are six; the most commonly used four:

- RequiredFieldValidator

- CompareValidator

- RangeValidator

- RegularExpressionValidator

- Validation controls are placed just after the controls whose values they are to validate

- Use the ControlToValidate attribute to specify the control to be validated

- Use the ErrorMessage attribute to specify the error message

 SHOW ex5.aspx

Riferimenti

Documenti correlati

In this paper, we have investigated the fiscal and distributional consequences of correcting the income tax homeownership bias through the inclusion of net imputed rent in

By the extraction, four PHENOLEA fractions with different biophenol concentrations have been produced: PHENOLEA OH-TYR, obtained from olive oil by-products which contains

(condizionate o meno), che offrono ad Amplifon la possibilità di acquistare in futuro la quota dei partrnes, ai partners la possibilità di vendere ad Amplifon la propria quota

4 For two of the control volumes the order of magnitude of NEE computed with the mass balance approach (F c +F s +F ha +F va ) was not compatible with biotic fluxes measured

There were statistically significant decreases in the EU/EEA mean 10-year trends relating to consumption of sub- groups of antibacterials in the community for tetracyclines (ATC

The general rating is positive: 38 websites out of 66 do have a link section, 9 49 websites have a space for the collaboration projects with other

A questo proposito per esempio, il sito finlandese MTT presenta, in home page, life stories, ossia esperienze di giovani ricercatori del proprio istituto, descrivendone non

nel corso degli anni Settanta il cerchio di amici polacchi si allargò a includere altri studiosi, come Jerzy Kłoczowski (1924-2017), medievista specializzato in