Export (0) Print
Expand All
Around the World with Visual Basic
Asynchronous Method Execution Using Delegates
Building a Progress Bar that Doesn't Progress
Calling All Operators
Create a Graphical Editor Using RichTextBox and GDI+
Creating A Breadcrumb Control
Creating a Five-Star Rating Control
Creating and Managing Secondary Threads
Data Binding Radio Buttons to a List
Deploying Assemblies
Designing With Custom Attributes
Digital Grandma
Doing Async the Easy Way
Extracting Data from .NET Assemblies
Implementing Callbacks with a Multicast Delegate
Naming and Building Assemblies in Visual Basic .NET
Programming Events of the Framework Class Libraries
Programming I/O with Streams in Visual Basic .NET
Reflection in Visual Basic .NET
Remembering User Information in Visual Basic .NET
Advanced Basics: Revisiting Operator Overloading
Scaling Up: The Very Busy Background Compiler
Synchronizing Multiple Windows Forms
Thread Synchronization
Updating the UI from a Secondary Thread
Using Inheritance in the .NET World
Using the ReaderWriterLock Class
Visual Basic: Simplify Common Tasks by Customizing the My Namespace
What's My IP Address?
Windows Forms Controls: Z-order and Copying Collections
Expand Minimize
Advanced Basics
Advanced Features in Visual Basic .NET and Testing for Scalability
Ken Spencer


Q I'm new to Visual Basic® .NET, and I have a few questions. First, what's new in Visual Basic .NET that I can take advantage of to improve my applications? Also, what's the purpose of assemblies, and where can I store resources such as strings, images, image paths, and so on? Finally, when do I use Web Forms and Windows® Forms, and what are the differences in deploying them?
Q I'm new to Visual Basic® .NET, and I have a few questions. First, what's new in Visual Basic .NET that I can take advantage of to improve my applications? Also, what's the purpose of assemblies, and where can I store resources such as strings, images, image paths, and so on? Finally, when do I use Web Forms and Windows® Forms, and what are the differences in deploying them?

A There are many areas that can be used to enhance your applications and to take advantage of the advanced features of Visual Basic .NET.
A There are many areas that can be used to enhance your applications and to take advantage of the advanced features of Visual Basic .NET.
But before deciding on the features to build, you should first analyze your customers' needs. This involves determining the particular needs of the application that you're developing. Once you have that under your belt, you'll understand the entities involved along with the processing or functions needed. Entities are the objects that are involved in an application. For instance, most organizations have entities such as these: Customers (or clients), Products (whether they be physical like cars or virtual like an insurance policy), and Orders.
Understanding the entities allows you to design the database and the various objects for the organization. The objects are then created in classes, with each object in a separate assembly. Along the way, you find out about processing features, business rules, and generic features. Features or rules that apply to all objects are cooked into a base class. This base class is then inherited from by each of the business objects.
The generic classes, such as the ones you would use for some database operations, are marked as not inheritable and are used by various business objects. For instance, the database class would be used by the Customers object to perform database operations, but it would not be called directly by the application. The reason these classes are marked as not inheritable is that they are not designed to be extended in business classes. The business classes and framework classes, on the other hand, are designed to be extended in a variety of ways. In fact, the base class should be marked as MustInherit to prevent developers from instantiating it directly.
This type of architecture will result in an n-tier application that is easy to build and easy to maintain. Plus, creating the functional parts of the application becomes a simple task, since you are just wiring together various objects.
Splitting apart your application like this will also result in the reuse of many assemblies. The assemblies become granular objects that you can use and reuse. Plus, you can create custom attributes and use them to document your assemblies. Eventually you'll be able to create a code repository that can be used to host the assemblies and easily reuse and share that code. Reflection comes in handy here. It allows you to pull these attributes from assemblies and discover what each class does.
As for storing resources such as strings, image paths, images, and such, you can easily do that in several ways. You can store a file (such as an image or text file) in an assembly by following some simple steps in Visual Studio® .NET. First, add the file to the project, then set the Build Action for the file to Embedded Resource.
Now you can retrieve this item from the assembly by using the code shown in Figure 1 (which reads a text file from the assembly). This code loads an assembly and then retrieves the resource. You can also use the ResourceManager to manage and retrieve resources in assemblies. This allows you to manage not only simple resources, but also resources for localization purposes as well. For instance, you can easily build an application that works for many different languages and use resources to supply both text and images for each dialect. Not only can you embed resources in your application (as shown in Figure 1), but you can also create text files and embed them. You can even use satellite assemblies to store resources. There are some good samples covering resources in the Microsoft® .NET Framework SDK including, "Resources and Localization Using the .NET Framework SDK."


