Dajbych.net


Overview of the .NET Framework

, 12 minutes to read

The .NET is a very large frame­work and some­times is hard to de­cide whether to code some­thing or use an ex­ist­ing com­po­nent of the frame­work. Some­times is faster to code, but when pro­duc­tion work­load and re­li­a­bil­ity comes into play, the per­spec­tive is dif­fer­ent. An overview of what .NET of­fers is a fun­da­men­tal knowledge. It is hard to keep in track be­cause .NET evolves very fast. This ar­ti­cle dis­cusses essen­tials which is the .NET Frame­work built from.

.NET 1.0

Common Language Runtime

The most sig­nif­i­cant char­ac­ter­is­tics of the .NET Frame­work is the abil­ity to use classes and call meth­ods across dif­fer­ent lan­guages. Al­most ev­ery­thing is writ­ten in C# now, but when this lan­guage will be beaten by some­thing rev­o­lu­tion­ary new, it will not be nec­es­sary to rewrite all C# code and li­braries.

All lan­guages are com­piled into Com­mon In­ter­me­di­ate Lan­guage and this lan­guage is com­piled to the na­tive code for the pro­ces­sor ar­chi­tec­ture where the code runs. That’s why the C# is called man­aged lan­guage. The .NET is the fastest run­time com­pare to others. It is slower just about 5% com­pare to the na­tive code, but the ben­e­fits are huge.

Garbage Collector

The garbage col­lec­tor make us free from the old C++ world where is nec­es­sary to man­age the mem­ory. The .NET takes care of it. Since .NET 4 the garbage col­lect­ing is pro­cessed in the sep­arate thread, so the per­for­mance is more than suf­fi­cient.

ADO.NET

ADO (Ac­tiveX Data Ob­jects) was part of COM (Com­po­nent Ob­ject Model) for ac­cess­ing data sources. ADO.NET has noth­ing com­mon with it but the name. It is a frame­work in Sys­tem.Data names­pace for ac­cess­ing main­stream databases. Ev­ery se­ri­ous database man­u­fac­turer de­liv­ers its provider for the ADO.NET, which means the .NET Frame­work sup­port.

String

In whole .NET is the only one and sin­gle string type used to rep­re­sent a string value. The string is in­ter­nally rep­re­sented in the UTF-8 for­mat and in mem­ory ex­ist only one in­s­tance. It seems to be a sure thing, but the hard-to-sur­vive C++ world is far away from it.

.NET 1.1

Regular Expressions

Reg­u­lar ex­pres­sions pro­vide a pow­er­ful, flex­i­ble, and ef­fi­cient tech­nique for string pro­cess­ing. When you need to pick up some­thing from the string, think se­ri­ously about reg­u­lar ex­pres­sion use. There is quite a com­plex the­ory around it, but it is worth to learn it. The .NET im­ple­men­ta­tion is com­pat­i­ble with Perl 5 and sup­ports right-to-left match­ing and on-the-fly com­pi­la­tion.

1st XML processing

The Xml­Doc­u­ment class in the Sys­tem.Xml names­pace pro­vides XML doc­u­ment pars­ing and brows­ing. It of­fers XPath lan­guage for data ex­trac­tion.

HttpUtility

The Htt­pUtil­ity is very im­por­tant static class used for en­cod­ing and de­cod­ing URL ar­gu­ments when pro­cess­ing web re­quests. Spe­cially one method is ex­tremely use­ful – the HtmlDe­code method, which re­places HTML en­ti­ties with equi­va­lent Uni­code char­ac­ters.

String Builder

Don’t even thing about con­cate­nating sev­eral strings in the loop other than via the String­Builder class. It uses char ar­rays in­ter­nally and is ex­tremely op­ti­mized. Of course, writ­ing strings di­rectly to the stream when pos­si­ble, avoids high mem­ory con­sump­tion.

.NET 2.0

Generics

The C# is very type safe lan­guage. Gener­ics al­gorithms are ab­s­tracted and re­quire type fea­tures they need. Those fea­tures may be de­scribed by in­ter­face or ab­s­tract class. For ex­am­ple the most com­mon is the IEnu­mer­able<T> which is a col­lec­tion of some ob­ject.

