Wiki Home

VFPSession Factory


Namespace: WIN_COM_API
In VFP, you can use Session-based class factories to create instances that use the factory's datasession.

In VFP, objects created in a particular Data Session execute in the context of that Data Session. Therefore, if you use a Session class (or any class that can have private datasessions such as Form or FormSet) then all objects created by this instance will share that Data Session

In the example below, each factory will create objects with that factory's Data Session. This datasession survives the factory, so you can release the factory, or let it go out of scope, but the data session survives for the life of the objects that use it.

CLEAR 
Factory1= CREATEOBJECT("SessionFactory")
Factory2= CREATEOBJECT("SessionFactory")

x=Factory1.Create("MyClass")   && Echoes, say, "2" to screen
y=Factory2.Create("MyClass")   && Echoes, say, "3" to screen

DEFINE CLASS MyClass AS Custom
  FUNCTION INIT()
    ? SET("DataSession")
ENDDEFINE

DEFINE CLASS SessionFactory AS Session
  FUNCTION CREATE(tcPassed)
    LOCAL loRetVal
    loRetVal= CREATEOBJECT(tcPassed)
    RETURN loRetVal
ENDDEFINE

Wouldn't it be nice if we could use the same factory to create the objects, i.e.
with something like this:
CLEAR
Factory= CREATEOBJECT("SessionFactory")

x=Factory.Create("MyClass") && Echoes, say, "2" to screen
y=Factory.Create("MyClass") && Echoes, say, "3" to screen

DEFINE CLASS MyClass AS Custom
  FUNCTION INIT()
    ? SET("DataSession")
ENDDEFINE

DEFINE CLASS SessionFactory AS Custom
  FUNCTION CREATE(tcPassed)
    isf = createobject("InternalSessionFactory")
    LOCAL loRetVal
    loRetVal= isf.CREATE(tcPassed)
    RETURN loRetVal
ENDDEFINE

DEFINE CLASS InternalSessionFactory AS Session
  FUNCTION CREATE(tcPassed)
    LOCAL loRetVal
    loRetVal= CREATEOBJECT(tcPassed)
    RETURN loRetVal
ENDDEFINE

-- David Fung
Category Code Samples Category Design Patterns Category Data
( Topic last updated: 2004.04.23 12:02:28 PM )