Dim frm As New frmDocIt()
Dim oAssembly As [Assembly]
Dim sResourceName As String
Dim pntLocation As Point

oAssembly = [Assembly].LoadFrom(sFileName)

sResourceName = oAssembly.GetName().Name + "." + custfile
       Dim strm As Stream = _
       oAssembly.GetManifestResourceStream(sResourceName)

If Not strm Is Nothing Then
'Read the contents of the embedded file
Dim reader As StreamReader = New StreamReader(strm)
frm.txtOutput.Text = reader.ReadToEnd()

frm.Text &= " — " & lblFileName.Text

pntLocation.X = Me.Location.X + 184
pntLocation.Y = Me.Location.Y + 100
frm.Location = pntLocation
frm.ShowDialog()
End If
oAssembly = Nothing
strm = Nothing

Whether you decide to use ASP.NET or Windows Forms or both, you can do many things that will make your application easier to build, maintain, and extend. The .NET Framework was specifically designed to make it easier to extend your apps.
One of the first things you should do when building a new application is create a set of exception handling features. These classes include standard features such as event logging, performance monitoring, and so forth, but also include more advanced features that make applications robust. This allows the developer to drop the exception handling classes into their application in a few seconds and not lose the business focus of the app.
In the .NET Framework there is a class for handling exceptions, ApplicationException. You can create your own exceptions by extending this class. You simply add the Inherits statement to the class like this:

Inherits ApplicationException

Now you can add your own properties and methods to the exception class. Then you can catch errors by using specific exceptions in your Catch statements like this:

Catch exc As UserLogonException

Creating your own exceptions is just one part of the advanced features you can work into your applications. This step makes the code more robust since it is easier to trap errors, and it has the advantage of making it more maintainable as well.
You also want to implement efficient error handling in the UI. In ASP.NET you can use the various validation controls to trap user errors. This allows you to quickly create an interface that traps errors and redirects the user to correct them on the spot. You can do the same thing in a Windows Forms application, but it takes a little more work. The easiest way to accomplish this is to create specific controls for user input and have these controls trap user errors. For instance, you can create a textbox that only accepts numbers, thus having an automated handler for numeric input. You can also do the same thing for specific data such as phone numbers, ZIP codes, and so forth. This approach provides you with a toolbox for data entry applications. In a recent book I coauthored, we created a new grid control based on the one that ships with ASP.NET. This control allows you to easily add a powerful grid to your application that automatically sorts and pages data, plus adds several other features.
Another useful approach is to create controls that provide common business functionality. For instance, you might create a Customer control which provides the UI for a customer, including things like customer name, address, and so forth. You can even implement properties that allow features to be turned on and off. Taking this approach allows you to build your application from a set of objects both at the UI level and the business object level.
When it comes to deployment, there are a number of things you can do to prepare your application. For instance, if you use custom event logs or custom performance monitor counters, you must provide a way to create them when you install your application. The easiest way to accomplish this is to use custom installers. You can create an installer by inheriting from the Installer class and adding your custom features.
You can install these custom features in two ways. You can create Microsoft Installer (MSI) packages that call the installers at setup time. You can also use the Installer tool (installutil.exe), which allows you to install custom services.

Q Can you make any suggestions on how to test my client/server .NET-centric application for scalability?
Q Can you make any suggestions on how to test my client/server .NET-centric application for scalability?

A The scalability of an application is usually limited by the data tier, business tier, database, or other back-end shared resource.
A The scalability of an application is usually limited by the data tier, business tier, database, or other back-end shared resource.
Because the scaling issues are in the shared resource area, it is easier to isolate and test these elements. To test everything but the UI, I created a simple ASP.NET front end that calls the client-side objects. I then set up Microsoft Application Center Test to exercise the ASP.NET application. To set up the test, I made sure the server objects were all running on a server that was configured the same as the production servers. I also set up ACT tests on multiple clients. This allowed me to run the client as one or more users.
When I ran the tests, they provided a good indication of performance, allowing me to create a baseline and change particular elements of the application to see how they affected performance. The end result was the ability to discover potential problems with any of the objects before deployment. This also stress-tested all of the objects under load.
You can also use Visual Studio Analyzer to test your components. In addition, you can find third-party testing applications that test entire applications.

Send your questions and comments for Ken to basics@microsoft.com.


Ken Spencerworks for 32X Tech (http://www.32X.com), where he provides training, software development, and consulting services on Microsoft technologies.

Show:
© 2014 Microsoft