samedi 17 décembre 2016

How to pass input for virtual function when input varies based on derived type?

I am trying to implement the DAO pattern. Basically it contains one transfer object and all the inputs need to be passed for underlying storage need to be set on that transfer object and after processing it will return the result via transfer object. Implemented the DAO pattern as Abstract factory. First it will return the type of storage like Mysql, Oracle etc. Second factory will return the operation type like fetch employee info, update employee info etc., Transfer object will vary from one operation type to other operation type. Now i am keeping the transfer object as part of operation type class and performing dynamic cast to set the inputs.

Questions:

  1. What will be better place to keep the transfer object? Whether it has to be kept on operation type class or some where else?

  2. Currently i am performing dynamic cast? Is it bad to use dynamic cast here?

SOURCE CODE:

#include  <stdio.h>

class ITransObjectManager // Interface
{
   protected:
      ITransObjectManager() {}
      virtual ~ITransObjectManager() {};

   public:
      virtual bool handleTransferObject()=0;
      void release();

      class Type
      {
         public:
            enum Type_t
            {
               GET_EMPLOYEE_INFO,
            };
      };
};


class IDaoFactory
{
   protected:
      IDaoFactory(){};
      virtual ~IDaoFactory(){}

   public:
      class StorageType
      {
         public:

            enum StorageType_t
            {
               MYSQL,
               ORACLE,           
            };
      };

      static IDaoFactory* getFactoryType(StorageType::StorageType_t);
      void release();

      virtual ITransObjectManager* getTransferObjectHandler(ITransObjectManager::Type::Type_t)=0;

};


/* Concrete implemenation */

class MySqlDao : public IDaoFactory
{

   public:
      ITransObjectManager* getTransferObjectHandler(ITransObjectManager::Type::Type_t);
};


class OracleDao : public IDaoFactory
{

   public:
      ITransObjectManager* getTransferObjectHandler(ITransObjectManager::Type::Type_t);
};


/* Concrete TranferObjectHandler's */
class EmployeeInfoDao
{
   public:
      EmployeeInfoDao() {}

      class TranferObject
      {
         public:
            TranferObject(){emp_id=0;}
            int emp_id;
            int emp_name;
      };

      TranferObject m_emp_trans_obj;;
};


class EmployeeInfoMySqlDao : public EmployeeInfoDao, public ITransObjectManager
{
   public:
      bool handleTransferObject(); // Getting data using Mysql
};


class EmployeeInfoOracleDao : public EmployeeInfoDao, public ITransObjectManager
{
   public:
      bool handleTransferObject(); // Getting data using Oracle
};



IDaoFactory*  IDaoFactory::getFactoryType(StorageType::StorageType_t p_storage_type)
{
   switch(p_storage_type)
   {
      case StorageType::MYSQL:
         {
            return new MySqlDao();
         };
      case StorageType::ORACLE:
         {
            return new OracleDao();
         }
   }

   return NULL;
}

ITransObjectManager* OracleDao::getTransferObjectHandler(ITransObjectManager::Type::Type_t p_trans_obj_hand_type)
{
   switch(p_trans_obj_hand_type)
   {
      case ITransObjectManager::Type::GET_EMPLOYEE_INFO:
         return new EmployeeInfoOracleDao();
   }
   return NULL;
}


ITransObjectManager* MySqlDao::getTransferObjectHandler(ITransObjectManager::Type::Type_t p_trans_obj_hand_type)
{
   switch(p_trans_obj_hand_type)
   {
      case ITransObjectManager::Type::GET_EMPLOYEE_INFO:
         return new EmployeeInfoMySqlDao();
   }
   return NULL;
}


bool EmployeeInfoMySqlDao::handleTransferObject()
{
   printf("\n Mysql Rate");
   return true;
}

bool EmployeeInfoOracleDao::handleTransferObject()
{
   printf("\n Oracle Rate"); 
   return true;
}


//Application side usage

int main()
{

   IDaoFactory *l_factory= IDaoFactory::getFactoryType(IDaoFactory::StorageType::MYSQL);

   ITransObjectManager *l_transObjHandler= l_factory->getTransferObjectHandler(ITransObjectManager::Type::GET_EMPLOYEE_INFO);

   /* START :: QUESTION PART */

   // Need to set input independent of oracle or Mysql using transfer object, but currently it is dependent on the storage type

   EmployeeInfoMySqlDao *l_empInfoMysql = dynamic_cast<EmployeeInfoMySqlDao *>(l_transObjHandler);

   if(l_empInfoMysql)
   {
      l_empInfoMysql->m_emp_trans_obj.emp_id=10;
   }

   /* END :: QUESTION PART */

   /* Getting data using oracle */
   l_transObjHandler->handleTransferObject(); 

}

Aucun commentaire:

Enregistrer un commentaire