Advancements for Common Application Scenarios
You can identify different options for upgraded .NET application advancement depending on the category of the application; this chapter is mainly focused on two of them: Windows applications and forms smart client, and business applications (.NET enterprise services). You will learn how to take advantage of new technologies and updated features, such as .NET ClickOnce deployment, the VB.NET Docking and Anchoring forms control property, the VB .NET Error Provider control, the FlowLayoutPanel and the TableLayoutPanel. The suggestions provided should help you make the most of the Visual Basic .NET and .NET Framework in general from an application-category perspective.
What are Windows Applications and Windows Forms Smart Clients?
Smart clients are easily deployed and managed client applications that provide an adaptive, responsive and rich interactive experience by leveraging local resources and intelligently connecting to distributed data sources. They are connected systems (usually through the Internet) that allow the user’s local applications to work together with remote applications. They have the ability to work offline, the capability to be deployed and updated in real time over the network from a centralized server, to support multiple platforms and languages because they are built on Web Services, and to run on almost any device that has Internet connectivity.
What characteristics differentiate smart clients from traditional rich client applications?
If a client application displays these 3 features, it can be said to be a smart client:
- Utilizes local resources: It has code statements on the client that enable local resources to be utilized. They may take advantage of the local CPU or GPU, local memory or disk, or any local devices connected to the client, such as a telephone or bar-code/RFID reader. It may also take advantage of local software, such as Microsoft Office applications, or any installed applications that interact with it.
- Connected: Smart clients are never standalone and always form part of a larger distributed solution. This could mean that the application interacts with a number of Web services that provide access to data or a line-of-business (LOB) application.
- Offline capable: Smart clients work whether or not they are connected to the Internet, because they are running on the local computer.
What architecture advancements can be applied to Windows-based applications?
- Intelligent Install and Update: The .NET Framework enables applications to be deployed using a variety of techniques, including simple file copy or download over HTTP. Applications can be updated while running and they can be deployed on demand by clicking a URL. It also provides a powerful security mechanism that guarantees the integrity of the application and its related assemblies.
- Self-Update and ClickOnce: ClickOnce is a new application deployment technology that makes deploying a Windows Forms application as easy as deploying a Web application. It is as simple as clicking a link on a Web page. To deploy or update an application, administrators have to only update files on a server; there is no need to individually touch every client. ClickOnce applications can be deployed through Web servers, file servers, DVDs, and so on. They can also be installed. When they are installed, Start menu and Add/Remove program entries are created or they run and are stored in cache. Furthermore, ClickOnce has several ways that it can be configured to automatically check for application updates. Alternatively, applications can use the ClickOnce APIs (System.Deployment) to control when updates should happen.
- Client Device Flexibility: The .NET Framework together with the .NET Compact Framework provides a common environment on which smart client applications can be built. Often, there will be multiple versions of smart clients, where each smart client targets a specific device type and takes advantage of the device’s unique features and provides functionality that is appropriate to its usage.
How has the resizing event been improved in Visual Basic .NET?
Visual Basic 6.0 requires you to write custom code for the Resize event to make sure that controls are correctly displayed on the resized form. This makes it possible for any resizable application to retain the original placement of controls on the form regardless of the size of the form itself.
In VB .NET you can accomplish the same result by working with the Docking and Anchoring properties of the controls in your forms. A control’s Anchor property can be modified so that it is dynamically positioned on screen. When a control is anchored to a form, and the form’s size changes, the control’s position on the form remains the same relative to the anchor position(s) it holds. The Docking property of a control allows it to adhere to the edge of the form or the container in which it resides.
What’s the advantage of using .NET intrinsic controls?
Many controls in Visual Basic 6.0 that enhanced the GUI of an application were available by means of importing ActiveX libraries. Many of these components are automatically migrated to .NET by the Visual Basic Upgrade Wizard, but not to their intrinsic counterparts in Visual Basic .NET. Instead, they are converted to ActiveX components.
Note: there’s an enhanced enterprise version of this migration tool, called the Visual Basic Upgrade Companion, that converts additional commonly used ActiveX controls to Windows Forms controls that offer similar functionality, and promotes the use of .NET native libraries when possible, instead of VB Compatibility libraries.
Even though most of the ActiveX controls upgraded from VB 6.0 will work fine when upgraded to VB .NET, you might be better off substituting them for native Windows Forms controls. You can make your project easier to maintain by removing references to ActiveX controls that are not imperative to use. Furthermore, by removing the references, you do not have to worry about their distribution and registration when they are installed on the user’s machine. In most cases, the task of programming native Windows controls is easier than everything that is involved with ActiveX control programming.
How has input validation been improved in Visual Basic .NET?
Requesting input from a user in your application always involves the risk of a user entering invalid information, so you must have input validation in your code to alert the user. In Visual Basic 6.0, the most common way to perform this task was to use the MsgBoxfunction. The biggest drawback with using a message box to display an error is that in some occasions, a text message is insufficient. For example, if a form has many input fields and there are several errors made, chances are that the user will not remember all the errors after the message box is closed. This means that the user will have to be reminded of the errors more than once to correct the problems with the input.
In Visual Basic .NET, the ErrorProvider control will display an icon to let the user know exactly what and where the problem is. When the mouse is placed over the error notification icon, a ToolTip will be displayed to further assist with the error resolution procedure.
How has control placement been made easier in Visual Basic .NET?
In Visual Studio 2005, two new containers are available that facilitate control placement: the FlowLayoutPanel and the TableLayoutPanel. These controls differ from the Panel container control that was previously available in the way in which controls are placed within it. The two new controls offer full customization for how and where you would like your contained controls to be displayed.
The FlowLayoutPanel makes it easy to automatically position controls when they are added to the container. When controls are added to the panel, they will flow horizontally and vertically to fit within the container. This makes it quick in situations where you would like to deal with perfectly aligned tuples of several controls (for example, several text boxes and labels). You can change the order in which controls flow by changing the values of the FlowDirection property.
The TableLayoutPanel control offers a row-column approach by which to arrange your controls. A noticeable difference is that only one control can be placed in each of the cells in the panel.
Does the .NET Framework support the use of Windows themes throughout the system?
Applications created with the .NET Framework version 1.0 will not support themes, but starting with the version 1.1, Windows XP visual styles are available. This behavior will be supported when Visual Studio 2005 ships because every form that is created will support themes by default.
Changing your Visual Basic .NET application’s code so that it supports themes can be done by following two approaches: invoking the EnableVisualStyles static method or using a manifest file. The first one causes all controls in your application to be drawn using visual styles, so it is important to call this method before any controls are drawn. The second one produces a file that includes information that will let your application know which version of the common controls library to use. By specifying that the application should use version 6.0 of the common controls library, you can be assured that all your controls will be drawn according to the selected theme. You will have to save the file with the same name of your application and append the extension “.manifest”, placing it in the same directory where the executable resides. You also have to set the Flatstyle property for any control System.
Each of the options previously mentioned have their own strengths and weaknesses. The manifest file does not involve writing code in your application to work, but using it risks having the user delete the manifest file and render your application themeless. The EnableVisualStyles procedure is easy to implement, but it requires the modification of your source code.
How does Visual Basic .NET supports application localization?
Both Visual Basic 6.0 and Visual Basic .NET support mechanisms to produce different presentations of your application for different languages. Using a single code base, you can distribute versions of your application in English, Spanish, and any other language you want.
In VB 6.0, if you plan to dynamically determine what language your application will run in, you must call some Win32 functions to determine the current locale. In Visual Basic .NET, this same information can be read from the CultureInfo object in the current thread.
How has the use of Resource Files been improved in Visual Studio .NET?
In Visual Basic 6.0, resource files are used to store the language specific data that your application uses. This data can be strings, images, or other types of resources. A VB 6.0 project can only contain a single resource file.
One way to add localization features to your application is to place versions of all your data for the different supported languages in a single resource file. Alternatively, you can store each language in different resource files; the resource files are compiled into independent DLLs. At run time, you dynamically load the DLL that contains the localized resources you need.
Visual Studio .NET provides a much more integrated level of support for localization. If you set the Localizable property of your form to True, Visual Studio creates a resource file for that form and stores the text strings for different languages in separate resource files that are automatically compiled into satellite DLLs. These DLLs are loaded dynamically and automatically at run time, depending on the current locale of the user’s computer. Alternatively, and for more control, you can do all this manually if you have to.
Visual Studio .NET does not provide any direct support for adding images to these resource files; however, the .NET Framework SDK provides the source code for a resource editor named ResEditor that does support images. Alternatively, you can embed all the images into the main assembly as resources.
How has the ToolTips functionality and capabilities changed in Visual Basic .NET?
In Visual Basic 6.0, you could associate a ToolTip with a control by setting the ToolTipText property of that control. To disable the ToolTips for a form, it was necessary to go through all the controls in that form and set their ToolTipText properties to the empty string. Without using Win32 calls, this was about the extent of the functionality that was available to VB 6.0 programmers.
In VB .NET, each form has its own ToolTip component. ToolTips are attached to controls by calling the SetToolTip() method of the ToolTip component. Additionally, in Visual Basic .NET, some new possibilities for easily customizing the behavior of your ToolTips are available.
In VB. NET, you can disable all the ToolTips in a form with a single command. The Active property determines whether the ToolTips register in this ToolTip component are displayed. Also, to change the amount of time that the mouse must remain still over a control before the ToolTip is displayed, you can modify the ToolTip.InitialDelay property (default time is half a second). And to change the amount of time that the ToolTip remains visible after it displays, change the AutoPopDelay property, which is set to five seconds by default. However, the ToolTip will disappear immediately after the mouse leaves the region the ToolTip applies to. Finally, a property named ReshowDelay determines the length of time it takes for subsequent ToolTips to appear as the pointer moves around the form.
A separate property can be used to set all of the ToolTip delay properties appropriately. This property is named AutomaticDelay and when you set this property, the same value is automatically copied into the InitialDelay property, a value that is ten times as large is placed in the AutoPopDelay property, and finally, a value that is one fifth as large is copied into the ReshowDelay property.
Which properties are available in the .NET Framework Enterprise Services?
.NET provides enterprise services for building highly scalable solutions. From the code itself, you can set properties required for enterprise services components or even deployment settings can be settled from the code. This is a great advantage for developers because you have the possibility of handling all those settings within the code.
Enterprise Services divides COM+ catalog properties into two categories:
- Properties the developer primarily determines, such as Transactions: exposed as attributes in the System.EnterpriseServicesnamespace and that may be added to the assembly by applying these attributes to the assembly, classes, interfaces or methods.
- Properties the administrator primarily determines, such as Identity: may be set through the Component Services administration tool after the assembly is registered or by using the COM+ Administrative interfaces.
When is it more appropriate to use Enterprise Services?
Enterprise Services is a powerful technology that is very useful when building complex applications that require specific technologies such as transactions, pooling, or queuing.
One of the most confusing aspects of Enterprise Services is when to use them. If your existing systems use COM+ extensively, you should use Enterprise Services when upgrading your applications to .NET.
What basic attributes must an enterprise application have?
To create a .NET class for an enterprise service component, the class must derive from the System.ServicedComponent class to exploit COM+ services. To do this, it is necessary to import the .NET Enterprise Services namespace. For some special purpose .NET namespaces, it is necessary to add a new reference to the corresponding assembly because that they are not included by default. This is the case for the System.EnterpriseServices namespace, so a reference to the associated assembly must be added.
How are Threads managed in .NET Enterprise Services?
COM+ manages threads for you. Every COM component has a ThreadingModel attribute that you can specify when you develop the component. This property determines how the component’s objects are assigned to threads for method execution.
Threads can be associated with two possible types of apartments:
- Single Threaded Apartments: STAs specify that only one thread is being executed. They are implemented as hidden windows that receive messages and dispatch method calls to the objects residing in the apartment. There can be multiple objects in an STA and there can be multiple STAs in a process.
- Multi Threaded Apartments: MTAs have a pool of executing threads. Synchronization must be considered in this scenario. Operating system synchronization mechanisms such as semaphores, critical sections, or mutexes can be used. MTAs are restricted to one per process.
The different types of processes can be defined as follows:
- A process that consists of just one STA is referred to as a single-threaded process.
- A process that has two or more STA and no MTA is referred to as an apartment model process.
- A process that has a MTA and no STA is referred to as a free-threaded process.
- A process that has a MTA and one or more STA is referred to as a mixed model process.
In reality, all processes are apartment model processes, and some apartments have a single thread while others have multiple threads. The threading model applies to an apartment, not to a process. It can also apply to a class of objects, but not a component, such as a DLL; instead, it applies to the object classes within the DLL. Different classes in a DLL can have different threading models.
A process can use both the apartment and free-threaded models, as long as it uses only one free-threaded apartment. It can have more than one single threaded apartment. Invocations to objects in the STAs will be automatically synchronized by Win32 and data will have to be marshaled whenever an apartment barrier is crossed.
What are the main considerations when working with transactions in .NET Enterprise Services?
The transaction features of COM+ are a repackaging of MTS. COM+ provides a new interface with individual get/set methods for each bit: SetComplete, EnableCommit, SetAbort, and DisableCommit.
Distributed transactions can also be thought of as automatic or declarative transactions in .NET. Performance is only one aspect of the decision making process related to your strategy for transaction management in your application. The other considerations are ease of development and flexibility and future expansion.
If you use a SqlTransaction object or OleDbTransaction object to manage transactions, you are going to have to manage the transaction yourself. You must consider how you will flow that transaction between objects and which object will be the root of the transaction responsible for calling Commit or Rollback.
Please note that the .NET Framework version 2.0 includes a new transaction programming paradigm. This functionality is included in the System.Transactions namespace and provides features such as asynchronous work, events, security, concurrency management, and interoperability.
Another important point to consider is the flexibility of the application. If the system was built from the ground up using Enterprise Services to provide transaction management, it will automatically manage and flow the transaction between all the resource managers, whether they are SQL databases or other transactional resources such as message queues. Additionally, with Enterprise Services you can create a component that participates in a distributed transaction using the Compensating Resource Manager (CRM). This provides new opportunities for executing business logic when the transaction outcome is known.
How are queued components used in .NET Enterprise Services?
Message Queuing (also known as MSMQ) constitutes a platform that supports flexible and reliable communication between applications. A task is asynchronously invoked when it is called without waiting for it to terminate and produce a result.
Using this type of execution allows applications to have better responsiveness and improved usage of the available resources. This mode of processing can be achieved using COM+ Queued Components and Message Queuing.
If you want to create a queued component in your application, first declare the interface that you want to call asynchronously through queued components and put the InterfaceQueuing attribute on the interface. The methods of this interface must not use any return values or ByRef arguments.
Your queued interface must be declared as public, or it will not be included in the registration of the assembly with COM+. Also, you have to specify that this assembly will be a queued component, by using the ApplicationQueuing attribute. After that you must provide an implementation of this interface in a server application. The ApplicationQueuing attribute instructs the server application to listen on a queue.
What other technology updates are available to an application with respect to .NET Enterprise Services?
- Security: the security infrastructure provided by Windows XP has been added to COM+. The application, component, and method levels can contain access controls, and the user has control over the user accounts and groups associated to roles.
- Load Balancing: The COM+ load balancing architecture allows you to define a router on a server that forwards object creations to a computer that has low utilization. After the object is created, method calls go to the same object even when it is stateless.
- COM+ Events: This service is an automated, loosely coupled event system that stores event information from different publishers in the COM+ catalog. Subscribers can query this store and select the events that they want to hear about.