780 likes | 1.19k Views
.NET Components. Objectives of Chapter 6 .NET Components. Introduce .NET framework Introduce the concepts of .NET components Discuss the types of .NET components, connections of components, and their deployments Distinguish local and distributed components
E N D
Objectives of Chapter 6 .NET Components • Introduce .NET framework • Introduce the concepts of .NET components • Discuss the types of .NET components, connections of components, and their deployments • Distinguish local and distributed components • Distinguish aggregation and containment compositions • Distinguish synchronous and asynchronous method invocations • Provide step by step tutorials on building, deploying, and using .NET components
Overview of .NET Framework • The .NET framework is introduced by Microsoft in 2000. • The .NET framework is a platform for rapid and easier building, deploying, and running secured .NET software components to be integrated in applications as well as for rapid developing XML web services and applications. • It provides a highly productive, component-based, multi-languages environment for integrating existing applications with internet to meet the challenges of new applications for deployment and operation of internet-scale applications.
Overview of .NET Framework (cont.) • The .NET framework encompasses a virtual machine that provides a new platform for software development. The core of the .NET framework includes XML and Simple Object Access Protocol (SOAP) to provide web services over Internet. • The concept of the .NET framework is to facilitate the developments of desktop, window, and web-based application services on Windows platform and make them available and accessible not only on Windows platform but also on other platforms through common protocols such as SOAP and HTTP.
Overview of .NET Framework (cont.) • .NET simplifies the componentization especially for Component Object Model (COM), Distributed COM (DCOM) technology. • Although COM components can be reused as plug-in play software components in component software and application constructions but the development process is too complex and COM does not support versioning (side by side execution), which may cause version conflict (DLL Hell problem). • The .NET technology supports component assembly deployment which allows multiple versions of same named components to coexist without any conflict. .NET technology simplifies the creation, and deployment of components in addition to securing reliable and scalable services provided by components.
Overview of .NET Framework (cont.) • .NET facilitates developments of distributed components by Remoting Channel technology. .NET framework supports the interoperability between COM and .NET components. • The XML web service is another kind of component .NET can provide services to COM components and COM components can also be used by any .NET components. .NET is much easier to develop components than COM. • Web service is a replacement of MS DCOM technology for Internet applications supported by XML, SOAP and HTTP protocols. .NET frees developer’s coding from heavy enterprise programming such as transaction management through Enterprise Service. • .NET web service overcomes DCOM’s lack of support for firewall and makes services available across platforms via loosely coupled XML SOAP protocols.
Overview of .NET Framework (cont.) • The .NET framework is available in .NET Framework SDK and Visual Studio.NET IDE SDK. • The .NET Framework SDK is the foundation of Visual Studio .NET and is a part of Visual Studio .NET when Visual Studio .NET is installed. The .NET framework consists of two main parts: Common Language Runtime (CLR) and a set of unified framework basic class libraries including ASP.NET web forms for building web applications, Windows Forms for building desktop applications, and ADO.NET for data access. • The SDK includes all your need to write, build, test, and deploy .NET applications. It supports all .NET languages such as VB .NET, VC.NET, C#, and others. .NET SDK and Visual Studio .NET can access services of all layers in the .NET framework platform.
Foundation of .NET framework – CLR • Common Language Runtime (CLR) is a virtual machine environment sitting on the top of Windows operating system. CLR consists of Common Type System (CTS), Just-In-Time IL Compiler (JIT), Execution unit, plus some other management services such as garbage collection and security management. • CLR is like JVM in Java. All these software components are assembled in a package of assembly (just like Java archive file .jar file) which consists of MS Intermediate Language (MSIL) code and manifest (Metadata about this packet). • The IL code is translated into native code by JIT compiler in CLR. IL code is verified by CTS first to check the validity of data type used in the code.
Foundation of .NET framework – CLR (cont.) • .NET framework integrates multiple programming languages (VB, managed VC#, C# etc) by Common Language CLR implementation. Not only a component in one language can access the services provided by other components in other languages, but also a class in one language can inherit properties and methods from related classes in other Languages. • The CTS defines a standard set of data type and rules for creating new types. The CLR knows how to execute these types. There are two categories of types: Reference type and Value type. The code targeting CLR and to be executed by CLR is called .NET managed code. All MS language compilers generate managed codes that conform to the CTS.
Foundation of .NET framework – CLR (cont.) • The IL code is like Java byte code. Regardless of the types of source code programming languages IL codes can communicate with each other by supports of the CLR. The IL code can be either in the format of executable (.EXE) or Dynamic Link Library (.DLL). • If these IL codes are generated by .NET compiler, they are called managed code. The managed code can be executed only on .NET aware platform. Some DLL or EXE generated by non .NET compilers (such as early version of VC++) are called un-managed code.
CLR • In summary, The CLR is a high performance execution engine. It provides a code-execution environment which manages the code targeting the .NET framework. • The code management includes management of memory, thread, security, code verification and IL compilation.
The .NET Framework Class Library • The .NET framework class library is a collection of reusable basic classes which are well organized by namespaces. The framework class library collects all classes including Windows Foundation Classes (WFC) into a single unified set of class • The namespace is just like a logical package in Java technology and the class library is just like the Java API structure. • A namespace consists of many classes and sub-namespaces. It is deployed as a component class library itself and is organized in a component–based hierarchy. The .NET framework itself is built up in a component model, for example, System namespace System.Runtime.Remoting is available in mscolib.dll and System.XML namespace is available in System.XML.dll. A .dll file is a .NET deployed component (Assembly).
The .NET Framework Class Library (cont.) • The root namespace in the class library is System namespace, which contains many basic classes such as Object, Console, and may contain sub-Namespaces such as IO, Net, Data, Remoting, etc. • For example, XML is a sub-Namespace of System namespace which is deployed as System.XML.dll, ADO.NET is available in System.Data.dll which corresponds to System.Data namespace, and Form based UI classes are available in System.Windows.Forms.dll corresponding to namespace System.Windows.Forms.
The .NET Framework Class Library (cont.) • Developers can create custom namespace and organize related classes in a custom namespace. A namespace can be deployed as an assembly of binary components. Same name classes can be placed in different namespaces because they are referenced by different namespace prefix. • In order to use classes in a namespace a directive using <namespace> in C# or • import <namespace> in VB must be included at the beginning of code. The system built-in basic class library is deployed in mscorlib.dll file.
The Component Model of .NET • MSIL DLL components are replacing COM Components, the MSIL Remoting Channels EXE components are replacing DCOM Component, and the Web Service components are new SOAP components intended to be cross-platform, and cross-language web based components. • Also, the .NET component technology is unified language oriented. Any .NET component is in the format of pre-compiled MSIL, which can be binary plugged in by any other MSIL components or any other .NET compatible clients.
The Component Model of .NET (cont.) • A .NET component is a single pre-compiled and self described MSIL module built from one or more classes or multiple modules deployed in a DLL assembly file. An assembly consists of up to four parts: 1) Manifest (table of info records): name of assembly, key info version, strong name, culture info, files that make up assembly, reference depended assemblies exported info. 2) Metadata of modules 3) IL code of module 4) Resources such as image files.
The Component Model of .NET (cont.) • A module has MSIL code and its metadata but without manifest. A module is not loadable dynamically. It is used as a building block at the compile time to build up an assembly Module file. It has a extension of .netmodule. There may be one or many classes in a module. • An Assembly is made up by one or many classes in a module. Each module may be coded in different languages but finally in same MSIL format. Assembly has a manifest file to self-describe the component itself. • An assembly has a file extension .dll or .exe and is dynamically loadable. That is why most people say .NET component is an Assembly (We will say it is deployed in an assembly). A .dll file is not executable just like a class file is a byte code file that is not executable.
The Component Model of .NET (cont.) • There are many different types of components in the .NET framework. We can classify them into visual or non-visual component categories. • A visual component is a control which can be deployed in a toolbox as an icon for “drag and drop” in a window form container. We focus on non-visual component, which is known as .NET component.
The Component Model of .NET (cont.) • A .NET component can be a local component (.dll), which can only be accessed locally (within same application domain), in same machine or a remote (distributed) component (.exe), which can be accessed remotely (across application domains) in same machine or different machines. • A .NET DLL component can be deployed as a private component, which knows the target client or can be deployed as a shared public component, which does not know the target client. A DLL component can be plugged-in to Windows form, Web form of another DLL or application.
The Component Model of .NET (cont.) Let’s take a look at a very simple sample component in C#. This component provides services of converting temperature between F and C. //Listing of TempConvComp.CS: using System; namespace TempConv { public class TempConvComp { public double cToF (double c) { return (int) ((c*9)/5.0+32); } public double fToC (double f) { return (int) ((f-32)*5/9.0); } } }
The Component Model of .NET (cont.) We can build a module from it by the following command: >csc /t:module TempConvComp.cs TempConvComp.netmodule This module can be added to a component by: >csc /t:library /addmodule: TempConvComp.netmodule anotherComp.dll Also, we can build a DLL component by the command: >csc /t:library TempConvComp.cs TempConvComp.dll
The Component Model of .NET (cont.) • Here are two clients of this component. One is TempConvCSClient.cs in C# and the other is TempConvCppClient.cpp in C++. Both of them reuse this TempConvComp component. • The following list is the C# program list of the client of TempConvComp component. using System; using TempConv; class MainApp { public static void Main() { TempConv.TempConvComp myCSTempConvComp = new TempConv.TempConvComp(); double choice; double input; double output; bool next = true;
The Component Model of .NET (cont.) while (next) { Console.WriteLine("Please enter your choice: 1 - Converter from F to C, 2 - from C to F, 3 - exit"); choice=Double.Parse(Console.ReadLine()); if (choice == 1) { Console.WriteLine("Please tell me the temperature in F: "); input=Double.Parse(Console.ReadLine()); output = myCSTempConvComp.fToC(input); Console.WriteLine(output); }
The Component Model of .NET (cont.) else if (choice ==2) { Console.WriteLine("Please tell me the temperature in C: "); input=Double.Parse (Console.ReadLine()); output = myCSTempConvComp.cToF(input); Console.WriteLine(output); } else { next= false; Console.WriteLine ("See you next time."); } } } }
The Component Model of .NET (cont.) • In TempConvCSClient.cs the client loads TempConv namespace by “using TempConv” directive and instantiates an instance of the TempConvComp component and invokes fToC() and cToF() methods provided by this component. • The Client application in C# can be built by the following command: • >csc /t:exe /r:TempConvTemp.dll TempConvCSClient.cs TempConvCSClient.exe
managed C++ client of TempConvComp component #include "stdafx.h" #include <iostream> #using <mscorlib.dll> #using "TempConv.dll" using namespace System; using namespace std; // This is the entry point for this application #ifdef _UNICODE int wmain(void) #else int main(void) #endif { int choice; double input; double output; bool next = true; TempConv::TempConvComp * myCSConvComp = new TempConv::TempConvComp ();
The Component Model of .NET (cont.) while (next) { Console::WriteLine(S"Please enter your choice: 1 - Converter from F to C, 2 - from C to F, 3 - exit"); cin>>choice; if (choice == 1) { Console::WriteLine(S"Please input the temperature in F: "); cin>>input; output = myCSConvComp->fToC(input); Console::WriteLine(output); } else if (choice == 2){ Console::WriteLine(S"Please input the temperature in C: "); cin>>input; output = myCSConvComp->cToF(input); Console::WriteLine(output); } else { next= false; Console::WriteLine (S"See you next time."); } } return 0; }
The Connection Model of .NET • .NET component compositions • Component compositions enable the component reuse in either aggregation compositions or containment compositions. • In aggregation composition model the service of inner component hands out its service directly to the client of outer component. In aggregation composition the outer component exposes the interfaces of inner component. The innerM() method of inner component becomes part of interface to the outer component
.NET containment compositions • In containment compositions, if a request to the outer component needs helps from an inner component the request is forwarded to that inner component. • The outer component does not expose the interface of the inner component. The containment is transparent to the client of an outer component. • The client is blind of the handler of the request. The outerM2() delegates a request to the innerM() method of inner component
.NET mixed component compositions • A .NET component can also be composed by mixed aggregations and containments in a flat structure or nested compositions in multiple levels in depth. • Here we use an example to explain the concepts of combined containment and aggregation compositions.
.NET component compositions (cont.) using System; namespace NS1 { public class Inner { public void innerM () { Console.WriteLine (“I am Inner.”) } } } using System; using NS1;
.NET component compositions (cont.) public class Outer { public Inner i = new Inner (); //aggregation: Outer expose the Inner public void outerM1 () { Console.WriteLine (“I am outer.”); } public void outerM2() //delegation in containment { i.innerM(); } public static void main() { outer o1 = new Outer(); Inner i1 = o1.i; i1.innerM(); //interface to the aggregate o1.outerM1(); o1.outerM2(); // interface to the containment Inner i2 = new Inner(); i2.innerM(); } }
The communication of Components by Event and Delegate • The .NET Delegate is a method type (a reference to a method) which is similar to function pointer in C++, but it is type-safe and secure. A delegate will delegate a flow control to its registered event handler when the event is raised. It works in the pattern of observer which is kind of event listener in Java. • An instance of a Delegate can hold a static method of a class or a method of a component or a method of object itself. • There are two types of Delegates: SingleCast or MultiCast.
The communication of Components by Event and Delegate (cont.) A SingleCast Delegate can only delegate one method at a time Delegate int MyDelegate(); public class MyClass { public int ObjMethod() {- - - } static public int StaticMethod () {- - - } public class Drive { Static public void Main() { Myclass c = new MyClass(); int x; MyDelegate dlg = new MyDelegate(c.objMethod()); x=dlg(); dlg = new MyDelegate (MyClass.StaticMethod()); x=dlg(); } }
The communication of Components by Event and Delegate (cont.) • As seen in this example, MyDelegate is a Delegate which references any method with int return type and without any parameter. • The signatures of objMethod and StaticMethod match the Delegate MyDelegate. • The first dlg() invokes objMethod and second dlg() invokes class method StaticMethod.
The communication of Components by Event and Delegate (cont.) • A MultiCast Delegate has a void return type and can bind multiple methods and it will invoke them in the order of registrations. • Delegate void MultiDelegate(); • MultiDelegate mdlg = null; • mdlg += new MultDelegate (Method1); • mdlg += new MultiDelegate (Method2); • Registration is done by += Delegate operation and unregistration is done by -= operation. • The Delegate plays a role of listener and event handler must register this listener to be able to handle the event once the event is fired off.
The communication of Components by Event and Delegate (cont.) • An event is a message sent by an object to invoke an action. The object that raises the event is event source and object intercepts the event and handles the event is event target. • This is an event-driven communication model between components or within same component. The Delegate class is the communication channel class between event source and event target. • Event can be predefined event such as an event trigger by a Windows Form component. A developer can also define a custom event.
The communication of Components by Event and Delegate (cont.) 1. Create a delegate class. Public delegate void DelegateStart(); 2. Create a class containing a delegate field, Class MyClass { public event DelegateStart EventStart; - - - 3.Define an event handler public void handleEvent(){ - - -} 4. Bind delegate event with event handler via event listener, trigger an event, invoke the event handler. Public static void Main() { MyClass EventObj = new MyClass(); EventObj.EventStart += new DelegateStart(handleEvent); EventObj.EventStart(); _ _ _ }
Remoting Connectors for .NET Distributed Components • A component or a client cannot directly access a remote component running in different application domain in same or different processes unless using Remoting channel connection. • The marshaling makes it possible to invoke a remote method of a distributed component. • There are two ways to marshal an object: in MBV (Marshal by Value) server passes a copy of object to client or in Marshal by Reference (MBR) client creates a proxy of a remote object. • When a remote component must run at a remote site, MBR is the only choice.
Remoting Connectors for .NET Distributed Components (cont.) • A channel is the way to implement communications between clients and remote components. We use TCP channel in our example. • The channel is similar to the socket communication in Java. Each channel must bind with a port. A client channel binds a client port and the server channel binds a port on the server. • We create TCP channel on port 4000 and register the channel with the remote class and URI name, which will be used by client to get an object of remote component. We also need to create a TCP channel and register it on the client site.
Remoting Connectors for .NET Distributed Components (cont.) using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; public class CoTempConv : MarshalByRefObject { public static void Main() { TcpChannel channel = new TcpChannel(4000); ChannelServices.RegisterChannel(channel); RemotingConfiguration.RegisterWellKnownServiceType ( typeof(CoTempConv), "TempConvCompDotNet", WellKnownObjectMode.Singleton ); System.Console.WriteLine("Hit <enter> to exit..."); System.Console.ReadLine(); }
Remoting Connectors for .NET Distributed Components (cont.) public double cToF(double c) { return (int)((c*9/5.0+32)*100)/100.0; } public double fToC(double f) { return (int)((f-32)*5/9.0*100)/100.0; } }
Remoting Connectors for .NET Distributed Components (cont.) the client of above component. using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Tcp; class MainApp { public static void Main() { try { TcpChannel channel = new TcpChannel(); ChannelServices.RegisterChannel(channel); CoTempConv myCSTempConvComp = (CoTempConv)Activator.GetObject( typeof(CoTempConv), "tcp://127.0.0.1:4000/TempConvCompDotNet"); double choice; double input; double output; bool next = true;