Event Log

Event­Log is the uni­fied log­ging sys­tem for Win­dows. In­stead of man­ag­ing cre­at­ing and trun­cat­ing log­ging files or fil­ter­ing and cat­e­go­riz­ing data, the Event­Log class takes care of it.

Configuration Manager

The Con­fig­u­ra­tion­Man­ager class con­tains con­nec­tion strings and other con­s­tants you want to change without need to re­com­pile. The ref­er­enced DLL as­sem­bly can ac­cess main ex­e­cutable as­sem­bly’s con­fig­u­ra­tion file too. In the ASP.NET the con­fig­u­ra­tion file is the web.con­fig file.

SQL Server

You can write SQL Server‘s stored pro­ce­dures in the .NET lan­guage. It can be some­times use­ful, but the .NET as­sem­bly must be stored into database via T-SQL com­mand. Only the 2.0 ver­sion is sup­ported.

Nullable Types

There are ref­er­ence types and value types. Very im­por­tant to know in C++ world, but rarely need to re­al­ize in C# world. Ref­er­ence types are ob­jects and value types are int, long, byte, bool, short, char and so on. Only value types need ref pa­ram­e­ter key­words. Also only value types are not ini­tial­ized with the null de­fault value be­cause they don’t point to any­thing. They are ini­tial­ized with some de­fault value in­stead. It is of­ten awk­ward. That’s what nul­lable types are for.

.NET 3.0

XAML

XAML al­lows cre­at­ing a user in­ter­face in HTML-like lan­guage. Declar­a­tive way has many ad­van­tages. One of them is bind­ing – essen­tial of the MVVM de­sign pat­tern. XAML is ac­cel­er­ated by a graph­ics card which means it’s very fast.

Web services

Web ser­vices are good for in­vo­ca­tion of re­mote meth­ods on a server from a client. Web ser­vices are also stan­dardized, there­fore of­ten used in in­ter­op­er­abil­ity sce­nar­ios. They are called WCF Ser­vices.

Workflow

The work­flow is an ac­tiv­ity model. It can be any­thing from a busi­ness pro­cess to a Team Foun­da­tion Server Build. The ad­van­tage is that ac­tiv­ity is de­fined declar­a­tively by XAML in­stead of code. It can be mod­i­fied as eas­ily as con­fig­u­ra­tion file without the need to re­com­pile.

1st feed processing

The Syn­di­ca­tion­Feed class proves sup­port for RSS 2.0 and Atom 1.0 syn­di­ca­tion for­mats.

.NET 3.5

Language Integrated Query

The LINQ is a killer feature for other lan­guages and must-have ca­pa­bil­ity once used to work with. The LINQ is some­thing like a SQL for ob­ject ori­ented pro­gram­ming. All the op­er­a­tions like sort­ing, tak­ing top n items, look­ing for items which sat­isfy some con­di­tion; all this stuff can be made very eas­ily and el­e­gantly in a sin­gle line by LINQ.

Entity Framework

When you learn LINQ and re­al­ize it is much more com­fort­able than SQL, you want to use LINQ for database queries. That’s what the En­tity Frame­work is made for. Since the ver­sion 4 you can write your classes which match­ing database ta­bles and use them in­stead of database schema. You can also gen­er­ate database schema from them. It is called Code-First ap­proach. When you need later to up­date database schema, you can use Database Migra­tions. It is sim­ple method which de­scribes schema changes and man­ages mi­gra­tions to the lat­est schema au­to­mat­i­cally by En­tity Frame­work.

2nd XML processing

The XDoc­u­ment class in the Sys­tem.Xml.Linq names­pace is de­signed to be used with LINQ. XDoc­u­ment pro­vides in-mem­ory APIs rather than stream­ing ones.

Reactive Extensions

The Rx is a li­brary to com­pose asyn­chronous and event-based pro­grams us­ing ob­serv­able col­lec­tions and LINQ-style query op­er­a­tors. It is not em­bed­ded into .NET Frame­work yet, but sig­nif­i­cant enough to be listed here. In com­par­i­son with TPL in­tro­duced later, Rx is de­signed to work with data streams while TPL is fa­mil­iar with the pro­ducer-con­sumer pat­tern.

