11 January 2007

Windows Script Components as an alternative to COM

Windows Script Components provide VBScript and JScript developers with the ability to create COM style components. They can wrap up shared functionality into a component which can be registered on a server and instantiated using a CreateObject call just like other COM.

Defining a component

<?xml version="1.0"?>
<component>
   <public>
      <property name="Forename" internalName="strForename" />
      <property name="Surname">
         <get/>
         <put/>
      </property>
      <method name="Save" />
   </public>
   <script language="VBScript">
   <![CDATA[
   
   Dim strForename, strSurname

   Function get_Surname()
      get_Surname = strSurname
   End Function

   Function put_Surname(strValue)
      strSurname = strValue
   End Function

   Function Save()
      If IsEmpty(strForename) Or IsEmpty(strSurname) Then
         Save = False
      Else
         Save = True
      End If
   End Function

   ]]>
   </script>
</component>

Usage

Set myObj = Server.CreateObject("MyApp.MyClass")
myObj.Forename = "Derek"
myObj.Surname = "Fowler"
myObj.Save()
Set myObj = Nothing

Advantages

  • You don't need to server-side include anything to use them, once they're registered they're available from anywhere
  • Like other COM you can use them Application or Session scoped in ASP which means you can use a tailored object rather than abusing Arrays or a Recordset to create your shopping basket
  • Anywhere you can use a COM component you can use a WSC - even within a .NET application

Disadvantages

  • You don't get the performance benefits of proper compiled COM components
  • They don't support destructors which can make clearing up a pain
  • You can't do proper locking in VBScript or JScript so it's difficult to avoid concurrancy issues such as when using them Application scoped in ASP

Having said all that for the majority of applications the advantages certainly outway the disadvantages. Creating your data access and business tiers using WSCs allow you to work outside the confines of ASP environment and create components you can use anywhere that supports COM.

For anyone working in a company that is resisting the adoption of .NET this ability to write functionality to use in ASP but which you can then reuse in ASP.NET provides you with a clear upgrade path.

Links

Microsoft downloads

2 comments:

Jan Hansen said...

Nice intro! I have a few comments though.

1. Using WSC i session- and application-scope is "dangerous" as WSC components are appartement threaded (and using appartement threaded components in these scopes is a sin :-)).

2. cleanup (at least in VBScript) could be handled by creating a local class which implements initializer and desctructor-code for buildup and teardown. E.g.

class CCleanup
  private sub class_initialize()
    ' create component ressources
  end sub
  private sub class_terminate()
    ' dispose of component ressources
  end sub
end class
dim objCleanup
set objCleanup = new CCleanup

I believe this will work as COM will terminate the objCleanup when the WSC object leaves scope and itself is terminated. When this happens objCleanup.class_terminate is called and your cleanup is done.


Regards, Jan

Derek Fowler said...

Thanks Jan, they're both good points, especially regarding apartment threading. I'll have to update the post.