Thursday, October 23, 2008

Introduction to the .NET Base Classes


The .NET Base Classes are the means by which you can access much of the core functionality of Windows, as well as perform operations such as data access from .NET code. It consists of a huge number of classes that Microsoft have written to carry out these operations, and which you can call in your code. You can even write classes that inherit from the base classes.

What's a .NET class?

If you've not encountered the term before you may wonder exactly what a .NET class is. This is only an introductory article, so we won't give an exact definition here, but depending on your previous programming experience you can think of it as follows:
For C++ Developers: Think of a .NET class as like a C++ class, except that it's packaged up in a way that means you can easily call it up or inherit from it in any .NET-aware language, including C++, VB and C#.
For VB Developers: Think of a .NET class as like an ActiveX control (with or without a user interface), but which is a lot easier to use. You can also inherit from a .NET class, which means you can write your own class that is based on it, but in which you override the implementations of some methods.

What Do the Base Classes Do?

The number of base classes is huge, so there's no way that we can cover everything here. But here's a quick summary of the kind of things that can be done with them.


» Strings. String handling.
» Collections and Arrays. There are all sorts of permutations for storing sets of objects: Arrays, Lists, Maps, Linked lists. All implemented in highly efficient ways.
» WinForms. Classes to display windows and controls - text boxes, combo boxes, listboxes, file dialogs etc., and to react to events on those controls (Windows messages in low-level C++ parlance).
» WebForms. Classes that do roughly the same as many of the WinForms classes, but are designed to work over the Internet: Instead of creating the relevant control, they generate all the HTML needed to get a web browser to display something that looks and behaves like the control in question. You can use WebForms in your ASP.NET pages.
» File Handling. You can use .NET base classes to navigate around the file system on your machine or network, to check the properties of files, read, modify and write to files, and move and copy files and folders around.
» Registry Access. Base classes are available that allow you to navigate, read, and modify the contents of the registry.
» Internet. Using the base classes you can connect to the Internet and download and upload files.
» ADO.NET. The ADO.NET classes allow you to connect to a data source and read or modify it - much the same abilities as are offered by the existing ADO, except that ADO.NET is designed much more around the concept of disconnected data, and makes heavy use of XML for data transmission.

There's a lot more than this available, but this taster should give you an idea.

Introduction to .NET

Introduction to .NET

The .NET framework has been plugged by Microsoft as a new programming model for the Internet age. Personally I prefer to think of it as a new programming model for almost everything that is Windows related.

Seriously, if you are working doing Windows programming, then I believe .NET is going to be well worth learning for future projects, simply because it will make programming so much easier.

So what is .NET?

There are lots of ways to think about .NET, but here's the way I prefer:

Over the last 10 years or so, Microsoft have gradually been improving the Windows platform, and the associated APIs and developer tools. So we've seen for example the emergence of COM, then DCOM, then COM+ to enable reuse of software. For Data Access, we had first ODBC, then OLE DB and ADO in various versions. For dynamic web sites we had different versions of ASP. And for programming there were a variety of languages, C++, VB, scripting languages etc.

One problem with this is that as the tools and languages have got more sophisticated they have also got more complex, due to the need to support earlier tools or versions of the tools. So for example, these days, COM is an extremely powerful way to package up code for reuse, but if you want to use COM effectively the learning curve is frightening. I've routinely heard people talk about 6 months as the minimum time for an experienced developer to learn COM. And if you want to master DCOM or COM+, you've got to learn COM first! Add to this the fact that many people have (rightly in my view) complained that many of these APIs and tools are in some ways badly designed, and you can see why Microsoft has had such a poor reputation amongst many developers up until now.

.NET appears to be a serious attempt to change all that. Rather than just updating the existing tools and languages (and so making them even more complex), Microsoft have started from the ground up again, and developed a completely new framework, within which most programming tasks can be easily accomplished. (And they haven't destroyed backwards compatibility. Your old code will work fine, it's just that new code can be written more easily). Not only that, but .NET has had a very favourable reaction on newsgroups - there seems to be a general consensus that .NET is well designed, and Microsoft are seriously listening to the needs of developers as far as .NET is concerned.

What does .NET give you?

