Factory - Using Dynamic Creation of CORBA Objects

Description:

Most of the examples instantiate the CORBA objects during startup and publish the object references before calling the 'Orb.run()' method. But CORBA objects may also be created on demand by other CORBA objects as presented in this example.

Source:

\Demo\Factory

Goal:

Realize a 'Factory' pattern for CORBA object

Example

Step 1. The IDL (Factory.idl):

We define an object interface with two children (for the sake of simplicity with all functionality defined within the parent), and a factory interface to create the appropriate object instance based on the passed in metadata:

    module Factory
    {
        interface IGreetings
        {
            string hello( in string Name);
        };
        
        interface IGreetingsFriends:IGreetings
        {
        };
        
        interface IGreetingsBusiness:IGreetings
        {
        };
        
        interface IGreetingFactory
        {
            exception GreetingExcp
            {
                string  strWhy;
                long    lErrorCode;
            };
            
            IGreetings createIGreetings( in string strNameOfInterface) raises (GreetingExcp);
        };
    };
    

(Will be compiled into Factory.cs)

Step 2. The Server Implementation (within FactorySrv.cs):

Implement the object interfaces:

    public class GreetingsFriends: IGreetingsFriendsPOA
    {
        public override string hello(  string _strName  )
        {
            string strGreeting = "Greetings to my Friend " +_strName;
            return strGreeting;
        }
    }
    
    public class GreetingsBusiness: IGreetingsBusinessPOA
    {
        public override string hello(  string _strName  )
        {
            string strGreeting = "Greetings to my business partner " + _strName;
            return strGreeting;
        }
    }
    

Implement the factory:

    public class GreetingFactory: IGreetingFactoryPOA
    {
        private GreetingsBusiness  m_GreetingsBusiness = null;
        private GreetingsFriends   m_GreetingsFriends = null;
        
        public override IGreetings createIGreetings(  string _strNameOfInterface  )
        {
            Factory.IGreetings  oIGreetings = null;
            
            switch( _strNameOfInterface)
            {
                case "Business":
                    if( m_GreetingsBusiness == null)
                    {
                        m_GreetingsBusiness = new GreetingsBusiness();
                    }
                    oIGreetings = m_GreetingsBusiness._this();
                    break;
                    
                case "Friends":
                    if( m_GreetingsFriends == null)
                    {
                        m_GreetingsFriends = new GreetingsFriends();
                    }
                    oIGreetings = m_GreetingsFriends._this();
                    break;
                    
                default:
                    throw new Factory.IGreetingFactoryPackage.GreetingExcp("Request for an unknown Interface", 1001);
            }
            return oIGreetings;
        }
    }
    

Note, that we have to return the CORBA objects servant reference obtained by calling its '_this()' method.

Step 3. The Client (FactoryClt.cs):

The client retrieves a factory reference in the usual way:

    Ics.CORBA.Object oObj = m_oOrb.string_to_object( "file://c:\\Factory.ior");
    
    m_oIGreetingFactory  = Factory.IGreetingFactoryHelper.narrow( oObj);
    

The remote factory will be used to create the appropriate CORBA objects on the server side:

    try
    {
        // should result in an 'IGreetingsBusiness' reference:
        Factory.IGreetings oIGreetings = m_oIGreetingFactory.createIGreetings("Business");
        System.Console.WriteLine("Create business greetings:\n\t '{0}'\n", oIGreetings.hello("K. Dudly"));
        
        // should result in an 'IGreetingsFriends' reference:
        oIGreetings = m_oIGreetingFactory.createIGreetings("Friends");
        System.Console.WriteLine("Create friends greetings:\n\t'{0}'\n", oIGreetings.hello("M. Maier"));
        
        // should result in a 'GreetingsExcp' exception:
        oIGreetings = m_oIGreetingFactory.createIGreetings("Aliens");
        System.Console.WriteLine("Create aliens greetings:\n\t'{0}'\n", oIGreetings.hello("M. Maier"));
    }
    catch( Factory.IGreetingFactoryPackage.GreetingExcp ex)
    {
        System.Console.WriteLine("Exception from Server. Reason:'{0}', ErrorCode:{1}",
                                  ex.strWhy, ex.lErrorCode);
    }
    

Step 4. Run the example

a.) Start the Server.

b.) Start the Client.