Click here to Skip to main content
11,639,861 members (59,572 online)
Click here to Skip to main content

Generic Async Factory

, 24 Nov 2006 CPOL 34K 141 18
Rate this:
Please Sign up or sign in to vote.
A Generic Extensible Async Factory

Introduction

Factories are used for creating objects. Different objects would have different creational logic. There are instances where the object creation might consume time and you may want to move the logic onto a non-blocking thread to keep the application responsive. The AsyncFactory is a step towards that. You can have different factory implementations, without worrying about the asynchronous execution logic.

The AsyncFactory

The AsyncFactory is a Generic class which can be sub classed for any factory implementation, that needs to executed asynchronously. The AysncFactory is an Abstract Generic class with a single protected abstract Create method. The method accepts a type parameter <P> and returns a type <T>. This method needs to be overridden in the subclass with specific implementation of your interest with the appropriate parameter and return types.

public abstract class AysncFactory<TReturn, TParam>
{
  protected abstract TReturn Create(TParam createParams); 
}

Internally the AysncFactory uses the age old .NET ThreadPool and queues the creation logic on a non-blocking thread. It has a BeginCreate and an EndCreate implementation, which eventually gets exposed to the clients. They follow the conventional BeginXXX and EndXXX signature and implementation.

public IAsyncResult BeginCreate(AsyncCallback callback, TParam createParams);
public TReturn EndCreate(IAsyncResult asyncResult);

There's an CreateAsyncResult class that implements IAsyncResult. It contains a private ManualResetEvent member to block the client when the EndCreate is called, till the creation logic is completed. The BeginCreate queues an internal AsyncCreate method which actually invokes the overridden Create method. Once the Create method completes, the AsyncCreate sets the flag on the WaitHandle, invokes the callback and returns. The EndCreate calls the WaitOne on the WaitHandle, which blocks till the Create function completes and then the set on the WaitHandle is called to signal all the blocked threads.

The clients call the BeginCreate with the callback and the parameters that need to be passed to the create method. The BeginCreate returns an IAsyncResult, which is needed to be passed to the EndCreate to trigger the completion. The EndCreate blocks until the creation is completed and returns the created object.

Sample Implementation

WinWordApplicationFactory is a factory class which creates instances of WinWord Application object. The Create method takes a boolean value to turn on/off the visible property, and returns an instance of ApplicationClass:

 public class WinWordApplicationFactory : AsyncFactory<MSWordApplication, bool>
 {
     protected override MSWordApplication Create(bool isVisible)
     {
         object oMissing = System.Reflection.Missing.Value;
         MSWordApplication msWord = new MSWordApplication();
         msWord.Visible = isVisible;
         return msWord;
     }
 }

Client

 private ApplicationClass wordApp;
 private WinWordApplicationFactory appFactory;
 public WinWordFactoryClient()
 {
     this.appFactory = new WinWordApplicationFactory();
     this.CreateWordInstance();
 }

 private void CreateWordInstance()
 {
     //Call the BeginCreate of the factory with a callback.
     appFactory.BeginCreate(new AsyncCallback(this.AppCreateCallback), true);
 }
 
 public void AppCreateCallback(IAsyncResult result) 
 {
     //Call the EndCreate to get the object created by the factory.
     this.wordApp = appFactory.EndCreate(result);

     //start using the wordApp instance.
     //this.wordApp.Documents.Open(....
 }

Conclusion

Generally Factory classes are singleton. I'm on the way to implementing the same. Till then, happy coding!

History

  • 24th November, 2006: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Krishnan Srinivasan
Architect
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
Bugtypo on IAyncResult Pin
Member 979457913-May-14 2:21
memberMember 979457913-May-14 2:21 
GeneralRe: typo on IAyncResult Pin
Krishnan Srinivasan19-May-14 18:17
memberKrishnan Srinivasan19-May-14 18:17 
GeneralWaitOne Pin
jbrathwaite29-Nov-06 9:38
memberjbrathwaite29-Nov-06 9:38 
GeneralRe: WaitOne Pin
kichasrini30-Nov-06 16:26
memberkichasrini30-Nov-06 16:26 
GeneralRe: WaitOne Pin
jbrathwaite2-Dec-06 17:12
memberjbrathwaite2-Dec-06 17:12 
GeneralRe: WaitOne Pin
kichasrini3-Dec-06 3:54
memberkichasrini3-Dec-06 3:54 
GeneralRe: WaitOne Pin
jbrathwaite2-Dec-06 22:14
memberjbrathwaite2-Dec-06 22:14 
GeneralRe: WaitOne Pin
kichasrini3-Dec-06 3:49
memberkichasrini3-Dec-06 3:49 
GeneralRe: WaitOne Pin
jbrathwaite3-Dec-06 23:02
memberjbrathwaite3-Dec-06 23:02 
GeneralRe: WaitOne Pin
kichasrini5-Dec-06 2:46
memberkichasrini5-Dec-06 2:46 
GeneralAnother take Pin
Alexey A. Popov24-Nov-06 23:05
memberAlexey A. Popov24-Nov-06 23:05 
GeneralRe: Another take Pin
kichasrini30-Nov-06 16:29
memberkichasrini30-Nov-06 16:29 
GeneralThanks! Pin
Alexey A. Popov24-Nov-06 21:08
memberAlexey A. Popov24-Nov-06 21:08 
GeneralRe: Thanks! Pin
kichasrini25-Nov-06 3:10
memberkichasrini25-Nov-06 3:10 
GeneralRe: Thanks! Pin
Alexey A. Popov27-Nov-06 7:18
memberAlexey A. Popov27-Nov-06 7:18 
GeneralRe: Thanks! Pin
kichasrini30-Nov-06 15:40
memberkichasrini30-Nov-06 15:40 
GeneralRe: Thanks! Pin
narsyseth27-Nov-06 7:18
membernarsyseth27-Nov-06 7:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150731.1 | Last Updated 24 Nov 2006
Article Copyright 2006 by Krishnan Srinivasan
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid