Chapter 14 Interoperability

Microsoft .NET is a powerful platform, and there are many advantages in writing a new application within the .NET Framework. However, a typical application is not a world unto itself, but is built from legacy components as well as new components, and interoperability is very important. We discussed one kind of interoperability in Chapter 11 in connection with Web Services. Using the SOAP protocol it is possible for .NET applications to call Web Services on other platforms, including Unix,...

Demonstration Wrapping a Legacy COM Server

The best way to get a feel for how this wrapping process works is to perform the operations yourself. The .NET client program is in the directory NetClient. The directory LegacyComServer contains the following files reg bankdual.bat Batch file to register the server unreg bankdual.bat Batch file to unregister the server BankConsole.exe Client executable file The source code for the client and server are in the directories ClientSource and ServerSource respectively. Both programs are written in...

Summary Amu

In this chapter we studied, in detail, the object-oriented aspects of C , with an emphasis on inheritance. After a review of the fundamentals of object-oriented programming, we introduced the Acme Travel Agency case study, which runs as a strand throughout the entire book. We examined the suitable abstractions that enable us to implement a reservation system for a variety of resources that must be reserved, and we provided an implementation of a hotel reservation system. The abstract base...

ArrayList Example

To get our bearings, let's begin with a simple example of using the ArrayList class. An array list, as the name suggests, is a list of items stored like an array. An array list can be dynamically sized and will grow as necessary to accommodate new elements being added. Collection classes are made up of instances of type object. We will create and manipulate a collection of Customer objects. We could just as easily create a collection of any other built-in or user-defined type. If our type were...

Principal Permission

Role-based security is controlled by the PrincipalPermission class. The PrincipalPermission example uses this class to make sure that the user identity under which the program is being run is an administrator. We do that by passing the identity name and a string representing the role to the constructor. Once again, we use the Demand method on the permission to check the validity of our permission request. PrincipalPermission PrincipalPerm new PrincipalPermission wi.Name, adminRole...

Preprocessor Symbols Using Compiler Option

Besides using a define preprocessor directive in your source code, you can also define preprocessor symbols using the define command-line option of the C compiler. For example, you can define the symbol UNIX using the following command You can also specify preprocessor directives in Visual Studio. In Solution Explorer right-click on the solution. From the context menu choose Properties. Select Build from Configuration Properties, and enter your desired string in the Conditional Compilation...

Using ICloneable

The final version of copy relies on the fact that our Course class supports the ICloneable interface and implements the Clone method. To clone the Roster collection we use the fact that ArrayList also implements the ICloneable interface, as discussed earlier in the chapter. Note that the Clone method returns an object, so we must cast to ArrayList before assigning to the Roster field. Course course new Course Title course.Roster ArrayList Roster.Clone return course Here is the third part of the...

Memberwise Clone

Next we will illustrate doing a memberwise copy, which can be accomplished using the MemberwiseClone method of object. Since this method is protected, we cannot call it directly from outside our Course class. Instead, in Course we define a method, ShallowCopy, which is implemented using MemberwiseClone. return Course this.MemberwiseClone Here is the second part of the test program, which calls the ShallowCopy method. Again we change the title and a student in the second copy. nCopy is done via...

Optimistic vs Pessimistic Locking and the DataSet

Optimism Pessimism Figure

Transactions help preserve database consistency. When you move money from your savings to your checking account to pay your phone bill, transaction processing ensures that the credit and withdrawal will both happen, or neither will happen. You will not wind up with a situation where the money goes into your checking account but is not withdrawn from the savings good for you, and bad for the bank or the reverse bad for you, but good for the bank . Nothing about that transaction prevents your...

Setup and Deployment Projects

Assemblies may be deployed as regular stand-alone binary code files i.e., DLL or EXE files , or they may be deployed using CAB, MSI, or MSM files. A CAB file is a cabinet file with the .cab file-name extension. A CAB file is used to compress and combine other files into one convenient manageable file. Although CAB files can be used for general purposes, they have traditionally been used for CD-based and Web-based installation purposes. MSI and MSM files are Microsoft Windows Installer files,...

The Fragile Base Class Problem

One subtle pitfall in object-oriented programming is the fragile base class problem. Suppose the override keyword syntax did not exist. Suppose further that you derive a class from a third-party class library, and you have a method in the derived class that does not hide or override any method in the base class. Now a new version of the class library comes out, and the base class has a new virtual method whose signature happens to match one of the methods in your class. Now you can be in...

Multiple Tables in a DataSet

Each DataSet has a collection of one or more DataTable objects. Each DataTable object represents one table. With a SelectCommand that contains a join you can place data from multiple database tables into one DataTable. If you want to update the multiple tables, you will have to specify the update commands, because all relationship of the data to the original tables is lost. The SqlDataAdapter for the HotelBroker object has the following SelectCommand property string cmd select CustomerId,...

Metadata and Reflection

The Serialization example in Chapter 2 demonstrates how metadata makes possible many of the services of the Common Language Runtime. Many of the technologies we cover in the rest of the book rely on metadata, although we will not always stop and point this out. Metadata is information about the assemblies, modules and types that constitute .NET programs. If you have ever had to create IDL to generate a type library so that your C COM objects could be called by Visual Basic, or to create proxies...

An Echo Program

The first example program for this chapter is Hello.aspx, shown as a link on the home page. The example is complete in one file and contains embedded server code. Here is the source code, which consists of HTML along with some C script code. There are also some special tags for server controls, recognized by ASP.NET. lt Page Language C gt lt SCRIPT RUNAT SERVER gt protected void cmdEcho_Click object Source, EventArgs e lblGreeting.Text Hello, txtName.Text lt FORM RUNAT SERVER gt Your name amp...

Multimodule Assemblies

An assembly can be made up of multiple modules. A module is a DLL or EXE that contains managed code plus metadata, but not necessarily a manifest. However, an assembly must have one and only one manifest. Hence an assembly can contain multiple modules, but only one of them can have a manifest that provides information on the contents of all the modules in the assembly. The module with the manifest may have just the manifest, or it can contain other code or resources. The main advantage of...

Hotel Broker Web Service

The next step in the case study is to make the Customer and Hotel components of the Hotel Broker available as a Web Service. This Web Service is found in the HotelBrokerWebService subdirectory of the case study for this chapter. This Web Service will be used both by Acme's customers to make reservations as well as by administrators for maintenance tasks associated with the Hotel Broker. The proxy classes themselves are built into a proxies assembly. Two batch files that can be used to create...

Unauthenticated Users

Every machine that runs .NET has a machine.config file that has the default configuration for the computer. This file is found in the directory, where v1.0.2914 would be replaced by the version of Microsoft.NET that is running on your machine. A Web or Web Service application may have a config.web file that has the configuration settings for that application. The settings for config.web affect all applications in the directory where it lives and all its subdirectories. Config.web files in the...

Problems with Impersonation

It would seem that we need only make sure that the user id the thread impersonates is a member of the HotelAdmin group and does not have any more privileges than are needed i.e., is not System or an administrator, with no ACL rights to any unnecessary files on the server and then everything will be just fine. Unfortunately, life is not so simple. Impersonation was designed to be used by a server to alter its rights by running a thread as another user. When the server is done impersonating a...

The Asynchronous Design Pattern

This design pattern is composed of two parts, a set of methods and an interface IAsyncResult. The methods of the pattern are IAsyncResult BeginXXX inputParams, AsyncCallback cb, Object AsyncObject ReturnValue EndXXX outputParams, IAsyncResult ar As a design pattern, the XXX represents the actual method being called asynchronously i.e., BeginRead EndRead for the System.IO.FileStream class . The BeginXXX should pass all input parameters of the synchronous version in, in out, and ref as well as...

Static Binding

In C the normal way methods are tied to classes is through static binding. That means the object reference type is used at compile time to determine the class whose method is called. The HideEmployee program we just looked at illustrates static binding, using a simple Employee class and a derived SalaryEmployee class. Here is the test program public static void Main string args Employee emp new Employee Ellen SalaryEmployee sal new SalaryEmployee Sally, 100m emp.Show sal.Show sal emp emp sal...

PermissionSet

You can deal with a set of permissions through the PermissionSet class. The AddPermission and RemovePermission methods allow you to add instances of a CodeAccessPermission derived class to the set. You can then Deny, PermitOnly, or Assert sets of permissions instead of individual ones. This makes it easier to restrict what third-party components and scripts might be able to do. The PermissionSet example demonstrates how this is done. We first define an interface IUserCode that our trusted code...

C Destructor Notation

The C language provides a special tilde notation SomeClass to represent the overridden Finalize method, and this special method is called a destructor. The C destructor automatically calls the base class Finalize. Thus the following C code generates code that could be expressed protected override void Finalize The second code fragment is actually not legal C syntax, and you must use the destructor notation. Although C uses the same notation and terminology for destructor as C , the two are very...

Calling a Delegate

You call a delegate just as you would a method. The delegate object is not a method, but it has an encapsulated method. The delegate object delegates the call to this encapsulated method, hence the name delegate. In the following code the delegate object notifyDlg is called whenever a negative balance occurs on a withdrawal. In this example the notifyDlg delegate object is initialized in the method SetDelegate. public void SetDelegate NotifyCallback dlg public void Withdraw decimal amount...

XmlDataDocument

Documents can include database output within them. For example, a sales report has an explanation as well as the sales data that was pulled from a data source. The XmlDataDocument class can be used to represent data in the form of an XML document. The XmlDataDocument class inherits from XmlDocument which represents an XML document for the .NET XML Framework classes. What makes the XmlDataDocument particularly interesting is that you can construct an XmlDataDocument from a DataSet by passing the...

SystemException

The System.Exception class provides a number of useful methods and properties for obtaining information about an exception. Message returns a text string providing information about the exception. This message is set when the exception object is constructed. If no message is specified, a generic message will be provided indicating the type of the exception. The Message property is read-only. Hence, if you want to specify your own message, you must construct a new exception object, as done in...

Operating System Identity and CLR Identity

As we mentioned at the start of the chapter, .NET security sits on top of the underlying operating-system security. The identity associated with the thread by the CLR and the identity associated with the thread by the underlying operating system are not the same. The identity of the thread from the operating-system perspective is reflected by the setting of the WindowsIdentity object returned by the static Windows.Identity.GetCurrent method. The CLR identity is reflected by the value of the...

IList

The IList interface is derived from ICollection and provides methods for adding an item to a list, removing an item, and so on. An indexer is provided that enables array notation to be used. We discussed indexers in Chapter 3. object this int index get set int Add object value void Clear void Insert int index, object value Our StringList sample code illustrates using the indexer and the Add, Contains, Remove, and RemoveAt methods. private static void ShowArray ArrayList array for int i 0 i lt...

Programming Perl in the NET Environment

A very important part of the vision behind Microsoft .NET is that the platform is designed from the ground up to support multiple programming languages from many sources, and not just Microsoft languages. This book, like other books in the series, is rooted in long experience in industrial teaching. It covers the Perl language from the ground up. Although oriented toward the ActiveState Perl.NET compiler, the book also provides excellent coverage of the Perl language suitable for other versions...