Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Architecture
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
Bing
BizTalk
Book Excerpts
Build and Deploy
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Design Patterns
Development Process
Display Technologies
Distributed Computing
DotNetNuke
DSL
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
Internet Explorer 8.0
Interviews
iPhone
Iron Ruby
Java
Java Script
jQuery
LINQ
Linux
Mac OS X
MDX
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
Network
NHibernate
Object Oriented Development
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
Podcasts
Post Mortem
PowerPoint
Print/Output
Product News
Product Reviews
Project Management
Python
Q&A
Rails
Rake
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Search
Security
Services
SharePoint
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server CE/AnyWhere/Mobile/Compact
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
Tips
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WF
Whitepapers
Windows 7
Windows Azure
Windows Live
Windows Server
Windows Vista
WinForms
Workflow
WPF
XAML
XML
XNA
XSLT



CODE Training


 


DevLink

Reader rating:
Click here to read 12 comments about this article.
Article source: CoDe (2004 - May/June)


Article Pages:  1  2 3 4 5 - Next >


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.



Article Pages:  1  2 3 4 5 - Next Page: 'Conventional Asynchronous Calling: A Refresher Course' >>

Page 1: Async-Up Your Objects
Page 2: Conventional Asynchronous Calling: A Refresher Course
Page 3: Conventional Asynchronous Calling: A Refresher Course (con't.)
Page 4: Asynchronous Calling, New and Improved
Page 5: Asynchronous Calling, New and Improved (con't)

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
3.6 out of 5

45 people have rated this article.

      Hacker Halted

 

DevLink