Design issue
-
I’m writing an MFC Doc/View (SDI) application with database support (DAO). In CMyDoc class I put a member variable CmyDaoDB* m_MyDaoDB and some function to add/delete data to/from Database. On the view I build a listbox control in wich I need to put the entire recordset (say Customers). I’d like to buil a member function in the CmyDoc class that return the list of Customer to view. But how? I think that the only solution is to access directly to CmyDaoDB from the view to get the customers: while (!m_MyDaoDb->IsEOF()) { /* Put the record in the list control */ m_MyDaoDb->MoveNext() } Is there a better way to get that list? (I mean more OO)
-
I’m writing an MFC Doc/View (SDI) application with database support (DAO). In CMyDoc class I put a member variable CmyDaoDB* m_MyDaoDB and some function to add/delete data to/from Database. On the view I build a listbox control in wich I need to put the entire recordset (say Customers). I’d like to buil a member function in the CmyDoc class that return the list of Customer to view. But how? I think that the only solution is to access directly to CmyDaoDB from the view to get the customers: while (!m_MyDaoDb->IsEOF()) { /* Put the record in the list control */ m_MyDaoDb->MoveNext() } Is there a better way to get that list? (I mean more OO)
You have the basic idea. The way that I do it is to pass the list control to the Doc and have the doc populate the list, that way the View doesn't need to know anything about the database. Something like this: CDoc::AddCustomers( CListCtrl* pList ) { // Reset the list control content pList->DeleteAllItems(); // I'm assuming that you somehow get a CDaoRecordset for the Customer table // and not accessing the database object directly. m_MyDaoDB->MoveFirst(); while( m_MyDaoDB->IsEOF() != 0 ) { // Add the record to the list m_MyDaoDB->MoveNext(); } } Don't know if this is a better OO way, but the Doc should know about the data, and the view handles the UI. I like to keep access to the data in the Doc and the View handles the display and user interaction.
-
I’m writing an MFC Doc/View (SDI) application with database support (DAO). In CMyDoc class I put a member variable CmyDaoDB* m_MyDaoDB and some function to add/delete data to/from Database. On the view I build a listbox control in wich I need to put the entire recordset (say Customers). I’d like to buil a member function in the CmyDoc class that return the list of Customer to view. But how? I think that the only solution is to access directly to CmyDaoDB from the view to get the customers: while (!m_MyDaoDb->IsEOF()) { /* Put the record in the list control */ m_MyDaoDb->MoveNext() } Is there a better way to get that list? (I mean more OO)
Is there a better way to get that list? (I mean more OO) "More OO" solution is to create a public member in your doc class which takes CListBox reference as an argument and fills the window with database contents:
// error handling omitted
void CYourDoc::FillListBox(CListBox &lbx)
{
// use m_MyDaoDb here to fill &lbx
while (!m_MyDaoDb->IsEOF())
{
int idx = lbx.AddString(...);
lbx.SetItemData(idx, ...);
m_MyDaoDb->MoveNext();
}
}Tomasz Sowinski -- http://www.shooltz.com