-2

在这里,我使用的是 wpf。在我的代码中,有一个由 datacontect 命名的文件夹:它包含如下代码:

public interface IAccountCategoryDataSource
{
    bool Add(AccountCategory accountCategory);
    bool Update(AccountCategory accountCategory);
    bool Remove(AccountCategory accountCategory);

    AccountCategory GetById(int id);
    AccountCategory GetByName(string name);
    IEnumerable<AccountCategory> GetByParentCategory(AccountCategory category);
    IEnumerable<AccountCategory> GetTopLevelCategories();
    IEnumerable<AccountCategory> GetBySearchTerm(string searchTerm);
    IEnumerable<AccountCategory> GetAll();

    event EventHandler<ObjectAddedEventArgs> AccountCategoryAdded;
    event EventHandler<ObjectUpdatedEventArgs> AccountCategoryUpdated;
    event EventHandler<ObjectRemovedEventArgs> AccountCategoryRemoved;
}

还有其他文件夹数据源:这里的数据源与上述文件有关

public class AccountCategoryDataSource : IAccountCategoryDataSource
{
    public int ReferenceCountOf(AccountCategory accountCategory)
    {
        int count = 0;

        string query = "select count(*) from account_categories where parent_category=" + accountCategory.Id
                     + " union select count(*) from accounts where category=" + accountCategory.Id;

        DataTable dataTable = SQLiteHelper.ExecuteQuery(query);
        foreach (DataRow row in dataTable.Rows)
        {
            count += Convert.ToInt32(row[0]);
        }

        return count;
    }

    #region IAccountCategoryDataSource Members

    public bool Add(AccountCategory accountCategory)
    {
        string query = "insert into account_categories(name, description, parent_category) values("
                            + "'" + accountCategory.Name + "', "
                            + "'" + accountCategory.Description + "', "
                            + accountCategory.ParentCategory.Id
                            + ")";
        accountCategory.Id = SQLiteHelper.ExecuteInsert(query);

        if (accountCategory.Id > 0)
        {
            if (AccountCategoryAdded != null)
            {
                AccountCategoryAdded(this, new ObjectAddedEventArgs(accountCategory));
            }

            return true;
        }

        return false;
    }

    public bool Update(AccountCategory accountCategory)..................

这与模型 floder 的 accountcategory 类有关

public class AccountCategory : IDataErrorInfo, IValidable
{
    #region State Properties

    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public AccountCategory ParentCategory { get; set; }
    public bool Builtin { get; set; }

}

并执行 :helper 类

 public class SQLiteHelper
{
    public static DataTable ExecuteQuery(string query)
    {
        DataTable dataTable = new DataTable();

        SQLiteConnection conn = new SQLiteConnection(Settings.Default["DBConnectionString"].ToString());

        using (SQLiteCommand cmd = new SQLiteCommand(query, conn))
        {
            conn.Open();

            SQLiteDataReader reader = cmd.ExecuteReader();
            dataTable.Load(reader);
            reader.Close();

            conn.Close();
        }

        return dataTable;
    }..........................

请解释数据契约的简要作用。以及它如何与助手、模型和数据源交互。它是否提供 linq to sql 概念?

4

1 回答 1

0

DataContract是一个 WCF 概念。它与 WPF 无关(直到它与 WCF 通信)。

从您的代码看来,它只是一个包含数据访问层类的文件夹,并且似乎与 WCF 无关

于 2012-05-16T03:43:23.523 回答