.NET 4.0

Managed Extensibility Framework

MEF was in­te­grated into .NET Frame­work into the Sys­tem.Com­po­nent­Model.Com­po­si­tion names­pace. It al­lows us­ing as­sem­blies as mod­ules. When you need to cre­ate a modu­lar sys­tem, MEF takes care of get­t­ing to­gether all mod­ules.

Parallel Extensions

Par­al­lel Ex­ten­sions con­tains Par­al­lel LINQ (PLINQ) and Task Par­allel Li­brary (TPL). When you have a lot of work to do, it is worth to use a task-based ap­proach. Split the work into many small tasks and run them as you need. Run them se­quen­tially or in par­al­lel, but without man­ag­ing threads. Par­al­lel class takes care of pro­ces­sors count and the ef­fi­cient num­ber of con­cur­rent threads. When you pro­gram on this level, you should use ap­pro­pri­ate col­lec­tion classes – con­cur­rent col­lec­tions in the Sys­tem.Col­lec­tions.Con­cur­rent names­pace.

Dynamic Language Runtime

You can use a dy­namic type when you need to in­ter­act with type lack­ing en­vi­ron­ment, dy­namic lan­guages or what­ever. It is very use­ful when in­ter­act­ing with COM en­vi­ron­ment be­cause it does not pro­vide the In­tel­liSence.

Lazy initialization

When the pro­gram con­tains some­thing time con­sum­ing to ini­tial­ize, which may not be used dur­ing the pro­gram life­time, use a lazy ini­tial­iza­tion is a very good idea. Lazy<T> ob­ject ini­tial­iza­tion oc­curs when the ob­ject is ac­cessed for the first time.

.NET 4.5

Asynchronous Pattern

Asyn­chronous pro­gram­ming was al­ways pos­si­ble in .NET, but it sucks, un­til the .NET 4.5. It in­tro­duces the await key­word, the im­ported F#’s pipe for­ward op­er­a­tor. When the threads just waits to the server re­spond, it ac­tu­ally does noth­ing but con­sum­ing ma­chine’s re­sources. Sep­arate thread is good for large com­put­ing tasks, but not for wait­ing thus block­ing op­er­a­tions. The key­word await means: “Hey, this will take a while, do some­thing use­ful in the mean­time.”

Simple web programming interface

The HTTP pro­to­col be­comes a wide stan­dard for data ex­change for its use­ful fea­tures other pro­to­cols don’t have. Use meth­ods that take sin­gle Uri ar­gu­ment in or­der to ma­nip­u­late data. For ex­am­ple:

JavaScript Object Notation

JSON be­comes lightweight uni­ver­sal ob­ject se­ri­al­iza­tion for­mat com­pared to XML. The JsonOb­ject is a con­cur­rent of very se­ri­ous and solid Json.NET li­brary used with older .NET ver­sions.

AtomPub

The Atom­Pub­Client class enables HTTP CRUD ac­cess to web re­sources us­ing the Atom­Pub pro­to­col.

2nd feed processing

Like its pre­de­ces­sor, the Syn­di­ca­tion­Client class proves sup­port for RSS 2.0 and Atom 1.0 syn­di­ca­tion for­mats.

3rd XML processing

The Xml­Doc­u­ment class in the Win­dows.Data.Xml.Dom names­pace pro­vides eas­ier that even be­fore XML doc­u­ment ma­nip­u­la­tion. In­spired by JavaScript’s HTML doc­u­ment ob­ject model, it con­tains GetEle­ment­ById and GetEle­ments­By­Tag­Name meth­ods. I sup­pose web de­vel­op­ers will love ap­proach this class pro­vides.

TPL Dataflow

The TDF, liv­ing in the Sys­tem.Thread­ing.Tasks.Dataflow names­pace, is the foun­da­tional layer for the asyn­chronous and con­cur­rent pro­gram­ming us­ing Task pro­vided in Task Par­allel Li­brary. It pro­vides a high-level ap­proach nec­es­sary to man­age large amount of data.