译:《Professional C# 4.0 and .NET 4》 第一章 .Net体系架构(3 over)

以下内容原文见《Professional C# 4.0 and .NET 4》 -> Part1: The C# Language -> Chapter 1: .NET Architecture
原文作者:Christian Nagel, Bill Evjen, Jay Glynn, Karli Watson, Morgan Skinner
翻译:Sam.Sha – ycoder.com
备注:只翻译UI技术相关的,不包含ASP

使用C#创建应用程序

你可以使用C#创建控制台程序:运行于dos窗口的纯文字应用程序,这种程序可以用于单元测试或者类似UNIX或Linux中的守护程序。但通常,你可以使用.NET技术创建出更丰富的应用程序,这个章节我们就概要的介绍这些不同类型的应用程序

创建ASP.NET应用程序 (译者注:因为本人关注UI技术,这部分省略了)

The original introduction of ASP.NET 1.0 fundamentally changed the web programming model. ASP.NET 4 is a major release of the product and builds upon its earlier achievements. ASP.NET 4 follows on a series of major revolutionary steps designed to increase your productivity. The primary goal of ASP.NET is to enable you to build powerful, secure, dynamic applications using the least possible amount of code. As this is a C# book, there are many chapters showing you how to use this language to build the latest in web applications.

The following section explores the key features of ASP.NET. For more details, refer to Chapters 40, “Core ASP.NET,” 41, “ASP.NET Features,” and 42, “ASP.NET MVC.”

features of ASP.NET

First, and perhaps most important, ASP.NET pages are structured. That is, each page is effectively a class that inherits from the .NET System.Web.UI.Page class and can override a set of methods that are evoked during the Page object’s lifetime. (You can think of these events as page-specific cousins of the OnApplication_Start and OnSession_Start events that went in the global.asa files from the classic ASP days.) Because you can factor a page’s functionality into event handlers with explicit meanings, ASP. NET pages are easier to understand.

Another nice thing about ASP.NET pages is that you can create them in Visual Studio 2010, the same environment in which you create the business logic and data access components that those ASP.NET pages use. A Visual Studio 2010 project, or solution, contains all the files associated with an application. Moreover, you can debug your classic ASP pages in the editor as well; in the old days of Visual InterDev, it was often a vexing challenge to configure InterDev and the project’s web server to turn debugging on.

For maximum clarity, the ASP.NET code-behind feature lets you take the structured approach even further. ASP.NET allows you to isolate the server-side functionality of a page to a class, compile that class into a DLL with the other pages, and place that DLL into a directory below the HTML portion. A @Page directive at the top of the page associates the file with a class. When a browser requests the page, the web server fires the events in the class in the page’s class file.

Last, but not least, ASP.NET is remarkable for its increased performance. Whereas classic ASP pages are interpreted with each page request, the web server caches ASP.NET pages after compilation. This means that subsequent requests of an ASP.NET page execute more quickly than the first.

ASP.NET also makes it easy to write pages that cause forms to be displayed by the browser, which you might use in an intranet environment. The traditional wisdom is that form-based applications offer a richer user interface but are harder to maintain because they run on so many different machines. For this reason, people have relied on form-based applications when rich user interfaces were a necessity and extensive support could be provided to the users.

Web forms

To make web page construction even easier, Visual Studio 2010 supplies Web Forms. They allow you to build ASP.NET pages graphically in the same way that Visual Basic 6 or C++ Builder windows are created; in other words, by dragging controls from a toolbox onto a form, then flipping over to the code aspect of that form and writing event handlers for the controls. When you use C# to create a Web Form, you are creating a C# class that inherits from the Page base class and an ASP.NET page that designates that class as its code-behind. Of course, you do not have to use C# to create a Web Form; you can use Visual Basic 2010 or another .NET-compliant language just as well.

In the past, the difficulty of web development discouraged some teams from attempting it. To succeed in web development, you needed to know so many different technologies, such as VBScript, ASP, DHTML, JavaScript, and so on. By applying the Form concepts to web pages, Web Forms have made web development considerably easier.

Web server Controls

The controls used to populate a Web Form are not controls in the same sense as ActiveX controls. Rather, they are XML tags in the ASP.NET namespace that the web browser dynamically transforms into HTML and client-side script when a page is requested. Amazingly, the web server is able to render the same server- side control in different ways, producing a transformation appropriate to the requestor’s particular web browser. This means that it is now easy to write fairly sophisticated user interfaces for web pages, without worrying about how to ensure that your page will run on any of the available browsers — because Web Forms will take care of that for you.

You can use C# or Visual Basic 2010 to expand the Web Form toolbox. Creating a new server-side control is simply a matter of implementing .NET’s System.Web.UI.WebControls.WebControl class.

XMl Web services

Today, HTML pages account for most of the traffic on the World Wide Web. With XML, however, computers have a device-independent format to use for communicating with each other on the Web. In the future, computers may use the Web and XML to communicate information rather than dedicated lines

and proprietary formats such as Electronic Data Interchange (EDI). XML Web services are designed for a service-oriented Web, in which remote computers provide each other with dynamic information that can be analyzed and reformatted, before final presentation to a user. An XML Web service is an easy way for a computer to expose information to other computers on the Web in the form of XML.

In technical terms, an XML Web service on .NET is an ASP.NET page that returns XML instead of HTML to requesting clients. Such pages have a code-behind DLL containing a class that derives from the WebService class. The Visual Studio 2010 IDE provides an engine that facilitates web service development.

An organization might choose to use XML Web services for two main reasons. The first reason is that they rely on HTTP; XML Web services can use existing networks (HTTP) as a medium for conveying information. The other is that because XML Web services use XML, the data format is self-describing, nonproprietary, and platform-independent.

创建Windows Forms

虽然C#和.NET特别适合与Web项目的开发,但他们同样也适合创建所谓的胖客户端或者粗客户端程序——这种程序所有内容都安装在终端用户机器上,这种支持形式被称为:Windows Forms,这是.NET从VB6中的Form借鉴过来的。在设计图形界面时,你可以简单的从工具箱中拖拽控件到窗体中,为给窗体添加行为,你可以为控件编写事件处理代码,一个Windows Form工程打包成一个可执行程序,必须伴随着.NET执行器安装在终端用户电脑上,如同其他的.NET工程类型,WIndows Form工程同时支持C#和VB2010,在39章节中会介绍更多”Windows Forms”

使用WPF( Windows Presentation foundation)

这块儿最新的技术是WPF( Windows Presentation Foundation),WPF使用XAML组件应用程序,XZML的全称是:可扩展的应用程序标记语言,这是微软体系中的一种全新的创建应用程序的方式,在2006年作为.NET Framework 3.0, 3.5, 4的一部分被引入,这意味着要运行WPF程序需要客户端安装.NET Framework 3.0以上版本,目前支持WPF应用程序的操作系统有Windows XP, WIndows Vista, Windows 7, Windows Server 2003, Windows Server 2008。

XAML是一种XML表述方式,用于创建WFP程序中所有可见的部分和行为,当然使用编程的方式也可以创建WPF程序。WPF采用的这种声明式编程方式,是行业的发展趋势,声明式编程椅蹲湃〈ü嘁胗镅裕–#,VB或者Java)编程创建对象,而可以通过XML编程方式声明任何的东西,35章“Core WPF”中将详细介绍使用XAML和C#的这些全新类型的创建应用程序的方式。

Windows 控件

虽然Web Forms和Windows Forms开发相似,但使用的是不同类型的控件填充,Web Forms使用web server 控件,而Windows Forms使用Windows控件。

一个Windows控件与一个ActiveX控件很相似,在一个Windows控件被实现后,他会被编译成一个DLL并需要安装到客户端机器上,实际上.NET SDK提供了一个创建ActiveX控件的包装工具,这样他就可以放置在Windows Forms中了。与Web控件相似,Windows控件的创建也从一个特殊的类派生:System.Windows.Forms.Control

Windows services

A Windows Service (originally called an NT Service) is a program designed to run in the background in Windows NT/2000/XP/2003/Vista/7 (but not Windows 9x). Services are useful when you want a program to be running continuously and ready to respond to events without having been explicitly started by the user. A good example is the World Wide Web Service on web servers, which listens for web requests from clients.

It is very easy to write services in C#. .NET Framework base classes are available in the System .ServiceProcess namespace that handles many of the boilerplate tasks associated with services. In addition, Visual Studio .NET allows you to create a C# Windows Service project, which uses C# source code for a basic Windows Service. Chapter 25, “Windows Services,” explores how to write C# Windows Services.

Windows Communication foundation

Looking at how you move data and services from one point to another using Microsoft-based technologies, you will find that there are a lot of choices at your disposal. For instance, you can use ASP.NET Web services, .NET Remoting, Enterprise Services, and MSMQ for starters. What technology should you use? Well, it really comes down to what you are trying to achieve, because each technology is better used in a particular situation.

With that in mind, Microsoft brought all these technologies together, and with the release of the .NET Framework 3.0 as well as its inclusion in the .NET Framework 3.5 and 4, you now have a single way to move data — the Windows Communication Foundation (WCF). WCF provides you with the ability to build your service one time and then expose this service in a multitude of ways (under different protocols even) by just making changes within a configuration file. You will find that WCF is a powerful new way of connecting disparate systems. Chapter 43, “Windows Communication Foundation,” covers this in detail.

Windows Workflow foundation

The Windows Workflow Foundation (WF) was really introduced back with the release of the .NET Framework 3.0, but has had a good overhaul that many will find more approachable now. You will find that Visual Studio 2010 has greatly improved as far as working with WF and makes it easier to construct your workflows. You will also find a new flow control, the Flowchart class, as well as new activities such as DoWhile, ForEach, and ParallelForEach.

