Article

Why Use .NET?

Page: 1 2

Mobile Support

Mobile devices are becoming an extremely popular method of accessing information on the Web. .NET gives you the tools to support over 200 different mobile devices from a single source base. Having analysed any device that requests your page, the Mobile Internet Toolkit (MIT) automatically dishes out your pages in different mark-up (WML, HTML 3.0) and in different form factors (screen size, card sizes) appropriate to the device in question.

This means that if we access our page through a WAP browser, we'd be served a WML output, paginated to our mobile phone's display size and network connectivity. However, if we accessed the very same page through a PDA, we'd be served an HTML output, with much more information on each page taking into account of the extra features and larger screen size our PDA has over the mobile phone.

Data Access

One area in which .NET really shines is that of its extendible data access components, known as ADO.NET. ADO.NET provides a high level interface to virtually every database that exists, with generic connectivity through ODBC and OLE DB, as well as specialised and optimised connectivity for SQL Server, Oracle and MySQL (through a third party component).

With ADO.NET, all data is transported in XML, allowing for great interoperability between data providers and applications. This also allows XML files to be used for data storage using the same structures and access techniques as traditional databases.

ADO.NET uses a disconnected model, which means that applications work with data locally, only contacting the server for data transfer or update. This means your application isn't as reliant on the performance of the database, improving performance and the scalability of applications by removing bottlenecks.

With these enhancements comes a productivity boost. While data access still isn't "plug and play" -- that is, you still need to deal with SQL statements, data structures, and to a certain extent, deal with your database -- it does introduce easy-to-use methods for accessing data. For example, here's the code to pull a set of rows from a table on a running SQL Server and display the first field in each row:

SqlConnection conn = new SqlConnection(connectionString);  
SqlDataAdapter a = new SqlDataAdapter("select * from mytable;", conn);  
DataSet s = new DataSet();  
a.Fill(s);  
foreach (DataRow dr in s.Tables[0].Rows)  
{  
 Console.WriteLine(dr[0].ToString());  
}

Anyone who's dealt with traditional ADO or other data access objects will notice how clean and methodical the access is.

We can also, at any point, read and write XML from our DataSet using the ReadXML and WriteXML methods. So, to extend the earlier example, we can now output the results of our query in structured XML to a file fileName:

SqlConnection conn = new SqlConnection(connectionString);  
SqlDataAdapter a = new SqlDataAdapter("select * from mytable;", conn);  
DataSet s = new DataSet();  
a.Fill(s);  
s.WriteXml(fileName);

Language Preference

.NET is language neutral. As mentioned at the start of this article, all .NET compilers interpret and compile your code to the same base language, IL (intermediate language, akin to Java bytecode) to run on the CLR (Common Language Runtime). This means you can happily use Visual Basic.NET and receive the same performance and functionality of those traditionally more powerful languages like C++. In essence, you can choose the language you use.

As with spoken languages, the availability of information in the form of example differs from language to language. While you may be able to read and express anything you would wish to in Esperanto, to read the majority of content on the Web, you'll still need a decent grasp of English. This is similar with .NET. You may choose Eiffel.NET as the language with which to develop your applications, yet most examples and references you'll find will be coded in either C# or VB.NET. To apply these to Eiffel, you'll need an understanding of these languages as well.

So, what are the choices? C# is a language developed by Microsoft specifically for .NET. Taking a similar syntax style to that of Java, C# is recognised as a clean, modern language and, with its similarity to Java, is a great bridge from Java to .NET. Visual Basic .NET (VB.NET) extends Visual Basic 6 to offer the power of object orientated programming within .NET. In a nutshell, anyone who's already familiar with VB6 will have little problem moving up to VB.NET.

Let's have a look at some example code: a class written in C#, and its equivalent in VB.NET:

namespace sitepoint.csharp  
{  
 public class HelloWorld  
 {  
   public string sayHello(string name)  
   {  
     return "Hello "+name+"!";  
   }  
 }  
}  
 
Namespace sitepoint.visualbasic  
 Public Class HelloWorld  
   Function sayHello(ByVal name As String) As String  
     Return "Hello " + name + "!"  
   End Function  
 End Class  
End Namespace

A single project can even use different classes for different files! This language tolerance also means we can reuse code developed with different languages without the headaches of the past. No marshalling, no COM, just transparent language interoperability courtesy of .NET.

Web Services

Web services aren't new or unique to .NET. In fact, that is the whole point of a Web service -- that it uses standards to enable cross-platform, cross-development execution of code. However, the way in which .NET simplifies the creation of Web services is certainly a great string in its bow.

A Web service is essentially a collection of methods that can be accessed via HTTP and SOAP. You can, as with any method, have a custom object return type, or pass custom objects to the method, which means you can distribute parts of your application very simply.

