Overview of the .NET Framework
The .NET is a very large framework and sometimes is hard to decide whether to code something or use an existing component of the framework. Sometimes is faster to code, but when production workload and reliability comes into play, the perspective is different. An overview of what .NET offers is a fundamental knowledge. It is hard to keep in track because .NET evolves very fast. This article discusses essentials which is the .NET Framework built from.
- CLR – Common Language Runtime
- GC – Garbage Collector
- String Builder
- SQL Server
- MEF – Managed Extensibility Framework
- TPL – Task Parallel Library
- DLR – Dynamic Language Runtime
- Lazy initialization
- TDF – TPL Dataflow
- RegEx – Regular Expressions
- 1st XML processing
- 1st feed processing
- 2nd XML processing
- 3rd XML processing
Common Language Runtime
The most significant characteristics of the .NET Framework is the ability to use classes and call methods across different languages. Almost everything is written in C# now, but when this language will be beaten by something revolutionary new, it will not be necessary to rewrite all C# code and libraries.
All languages are compiled into Common Intermediate Language and this language is compiled to the native code for the processor architecture where the code runs. That’s why the C# is called managed language. The .NET is the fastest runtime compare to others. It is slower just about 5% compare to the native code, but the benefits are huge.
The garbage collector make us free from the old C++ world where is necessary to manage the memory. The .NET takes care of it. Since .NET 4 the garbage collecting is processed in the separate thread, so the performance is more than sufficient.
ADO (ActiveX Data Objects) was part of COM (Component Object Model) for accessing data sources. ADO.NET has nothing common with it but the name. It is a framework in System.Data namespace for accessing mainstream databases. Every serious database manufacturer delivers its provider for the ADO.NET, which means the .NET Framework support.
In whole .NET is the only one and single string type used to represent a string value. The string is internally represented in the UTF-8 format and in memory exist only one instance. It seems to be a sure thing, but the hard-to-survive C++ world is far away from it.
Regular expressions provide a powerful, flexible, and efficient technique for string processing. When you need to pick up something from the string, think seriously about regular expression use. There is quite a complex theory around it, but it is worth to learn it. The .NET implementation is compatible with Perl 5 and supports right-to-left matching and on-the-fly compilation.
1st XML processing
The XmlDocument class in the System.Xml namespace provides XML document parsing and browsing. It offers XPath language for data extraction.
The HttpUtility is very important static class used for encoding and decoding URL arguments when processing web requests. Specially one method is extremely useful – the HtmlDecode method, which replaces HTML entities with equivalent Unicode characters.
Don’t even thing about concatenating several strings in the loop other than via the StringBuilder class. It uses char arrays internally and is extremely optimized. Of course, writing strings directly to the stream when possible, avoids high memory consumption.
The C# is very type safe language. Generics algorithms are abstracted and require type features they need. Those features may be described by interface or abstract class. For example the most common is the IEnumerable<T> which is a collection of some object.
EventLog is the unified logging system for Windows. Instead of managing creating and truncating logging files or filtering and categorizing data, the EventLog class takes care of it.
The ConfigurationManager class contains connection strings and other constants you want to change without need to recompile. The referenced DLL assembly can access main executable assembly’s configuration file too. In the ASP.NET the configuration file is the web.config file.
You can write SQL Server‘s stored procedures in the .NET language. It can be sometimes useful, but the .NET assembly must be stored into database via T-SQL command. Only the 2.0 version is supported.
There are reference types and value types. Very important to know in C++ world, but rarely need to realize in C# world. Reference types are objects and value types are int, long, byte, bool, short, char and so on. Only value types need ref parameter keywords. Also only value types are not initialized with the null default value because they don’t point to anything. They are initialized with some default value instead. It is often awkward. That’s what nullable types are for.
XAML allows creating a user interface in HTML-like language. Declarative way has many advantages. One of them is binding – essential of the MVVM design pattern. XAML is accelerated by a graphics card which means it’s very fast.
Web services are good for invocation of remote methods on a server from a client. Web services are also standardized, therefore often used in interoperability scenarios. They are called WCF Services.
The workflow is an activity model. It can be anything from a business process to a Team Foundation Server Build. The advantage is that activity is defined declaratively by XAML instead of code. It can be modified as easily as configuration file without the need to recompile.
1st feed processing
The SyndicationFeed class proves support for RSS 2.0 and Atom 1.0 syndication formats.
Language Integrated Query
The LINQ is a killer feature for other languages and must-have capability once used to work with. The LINQ is something like a SQL for object oriented programming. All the operations like sorting, taking top n items, looking for items which satisfy some condition; all this stuff can be made very easily and elegantly in a single line by LINQ.
When you learn LINQ and realize it is much more comfortable than SQL, you want to use LINQ for database queries. That’s what the Entity Framework is made for. Since the version 4 you can write your classes which matching database tables and use them instead of database schema. You can also generate database schema from them. It is called Code-First approach. When you need later to update database schema, you can use Database Migrations. It is simple method which describes schema changes and manages migrations to the latest schema automatically by Entity Framework.
2nd XML processing
The XDocument class in the System.Xml.Linq namespace is designed to be used with LINQ. XDocument provides in-memory APIs rather than streaming ones.
The Rx is a library to compose asynchronous and event-based programs using observable collections and LINQ-style query operators. It is not embedded into .NET Framework yet, but significant enough to be listed here. In comparison with TPL introduced later, Rx is designed to work with data streams while TPL is familiar with the producer-consumer pattern.
Managed Extensibility Framework
MEF was integrated into .NET Framework into the System.ComponentModel.Composition namespace. It allows using assemblies as modules. When you need to create a modular system, MEF takes care of getting together all modules.
Parallel Extensions contains Parallel LINQ (PLINQ) and Task Parallel Library (TPL). When you have a lot of work to do, it is worth to use a task-based approach. Split the work into many small tasks and run them as you need. Run them sequentially or in parallel, but without managing threads. Parallel class takes care of processors count and the efficient number of concurrent threads. When you program on this level, you should use appropriate collection classes – concurrent collections in the System.Collections.Concurrent namespace.
Dynamic Language Runtime
You can use a dynamic type when you need to interact with type lacking environment, dynamic languages or whatever. It is very useful when interacting with COM environment because it does not provide the IntelliSence.
When the program contains something time consuming to initialize, which may not be used during the program lifetime, use a lazy initialization is a very good idea. Lazy<T> object initialization occurs when the object is accessed for the first time.
Asynchronous programming was always possible in .NET, but it sucks, until the .NET 4.5. It introduces the await keyword, the imported F#’s pipe forward operator. When the threads just waits to the server respond, it actually does nothing but consuming machine’s resources. Separate thread is good for large computing tasks, but not for waiting thus blocking operations. The keyword await means: “Hey, this will take a while, do something useful in the meantime.”
Simple web programming interface
The HTTP protocol becomes a wide standard for data exchange for its useful features other protocols don’t have. Use methods that take single Uri argument in order to manipulate data. For example:
- XmlDocument.LoadFromUriAsync downloads and parses the XML file.
- WebClient.DownloadStringAsync serves downloaded content directly as a string.
- WebClient.DownloadFileAsync saves downloaded content to the local storage.
- WebClient.UploadFileAsync uploads a file to the server.
- WebClient.PostAsync is the easiest way to send a POST request.
JSON becomes lightweight universal object serialization format compared to XML. The JsonObject is a concurrent of very serious and solid Json.NET library used with older .NET versions.
The AtomPubClient class enables HTTP CRUD access to web resources using the AtomPub protocol.
2nd feed processing
Like its predecessor, the SyndicationClient class proves support for RSS 2.0 and Atom 1.0 syndication formats.
3rd XML processing
The TDF, living in the System.Threading.Tasks.Dataflow namespace, is the foundational layer for the asynchronous and concurrent programming using Task provided in Task Parallel Library. It provides a high-level approach necessary to manage large amount of data.