Thursday, April 2, 2009

The .NET Solution

So much for the brief history lesson. The bottom line is that life as aWindows programmer has been
tough. The .NET Framework is a rather radical and brute-force approach to making our lives easier.
The solution proposed by .NET is “Change everything” (sorry, you can’t blame the messenger for the
message). As you will see during the remainder of this book, the .NET Framework is a completely new
model for building systems on the Windows family of operating systems, as well as on numerous
non-Microsoft operating systems such as Mac OS X and various Unix/Linux distributions. To set the
stage, here is a quick rundown of some core features provided courtesy of .NET:
• Full interoperability with existing code: This is (of course) a good thing. Existing COM binaries
can commingle (i.e., interop) with newer .NET binaries and vice versa. Also, Platform Invocation
Services (PInvoke) allows you to call C-based libraries (including the underlying API
of the operating system) from .NET code.
• Complete and total language integration: Unlike COM, .NET supports cross-language inheritance,
cross-language exception handling, and cross-language debugging.
• A common runtime engine shared by all .NET-aware languages: One aspect of this engine is
a well-defined set of types that each .NET-aware language “understands.”
• A base class library: This library provides shelter from the complexities of raw API calls and
offers a consistent object model used by all .NET-aware languages.
• No more COM plumbing: IClassFactory, IUnknown, IDispatch, IDL code, and the evil VARIANTcompliant
data types (BSTR, SAFEARRAY, and so forth) have no place in a native .NET binary.
• A truly simplified deployment model: Under .NET, there is no need to register a binary unit
into the system registry. Furthermore, .NET allows multiple versions of the same *.dll to
exist in harmony on a single machine.
As you can most likely gather from the previous bullet points, the .NET platform has nothing to
do with COM (beyond the fact that both frameworks originated from Microsoft). In fact, the only
way .NET and COM types can interact with each other is using the interoperability layer.
■Note Coverage of the .NET interoperability layer (including PInvoke) is beyond the scope of this book. If you
require a detailed treatment of these topics, check out my book COM and .NET Interoperability (Apress, 2002).

Introducing the Building Blocks of the .NET
Platform (the CLR, CTS, and CLS)
Now that you know some of the benefits provided by .NET, let’s preview three key (and interrelated)
entities that make it all possible: the CLR, CTS, and CLS. From a programmer’s point of view, .NET
can be understood as a new runtime environment and a comprehensive base class library. The runtime
layer is properly referred to as the common language runtime, or CLR. The primary role of the
CLR is to locate, load, and manage .NET types on your behalf. The CLR also takes care of a number
of low-level details such as memory management and performing security checks.
Another building block of the .NET platform is the Common Type System, or CTS. The CTS
specification fully describes all possible data types and programming constructs supported by the
runtime, specifies how these entities can interact with each other, and details how they are represented
in the .NET metadata format (more information on metadata later in this chapter).
Understand that a given .NET-aware language might not support each and every feature defined
by the CTS. The Common Language Specification (CLS) is a related specification that defines a subset
of common types and programming constructs that all .NET programming languages can agree on.
Thus, if you build .NET types that only expose CLS-compliant features, you can rest assured that all
.NET-aware languages can consume them. Conversely, if you make use of a data type or programming
construct that is outside of the bounds of the CLS, you cannot guarantee that every .NET programming
language can interact with your .NET code library.

The Role of the Base Class Libraries
In addition to the CLR and CTS/CLS specifications, the .NET platform provides a base class library
that is available to all .NET programming languages. Not only does this base class library encapsulate
various primitives such as threads, file input/output (I/O), graphical rendering, and interaction
with various external hardware devices, but it also provides support for a number of services required
by most real-world applications.
For example, the base class libraries define types that facilitate database access, XML manipulation,
programmatic security, and the construction of web-enabled (as well as traditional desktop and
console-based) front ends. From a high level, you can visualize the relationship between the CLR,
CTS, CLS, and the base class library,

No comments:

Post a Comment