An example: below we have the Hello World example class used earlier in this article:

namespace sitepoint.csharp  
{  
 public class HelloWorld  
 {  
   public string sayHello(string name)  
   {  
     return "Hello "+name+"!";  
   }  
 }  
}

At the moment, to use the sayHello method, we must use it within .NET and have the compiled assembly available on our machine.

By exposing the method sayHello as a Web service, however, we can host the code on a network, far away from our machine, and also allow developers of other languages (not necessarily .NET languages!) to use our functionality.

Exposing the method as a Web method is simplicity itself. We just add a WebMethod attribute to the method and inherit behavior from the WebService class:

using System.Web.Services;  
 
namespace sitepoint.csharp  
{  
 public class HelloWorld: WebService  
 {  
   [WebMethod]  
   public string sayHello(string name)  
   {  
     return "Hello "+name+"!";  
   }  
 }  
}

After linking this to a service page (a one-line instruction contained within a .asmx file), we have exposed this method and its functionality on the Web, ready for all to use (or consume).

When you consume a Web service, you create a proxy class that contains the methods exposed by the service, and define classes for the types used within the methods. This proxy class means you can treat the Web service as if it were local to your application: it automatically handles the remote calls to the service.

If you've identified functionality in your code that you feel could benefit from being distributed (heavy computational tasks, for example), which could be sold as a service to third parties, or which might require high levels of maintenance, Web services will help ease deployment headaches while opening new avenues for sale. It's a win-win situation.

It's By Microsoft

This might be a point against .NET in your books, and if you're wishing to write cross-platform software, it certainly is. But whatever your personal view of the corporation, one fact remains: Microsoft has the money to innovate and market their products like no one else in the industry. This means that .NET isn't disappearing any time soon and, as with every Microsoft development framework, there are heaps of freely available guides, ebooks and reference libraries to help us developers.

Previews of Microsoft's next generation operating system Longhorn show how the skills learnt from developing .NET today will be directly transferable to Longhorn development tomorrow. This almost guarantees the worth of .NET skills for the foreseeable future, something that's normally very difficult to say in the rapidly evolving computer industry.

Of course, with the positives come certain negatives, most principally the lack of full cross-platform support with .NET. While there is nothing stopping .NET from making its way to Linux technically (and projects like Mono show just how true this is), there is no drive from Microsoft to support .NET on alternative operating systems, because of the obvious conflict of interest with Windows, the market where Microsoft makes the bulk of its money. While this may rule out .NET development for some, in reality, products like Windows Server 2003 now remove many of the obstacles to Windows server adoption such as the concerns around security, reliability and performance that existed in the past.

It's Cheap!

Not a statement that can be usually directed at Microsoft, particularly in comparison with its open source rivals, but pound for pound, dollar for dollar, .NET offers great value for money.

To get started with .NET, all you need is a box with a modern version of Windows installed (ASP.NET requires IIS 5.0 or above, meaning Windows 2000 or XP Pro are required for serving Web applications). The framework itself is free, and Microsoft's strong support for .NET means a decent IDE in the form of Web Matrix is available for free; however, as with most development frameworks, to write code, all you need is Notepad.

And, of course, there is the mighty Visual Studio .NET. By requiring a hefty license fee, it can't be called cheap, but the productivity gains and project organisation it introduces means Visual Studio .NET can pay for its license several times over in a single development process.

As mentioned in the data access section of this article, .NET supports virtually every database available today, including open source, freely available databases like MySQL. However, Microsoft also has a free database in the form of MSDE, a developer edition of their SQL Server database. While Microsoft still will not allow commercial use of the database, MSDE provides a powerful, easy to administer database that can be directly scaled to SQL Server.

For more information on where to get the components you need, and a comprehensive guide on how to install them, see the first chapter of "Build Your Own ASP.NET Web Site Using C# & VB.NET".

Prices for Web hosting have also been steadily decreasing since .NET's release. Large hosts like InterLand and 1&1 now offer fully-featured .NET packages at similar prices to traditional LAMP (Linux-Apache-MySQL-PHP) configurations.

The Future

Microsoft is in no way finished with .NET yet. Whidbey, due for release next year, includes countless enhancements and new additions, including object relational mapping, simplified user management, themeing, and much more besides. Read the Preparing for Whidbey article for more information on what to expect.

Go For It

Have I whetted your appetite? Then go try it for yourself! And with Build Your Own ASP.NET Web Site Using C# & VB.NET as your friendly guide along the way, you'll be producing powerful .NET Web applications in no time.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Rate This Article

  • 1
    Poor
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
    Great

Post A Comment

You need to be a member of the SitePoint Forums to comment on this post. Sign Up

Already a member? Post using your SitePoint Forums account: