2010-11-22 9 views
0

Alle QBFC-Entwickler da draußen? Ich benutze QBFC, um verschiedene Arten von Objekten aus Quickbooks herauszuziehen: Kunden, Artikel, Rechnungen, TaxCodes, usw. Der Datenabfragecode variiert wirklich nur, sobald du zum Ret-Objekt kommst, also versuche ich, einige Funktionen zu erstellen abstrahiere den Prozess.Generische Schnittstellen für ICustomerRetList und ICustomerRet - QBFC

Ein typisches Ruhe Objekt sieht aus wie

IReponseList 
    IResponse 
     RetList 
      Ret 

IResponseList und IResponse generisch genug ist auf allen Abfrageantworttypen zu arbeiten. Es scheint jedoch keine generische RetList- und Ret-Schnittstelle zu geben, die ich für die Abstraktion verwenden kann. Ich habe nur typspezifische Schnittstellen wie ICustomerRetList, ISalesTaxCodeRetList usw. Ich möchte den Code unabhängig davon schreiben, welcher TYPE der Rückgabeliste es ist ....

Gibt es eine Schnittstelle für RetList oder Ret, die ich gerade kann nicht scheinen zu finden?

Dank

Antwort

0

Die Schnittstelle IQBBase ist die nächste Sache, was Sie suchen. Fast alles in QBFC ist von IQBase abgeleitet, einschließlich aller Abfragetypen und aller Rückgabetypen. Unter Verwendung von IQBBase Verweisen und .NET Generics ist es möglich, ein Framework zu erstellen, um mit Abfrageergebnissen umzugehen.

Update: Das folgende Iterator-Beispiel ist jetzt als Teil der Zombie-Bibliothek für QBFC verfügbar, die Sie grab from github können.

Zum Beispiel, hier ist eine generische Iterator, die RetList Art und Ret-Typen als Parameter nimmt:

/// <summary> 
/// This generic class simplifies and standardizes iteration syntax 
/// for QBFC lists. Using this class we can use the foreach keyword 
/// to iterate across all items in a list. 
/// </summary> 
/// <typeparam name="L">The type of the list, for example IBillRetList</typeparam> 
/// <typeparam name="D">The type of the item, for example IBillRet</typeparam> 
public class QBFCIterator<L, D>:IEnumerable<D> where L : class, IQBBase 
{ 

    private L m_List; 

    /// <summary> 
    /// This constructor can be used for response list items or for sub-lists that are properties 
    /// on other QBFC objects. 
    /// </summary> 
    /// <param name="lst">The sub-list</param> 
    public QBFCIterator(IQBBase lst) 
    { 
     m_List = lst as L; 

     if (m_List == null && lst != null) 
     { 
      throw new Exception("iterator type mismatch"); 
     } 
    } 

    public bool IsEmpty 
    { 
     get 
     { 
      if (m_List == null) 
      { 
       return true; 
      } 
      else 
      { 
       return Count == 0; 
      } 
     } 
    } 

    /// <summary> 
    /// An efficient alternative to the Count() function 
    /// </summary> 
    public int EntityCount 
    { 
     get { return Count; } 
    } 

    public D GetFirstItem() 
    { 
     if (IsEmpty) 
     { 
      throw new Exception("Cannot retrieve item from empty list"); 
     } 
     else 
     { 
      return GetAt(0); 
     } 
    }   

    #region Late-bound properties 
    // 
    // Since .NET requires that all methods invoked on a parameterized type 
    // must compile based solely on interface constraints, we must use late 
    // binding to access the count property and GetAt methods. This may have 
    // an impact on performance and could conceivably cause run time errors 
    // with incorrect type parameters. 
    // 
    private int Count 
    { 
     get 
     { 
      if (m_List == null) 
      { 
       return 0; 
      } 
      else 
      { 
       Type t = m_List.GetType(); 

       return (int)t.InvokeMember("Count", 
        System.Reflection.BindingFlags.GetProperty, null, m_List, null); 
      } 
     } 
    } 

    private D GetAt(int idx) 
    { 
     Type t = m_List.GetType(); 

     return (D)t.InvokeMember("GetAt", 
      System.Reflection.BindingFlags.InvokeMethod, null, m_List, new Object[] { idx }); 
    } 

    #endregion 

    #region IEnumerable<D> Members 

    public IEnumerator<D> GetEnumerator() 
    { 
     if (m_List != null) 
     { 
      for (int idx = 0; idx < Count; idx++) 
      { 
       yield return GetAt(idx); 
      } 
     } 
    } 

    #endregion 

    #region IEnumerable Members 

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() 
    { 
     if (m_List != null) 
     { 
      for (int idx = 0; idx < Count; idx++) 
      { 
       yield return GetAt(idx); 
      } 
     } 
    } 

    #endregion 
}