Optimizing the Application

Creating the Configuration File

A configuration file is simply an XML file with the appropriate tags and an appropriate name. A configuration file for an application will have the name ..config, where is the name of the application, and is the extension of the application (such as .exe).

Thus, the .config file for an application named myApplication.exe would be myApplication.exe.config. A configuration file must be located in the same folder as the application assembly that it configures.

The content of a configuration file must be configured in the .config file schema.

Aside from the first element, which specifies the XML version and the encoding, and the top-level element, there is no required content for a configuration file. All other elements are optional and can be added or dispensed with as needed.

In Visual Basic .NET, you can create a configuration file for your assembly by choosing Add New Item from the File menu and selecting Application Configuration File. A file with the structure described earlier is added to your application. You can add elements manually to this file, and it will be appropriately named when the application is built.

In Visual C#, you must manually create the .config file by opening a text editor, such as Notepad, and writing the schema displayed previously, adding any desired elements at the same time. You must then save the file as app.config and add the file to your project.

To create a configuration file with Visual Basic .NET

From the Project menu, choose Add New Item.
  1. The Add New Item window opens.
  2. In the Add New Item window, choose Application Configuration File.
  3. A configuration file is added to your project.
  4. Within the element, add schema elements appropriate to the configuration you would like for your application.
  5. Consult the Visual Studio .NET documentation for detailed information on all of the available schema elements.
  6. Save your file and build your application.
  7. To create a .config file with Visual C#
  8. From the Project menu, choose Add New Item.
  9. In the Add New Item window, choose Text File.
  10. A new text file is added to your project, and the text editor for it opens.
Configuring Your Application Using Dynamic Properties

Dynamic properties allow you to configure the startup values of objects in your application. You can map the specific properties of objects to entries in your configuration file and then retrieve them dynamically at run time. Dynamic properties are useful for specifying external resources that might change in the course of an application's lifetime, such as a database connection string.

By reading the value of such a property dynamically, you can reconfigure your application without having to recompile and redeploy. Visual Studio .NET allows you to configure dynamic properties using the Properties window at design time, or you can manually add code to retrieve dynamic property values.

In order to read a dynamic property from the .config file, you must supply a key. The key is written to the .config file and corresponds to the appropriate value to return from the .config file. When a key is set for a dynamic property, the key and the value of that property are automatically written to the .config file. The following example shows the element that is added to the .config file by Visual Studio .NET to make Button1.Text configurable:

The value for key (Button1.Text) is used by the application to retrieve the value (Button1) at run time. Because key values need to be read by humans, it is a good idea to use a consistent pattern for creating key values. The default pattern used by Visual Studio .NET is ., where control is the name of the control and propertyname is the name of the property.

After the program has been deployed, you can configure any dynamic properties by directly editing the configuration file. For example, to change the value of Button1.Text from Button1 to myButton, you would locate the appropriate element in the .config file and change the value represented therein. The next time the application starts, the new property value will be read from the configuration file.

To use the Properties window to create a dynamic property

In the designer, select the appropriate control.

In the Properties window, expand the Dynamic Properties node.

If your property is already represented in the Dynamic Properties node, click the ellipses next to the entry for it, and specify a key for the property.

The key and the value for that property are written to the .config file.

If your property is not already represented in the Dynamic Properties node, click the ellipses to the right of the (Advanced) entry.

The Dynamic Properties window appears.

In the Dynamic Properties window, select the properties you would like to be configurable, and set a key using the drop-down menu.

Setting and Retrieving Dynamic Properties Manually

At times, you might want to make non-UI properties configurable as well. Consider, for example, a class instantiated at run time. You might want to provide a set of initial properties for that object, but the set of initial properties might vary depending on external factors. You can provide default properties for dynamically created objects in the configuration file and retrieve them dynamically at run time using the AppSettingsReader class.

The AppSettingsReader class is found in the System.Configuration namespace and uses a key to retrieve a value from the configuration file. The main method of this class is the GetValue method. The GetValue method requires a String value for the key, and a Type that indicates the type of object to be retrieved. Even though a Type is specified, the retrieved value is returned as an object and must be explicitly converted to the correct data type.

Optimizing Your Application's Performance

Even after an application is built and deployed, development doesn't stop. In business settings, applications are constantly being fine-tuned to maximize performance or to conserve resources. In this section, you will learn guidelines for optimizing your program.

Optimization Begins During Development

Efficient, optimized code is written as the result (in part) of careful planning and good coding practice. By following coding guidelines, you can create applications that already have a measure of optimization built in.

Coding guidelines

Avoid late binding. Whenever possible, avoid the use of Object (object) data types. Unnecessary conversions are expensive in terms of resources and decrease application performance. Visual Basic .NET users should always code with Option Strict On, so as to enforce strict typing and reduce unnecessary conversions.

Avoid global variables. Use local variables and constants whenever possible. Local variables are allocated in a memory region that is easier for code to access. Global variables should only be used for truly global needs. The use of constants for frequently used values can also make code more efficient.

Be wary of loops. Because loops can be the most operation-intensive regions of an application, they merit special attention. Take care to design your loops so that the fewest operations are required.

Optimization Is an Iterative Process

The general plan for optimizing your code

Measure performance data.

Identify bottlenecks.

Tune your code.

Repeat.

Once bottlenecks in the code have been identified, those areas of code should be examined to determine if anything can be done to address any performance issues. Once issues have been addressed, you should measure performance data again, not only to identify different bottlenecks, but also to confirm that the code optimizations that you just implemented had any performance benefit. Assuming success in this regard, your next round of performance monitoring can identify new or less severe bottlenecks, allowing you to further optimize your application.

Measuring Performance

Microsoft Windows 2000 and Windows XP include a utility called perfmon.exe, which can be used to monitor performance. This application can be used to monitor a wide variety of performance-related issues. You can use this utility to view performance data graphically or to write data to log files.

You can also use Trace statements to monitor application execution. Emitted Trace statements can be used to pinpoint the execution of your application at run time. When cross-referenced with performance data, this combination allows you narrow down the location of bottlenecks in your code.

Using the Compiler Optimizations

You can perform automatic optimizations on your application by enabling optimization with the compiler. Although this step does not take the place of careful coding, you can gain a performance benefit in some cases by applying these optimizations.

You can enable optimizations for your application in the Property Pages for your application. To view the optimizations for your application, right-click your project in Solution Explorer and choose Properties. For Visual Basic .NET, choose optimizations in the Configuration Properties folder. The Optimizations Property Page is displayed.

You can enable optimizations by selecting the check box labeled Enable Optimizations (Visual Basic .NET) or by setting Optimize Code to True (Visual C#). This will cause compiler optimizations to be applied when the application is built. Because compiler optimizations can cause code rearrangements at the Intermediate Language level, applications can be difficult to debug after optimizations have been applied. Thus, you should only use compiler optimizations on release code.

related post

DAY 11 OOPS INTRODUCTION

DAY 12 POLYMORPHISM

DAY 13 INHERITANCE AND POLYMORPHISM

DAY 14 EBUGGING TOOLS IN DOT NET

DAY 15 DEBUG AND TRACE IN CLASSES

DAY 16 UNIT TEST PLAN

DAY 17 EXCEPTIONS IN VISUAL STUDIO

DAY 19 ADO.NET INTRODUCTION

DAY 20 DATA ACCESSING IN DOT NET

DAY 21 DATA BASE OBJECTS


No comments:

Post a Comment