Quite a lot is the answer. Here's the main points:
  • Most things that you might want to do on Windows, including data access, windowing, connecting to the Internet, and much of the functionality of the Win32 API is now accessible through a very simple object model.
  • The VB language has been hugely upgraded, so it now includes classes and most of the features previously accessible in C++.
  • A new language, C#, has been introduced, which combines the efficiency of C++ with some of the ease of development of VB.
  • Memory management for .NET applications is much more sophisticated, meaning that a badly behaved .NET component is extremely unlikely to crash other components running in the same process.
  • ASP.NET, the replacement for ASP, offers compiled web pages (making processing of web requests much more efficient) and includes a large number of pre-written components that can generate commonly used HTML form and user-interface items for you (the so-called server controls.
  • The main programming languages have been moved far closer together, so code written in VB, C++ and C# can be intermixed: For example you can write a class in VB, derive a class in C# (or C++), and freely step between the languages in the debugger.
  • Components are wrapped up in a new unit called an assembly, which is highly self-describing, making installation and use of components very easy.

How does .NET work?

The most significant aspect of the .NET architecture is that code in VB, and C# is compiled not to native executable code, but to an Intermediate Language (IL), with the final step of converting to native executable normally happening at runtime. Such code is termed managed code C++ code can optionally be marked as managed code, in which case it gets compiled to IL too. This makes your C++ code interoperable with VB and C# and allows you to take advantage of all the .NET features, but does restrict you to not using some features of C++ (such as multiple inheritance) that are not supported on .NET.

The principle of IL clearly owes something to the ideas of Java, which was also compiled in two stages. (And the resemblence between C# and Java is very strong). However, with Java the emphasis is on cross-platform independence, while .NET is geared to Windows, and has an emphasis on cross-language (rather than cross-platform) independence. Having said that, Microsoft have left open the possibility of .NET being developed on other platforms. Microsoft also appear to be convinced that the existence of IL won't substantially hurt performance of applications, and may even improve it (because at the final stage of compilation, the compiler knows what processor is going to run the code, and so can take advantage of hardware features specific to that processor - this is not the case with direct compilation of source code into executable). Whether that belief will turn out to be correct has yet to be tested.

Where Can I Get .NET?

The .NET framework has not yet been release. Most likely date at present seems to be sometime in late 2001. The Beta1 version has however been made available. MSDN Universal subscribers will receive it with their MSDN subscriptions, and for non-MSDN subscribers you can register to receive a copy for the cost of carriage, at http://msdn.microsoft.com/vstudio/nextgen/beta.asp.

Monday, October 20, 2008

framework

Q: What is an object-oriented framework?

A: The short answer is: A framework is a set of related classes which you specialize and/or instantiate to implement an application or subsystem.

Q: What is the difference between an object-oriented framework and a class library?

A: An object-oriented framework is a kind of class library. The distinguishing feature is that when the library is a framework the flow of control is bi-directional between the application and the library. This feature is achieved by the dynamic binding in object-oriented languages where an operation can be defined in a library class but implemented in a subclass in the application.

Q: Are frameworks a kind of GUI technology?

A: No. The first frameworks, such as Model/View/Controller, were developed for graphical user interfaces, but the technique is general and you can now find frameworks for many different domains.

Q: What is so good about frameworks?

A: Two main reasons. The framework is not just a collection of classes but also defines a generic design. When you use the framework your re-use that design and save time and effort. Secondly, because of the bidirectional flow of control the framework can contain much more functionality than a traditional library regradless if it is a procedural or class library. Having to write less code also saves you time and effort.

Q: If those are the good news, which are the bad ones?

A: The main obstacle to frameworks is the learning curve. To reuse a design you have to learn and understand it. This is never easy if the domain is non-trivial. Furthermore, no one has yet come up with a real good way to document a framework.

Q: Can I develop an object-oriented framework in C?

A: Not easily, since frameworks rely heavily on dynamic binding. In C you would have to implement that by hand and combine it with strict programming conventions.

Q: What is the first step towards framework based development?

A: Before you even start to think about develop a framework you must learn how to use one. Get a commercially or publicly available framework and start to develop applications from it.

Q: What is the most difficult thing to learn when starting to use frameworks?

A: That you have to turn the way you think about programming inside out. Frameworks follow the Hollywood principle: "Don't call us, we'll call you". The library code is in charge and as an application programmer you just provide various bits and pieces and is no longer in control. It is like programming event driven applications, but one step further.

Q: Can I use more than one framework in same application?

A: Historically frameworks were monolithic and were not easy to combine. But the current trend is to make smaller frameworks, one framework for each part or aspect of an application rather than a single framework for the whole application.

Q: Which of the available OO methods should I choose for developing a framework?

A: None. All of the established methods build on the traditional one way flow of control. They support inheritance between classes but are very weak on dynamic binding. You can use the notation to describe different parts of the framework but the method itself can do more harm than good.

Q: What is the relationship between frameworks and design patterns?

A: Frameworks are software whereas patterns are knowledge or information about software. Patterns can be used to describe frameworks and some part or aspect of a framework can be a realization of a pattern.

Q: When should I develop a framework of my own?

A: Developing a framework is somewhat like developing a scientific theory for some area. Developing a framework is hard and you should do that only for your own special area of competence.

Q: Is the development process different when you use a framework?

A: Sure. The problem has to be cast into the generic template imposed by the framework. That may at first appear to be a limitation, but trying to express the problems in terms of a previously successful design is often the best way to understand the user's real needs.