Book review: Applied Microsoft .NET Framework Programming

Book data

  • Titel: Applied Microsoft .NET Framework programming
  • Author: Jeffrey Richter
  • ISBN: 0-7356-1422-9

Review

The book starts off with an introduction of the .NET platform as to how it fits in the Microsoft vision of application and application development for the future. Then the book is divided into several parts.

Part 1 describs the basics of the .NET framework. After a high level view of the different pillars of the .NET platform (Assemblies, types and the FCL), you get deep into what assemblies are and how to build them. Finally you get a thorough treatment on shared assemblies and the GAC. After part 1, as with most “multi-part-things”, you get part 2. Part 2 is all about the fundamental building blocks of .NET, and .NET assemblies: types. After an introduction into the fundamentals of types (the mother of all types System.Object, casting, partitioning with namespaces), you get to know the three kind of types that exist in the .NET framework: fundamental types, reference types and value types. Finally he shows you the common object operations and how to implement them correctly for your own types.

My opinion

At several subjects in the book, Jeffrey Richter gives you his opinion on some of the design choices and language constructs chosen by Microsoft thereby not sparing his critique. But as most of the time with opinions, their are several of them, so here’s mine on:

  • Support for fundamental types and type mapping in languages. Mr. Richter prefers using the FCL type names in favour of the primitive type names of programming languages. Although I do agree with his statement for “new” languages, like C#, I have some reservations for “old” languages. The language interoperability of .NET has been used by Microsoft to promote its use, because you could (simplification) “recompile your code for .NET” and immediately start using .NET. If you don’t support primitive types for languages, this means you have to change the type of every variable in the code. Personally, I would put Richter’s recommendation in a “Source Code Standard” to be followed by all newly written code.
  • Differentiation between reference types and value types. Allthough the advice given is better than no advice, it totally depends on how your types are going to get used. And this is not always easy to predict. How do I know if my type will be returned frequently by methods? In fact, I do not understand why Microsoft makes the designer of a type decide on to how the user of a type (the developer) will allocate it. It means limiting choices for your library users by using some guidelines like the ones presented in this book, without really being able to make those choices.

In part 3 we learn to design our own types. It starts with an overview of the different kind of type members, like methods, fields, etc… and there accessibility modifiers which allow you to define their visibility inside and among assemblies and their attributes which allow you to define properties of the members, like static, virtual etc… After this overview, each subsequent chapter is an in-depth explanation of the different member types. First, constants and fields are discussed, followed by a chapter on methods, discussing things like constructors and their different treatment for value and reference types, operator overloading : implementation and pitfalls, type conversion, parameter passing, virtual methods and virtual method versioning, then a chapter on properties, discussing the why and how and finally a chapter about events showing how to define events, use events and adding custom add and remove handlers.

My opinion

  • on Operator overloading and CLS-compliance. As the author points out in a table, Microsoft has put a lot of energy into making operator overloading in C# compliant with CLS. It does show for a mentality inside Microsoft of favouring their own constructs. The way they do it is not extensible at all. If I design a new language and define some custom operators which can be overloaded by users of the language, I can never make this CLS compliant. I totally agree with the authors solution which is … (read the book).

Part 4 explains some essential types of the framework, like text, enumerations, etc… Again, each type gets a complete chapter for in-depth discussing. The chapter on text gradually builds up from characters to strings (System.String), to building strings (System.Text.StringBuilder), formatting strings, culture and strings and ends with encoding. Following is a chapter on enumerated types and bit flags, it being followed by a chapter on arrays. This last chapter discusses things like single- and multidimensional arrays, jagged arrays, casting arrays, arrays as parameters, bypassing index checking among others. The next chapter discusses interfaces and things like the difference between implementation inheritance and interface inheritance. It then continues with advice on when to use interfaces and provides a case study: plug-ins. Finally some pitfalls with interfaces like implementing different interfaces with same named methods and explicit interface method implementations are explained. The last but one chapter of this part is about attributes and explains the why of attributes and the how: defining your own , applying them and detection in code. The last chapter then discusses delegates. It starts with explaining what delegates are and proceeds with various uses of delegates, a thorough explanation of how they work inside the CLR (the C# language is just an abstraction above the real CLR implementation) and finishes with delegate manipulation and delegate chaining.

Delegates in C++

Allthough C++ doesn’t natively support delegates as Richter points out, you can get a long way as googling with the key words “C++ delegate implementation” shows.

The last part of the book is about type management and discusses exceptions, garbage collection and a short introduction to CLR hosting, AppDomains and Reflection. The chapter on exception handling starts of with a brief intro to the why of exception handling and then proceeds on the how: the try, catch and finally blocks. Following are design guidelines for your own exception classes and a discussion on exception classes: the System.Exception class, the FCL eception classes and designing your own exception classes. The chapter finishes with exception filters (in Visual Basic.NET) and how unhandled exceptions are treated in the .Net framework. The garbage collection chapter gives a very thorough treatment on the what is and the how it’s done of garbage collection. It then proceeds with explaining the integration of “destruction” in the garbage collection paradigm by discussing Finalization and The Dispose Pattern. It closes with a treatment on weak references, generational garbage collection and manipulating the garbage collector through code. The final chapter discusses CLR hosting, AppDomains and Reflection. The treatment on CLR Hosting is not very extensive but AppDomains and Reflection are treated with the same thoroughment as we’ve come to like about this book: cross AppDomain object accessing, AppDomain events, reflecting over Assemblies, Types, Typemembers, Type interfaces and dynamically creating types and invoking members.

My opinion

  • In his treatment about exceptions, the author makes a difference between handling exceptions in applications and handling exceptions in libraries. I find this split artificial: when is something a library and when is it an application? Surely the difference cannot be the file extension (“exe” vs “dll”). If I have an application which needs management of its users, is the user management functionality then “an application” or is it “a library”? You could say its part of the application, but when I start to re-use it, then it becomes a library.

Conclusion

This is a must have book for everyone serious about programming .NET. It doesn’t just tell you how to do things, but also how it works and why things are like that. It is also stuffed with vallueable design advice. It’s not a publicity campaign for Microsoft neither, as Richter is not affraid to give his views on some design decisions, and also has critique on the implementation of some constructs. It is of course up to the reader to agree or disagree.

Alltough most sample code is C# code, if a CLR construct is only supported in another language, the author provides samples in those languages. So you will find IL code and Visual Basic samples where appropriate. I would have liked a little more sample code, allthough the code in the book is clear and very well understandable. It just would have been nice if some sample applications where delivered that would provide starting points for the reader to experiment with the concepts.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s