Main page / CORBA with MinCor.NET page

Factory - Using Dynamic Creation of CORBA Objects


Description: 
	Most of the examples instantiate the CORBA objects during startup and publish the object 
	references before begin waiting for remote calls. But CORBA objects may also be created
	on demand by other CORBA objects as presented in this example.	


Source: 
	MinCor\Demo\Factory

Goal: 
	Realize a 'Factory' pattern for CORBA object



Example 

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)



The Implementation (within ServerImpl.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.
    
    
The Client (ClientImpl.cs):

    The client retrieves a factory reference in the usual way:
    
    Middsol.CORBA.Object oObj = m_oOrb.string_to_object( "file://.\\FactorySrv.ior");
    
    m_oIGreetingFactory  = Factory.IGreetingFactoryHelper.narrow( oObj);
    
    
    The remote factory will be used to create the appropriate CORBA objects on the server side:
    
    string srLog = "Demo: FactoryClt";
    try
    {
        // should result in an 'IGreetingsBusiness' reference:
        Factory.IGreetings oIGreetings = m_oIGreetingFactory.createIGreetings("Business");
        srLog += "\nCreate business greetings:" + oIGreetings.hello("K. Dudly");
        
        // should result in an 'IGreetingsFriends' reference:
        oIGreetings = m_oIGreetingFactory.createIGreetings("Friends");
        srLog += "\nCreate friends greetings:" + oIGreetings.hello("M. Maier");
        
        // should result in a 'GreetingsExcp' exception:
        oIGreetings = m_oIGreetingFactory.createIGreetings("Aliens");
        srLog += "\nCreate aliens greetings:" + oIGreetings.hello("M. Maier");
    }
    catch( Factory.IGreetingFactoryPackage.GreetingExcp ex)
    {
        srLog += "\nCatch Exception from Server (GreetingExcp).";
        srLog += "\n         Why:" + exUser.strWhy;
    }