Async-Up Your Objects Encapsulate asynchronous functionality directly into your business objects. The .NET Framework facilitates calling object methods asynchronously through the use of delegates. You may already know how to do this using helper code, but there is a cleaner and much cooler way of packaging this kind of functionality right inside your business objects. This article assumes a basic knowledge of delegates, the .NET Framework's type-safe version of C++ function pointers. | " | You can avoid waiting for your calls to complete by using an asynchronous delegate in the conventional manner to call an operation on its own thread.
| " |
Part of good component writing involves designing and developing components with the idea that developers other than you will use them. A good practice is to use conventions that are familiar and, if possible, industry standard. In this article, you will learn how to build asynchronous functionality directly into your business objects using a method-naming scheme already in use by asynchronous delegates, asynchronous Web services, and various other asynchronous classes throughout the .NET Framework. In the end, your business objects will be powerful, scalable, and follow a familiar .NET pattern. Before you design these new objects, let's review how to call an object's method using an asynchronous delegate. First, set up a simple class to serve as a business object and that will evolve throughout this article. The Business Object Call this class FileFunctions and give it one method: MoveFiles. This method receives two strings as its arguments: sourceFolder and destinationFolder. Quite simply, the method accesses the directory location identified by the sourceFolder argument, and moves all files within to the directory identified by the destinationFolder argument. The return variable for this method is a Boolean that indicates success or failure of this operation. The actual business functionality will not be described or listed here. In C#: public bool MoveFiles(string sourceFolder, string destinationFolder) { // Functionality for file moving. return true; }
In VB .NET: Public Function MoveFiles( _ ByVal sourceFolder As String, _ ByVal destinationFolder As String)
As Boolean
' Functionality for file moving. Return True
End Function
To perform this function in a normal synchronous fashion, instantiate the class and set a Boolean variable to the method call, sending in the required arguments. In fact, you can do this all in one line. In C#: bool b_Success = new FileFunctions().MoveFiles( "c:\\BeforeFolder", "c:\\AfterFolder");
In VB .NET: Dim b_Success As Boolean = _ New FileFunctions().MoveFiles( _ "c:\\BeforeFolder ", _ "c:\\AfterFolder")
This works just fine, except for the fact that the calling program just sits there until this operation is complete and a success or failure flag has been returned. So let's avoid this wait by using an asynchronous delegate in the conventional manner to call this operation on its own thread. | & | | 
By: Miguel Castro Miguel is an architect with IDesign who specializes in architecture consulting and building .NET solutions. He is a Microsoft MVP and INETA speaker and has been a software developer for over 22 years. With a Microsoft background that goes all the way back to VB 1.0 (and QuickBasic in fact), Miguel jumped on .NET as soon as the first public Beta was released and has provided .NET solutions for clients around the country in a variety of industries. He considers himself to be a .NET Developer and Architect and has equal love for both VB and C#, and no tolerance for language bigotry. He’s spoken at numerous user groups around the country as well as developer conferences.
He’s the author of the CodeBreeze code-generator, which among things can be found on his Web site:
www.steelbluesolutions.com
Miguel currently lives in Lincoln Park, NJ with his wife Elena and his daughter Victoria.
subscriptions@infotekcg.com | Fast Facts | | A delegate itself is neither synchronous nor asynchronous. The method of activation is what makes it act in one manner or the other. | |
|