ICustomerRetList和ICustomerRet的通用接口 - QBFC

时间:2010-11-22 14:23:44

标签: qbfc

那里有任何QBFC开发者吗?我正在使用QBFC从Quickbooks中提取多种不同类型的对象:客户,项目,发票,TaxCodes等。数据查询代码实际上只有在您到达Ret对象后才会变化,因此我正在尝试构建一些函数抽象过程。

典型的休止对象看起来像

IReponseList
    IResponse
         RetList
             Ret

IResponseList和IResponse都足够通用,可以处理所有查询响应类型。但是,似乎没有可用于抽象的通用RetList和Ret接口。我只有类型特定的接口,如ICustomerRetList,ISalesTaxCodeRetList等。我想编写独立于返回列表类型的代码....

是否有RetList或Ret的界面,我似乎无法找到?

由于

1 个答案:

答案 0 :(得分:0)

界面IQBBase是您最接近的内容。 QBFC中的大多数内容都源自IQBase,包括所有查询类型和所有返回类型。使用IQBBase引用和.NET泛型可以创建一个框架来处理查询结果。

更新:下面的迭代器示例现在可用作QBFC的Zombie库的一部分,您可以grab from github

例如,这是一个通用迭代器,它将RetList类型和Ret类型作为参数:

/// <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
}