WF is covered in Chapter 44, “Windows Workflow Foundation 4.”

C#在.NET企业体系结构中的职责

C#需要.NET运行器的支持,这在几年前或许是个问题,但现在大多数家用电脑都安装了.NET环境,与此同时,安装一个C#应用程序同时以为着安装了一个.NET二次组件,因此,很多C#应用程序首先出现在企业应用环境,可以说C#是一个非常好的机会,用以组建多层客户端-服务端应用程序

When combined with ADO.NET, C# has the ability to quickly and generically access data stores such as SQL Server and Oracle databases. The returned datasets can easily be manipulated using the ADO.NET object model or LINQ, and automatically render as XML for transport across an office intranet.

After a database schema has been established for a new project, C# presents an excellent medium for implementing a layer of data access objects, each of which could provide insertion, updates, and deletion access to a different database table.

Because it’s the first component-based C language, C# is a great language for implementing a business object tier, too. It encapsulates the messy plumbing for intercomponent communication, leaving developers freeto focus on gluing their data access objects together in methods that accurately enforce their organizations’ business rules. Moreover, with attributes, C# business objects can be outfitted for method-level security checks, object pooling, and JIT activation supplied by COM+ Services. Furthermore, .NET ships with utility programs that allow your new .NET business objects to interface with legacy COM components.

To create an enterprise application with C#, you create a class library project for the data access objects and another for the business objects. While developing, you can use Console projects to test the methods on your classes. Fans of extreme programming can build Console projects that can be executed automatically from batch files to unit test that working code has not been broken.

On a related note, C# and .NET will probably influence the way you physically package your reusable classes. In the past, many developers crammed a multitude of classes into a single physical component because this arrangement made deployment a lot easier; if there was a versioning problem, you knew just where to look. Because deploying .NET enterprise components involves simply copying files into directories, developers can now package their classes into more logical, discrete components without encountering “DLL Hell.”

Last, but not least, ASP.NET pages coded in C# constitute an excellent medium for user interfaces. Because ASP.NET pages compile, they execute quickly. Because they can be debugged in the Visual Studio 2010 IDE, they are robust. Because they support full-scale language features such as early binding, inheritance, and modularization, ASP.NET pages coded in C# are tidy and easily maintained.

Seasoned developers acquire a healthy skepticism about strongly hyped new technologies and languages and are reluctant to use new platforms simply because they are urged to. If you are an enterprise developer in an IT department, though, or if you provide application services across the World Wide Web, let us assure you that C# and .NET offer at least four solid benefits, even if some of the more exotic features such as XML Web services and server-side controls don’t pan out:

➤ Component conflicts will become infrequent and deployment is easier because different versions of the same component can run side by side on the same machine without conflicting.

➤ Your ASP.NET code will not look like spaghetti code.

➤ You can leverage a lot of the functionality in the .NET base classes.

➤ For applications requiring a Windows Forms user interface, C# makes it very easy to write this kind of application.

Windows Forms have, to some extent, been downplayed due to the advent of Web Forms and Internet-based applications. However, if you or your colleagues lack expertise in JavaScript, ASP, or related technologies, Windows Forms are still a viable option for creating a user interface with speed and ease. Just remember to factor your code so that the user interface logic is separate from the business logic and the data access code. Doing so will allow you to migrate your application to the browser at some point in the future if you need to. In addition, it is likely that Windows Forms will remain the dominant user interface for applications for use in homes and small businesses for a long time to come. In addition to this, the new smart client features of Windows Forms (the ability to easily work in an online/offline mode) will bring a new round of exciting applications.

总结

This chapter has covered a lot of ground, briefly reviewing important aspects of the .NET Framework and C#’s relationship to it. It started by discussing how all languages that target .NET are compiled into Microsoft Intermediate Language (IL) before this is compiled and executed by the Common Language Runtime (CLR). This chapter also discussed the roles of the following features of .NET in the compilation and execution process:

本章涉及了大量.NET Framework 基础,重要方面的概要以及与C#的关系。

➤ 程序集和.NET基础类库

➤ COM 组件

➤   JIT 编译

➤   应用程序域

➤   垃圾回收

Figure 1-4 provides an overview of how these features come into play during compilation and execution.

你学习了IL的特性,尤其是他的强制类型和面向对象,以及这些特性如何影响针对.NET的编程语言,包括C#,你还学习了IL的强制类型如何确保语言的互用,还有CLR服务比如垃圾回收和安全,还有一个重点是通用语言规范(CLS)和通用类型系统(CTS)对于语言互用性的帮助

最后,你学习了C#如何用于创建基本的应用程序,这些程序基于.NET技术,包括ASP.NET。

第2章节将讨论如何编写C#代码


+ 六 = 7