手工制作的强力键入的ADO.net DataTable - 它可以更干净吗?

时间:2009-07-30 01:00:10

标签: c# strongly-typed-dataset

我最近遇到了一个非常简单的Typed DataTable(没有使用.XSD)(我丢失了作者的URL,所以我不能相信他)但看起来有很多重复的代码(例如Add / Remove / GetNewRow方法)。

我试图将重复的方法推入超类,但由于Employee需要通用,我遇到了问题。我希望StackOverflow的集体蜂巢能够提出一些想法来清理它吗? (如果它甚至可能吗?)

using System;
using System.Data;
using System.Collections;
using System.Data.SqlClient;

namespace TypedDataSet {

  public class Employees : DataTable {
    protected SqlDataAdapter _adapter;

    public Employees() {
      string connectionString = TypedDataSet.Properties.Settings.Default.ConnectionString;
      _adapter = new System.Data.SqlClient.SqlDataAdapter("SELECT Id, Firstname, Surname FROM Employee", connectionString);
      _adapter.Fill(this);
    }

    public Employee this[int index] {
      get { return (Employee)Rows[index]; }
    }

    public void Add(Employee row) {
      Rows.Add(row);
    }

    public void Remove(Employee row) {
      Rows.Remove(row);
    }

    public Employee GetNewRow() {
      Employee row = (Employee)NewRow();
      return row;
    }

    protected override DataRow NewRowFromBuilder(DataRowBuilder builder) {
      return new Employee(builder);
    }

    public IEnumerator GetEnumerator() {
        return Rows.GetEnumerator();
    }

    protected override Type GetRowType() {
        return typeof(Employee);
    }
  }

  public class Employee : DataRow {
    internal Employee(DataRowBuilder builder)
      : base(builder) {
    }

    public Int64 Id {
      get { return (Int64)base["Id"]; }
      set { base["Id"] = value; }
    }

    public string FirstName {
      get { return (string)base["Firstname"]; }
      set { base["Firstname"] = value; }
    }

    public string Surname {
      get { return (string)base["Surname"]; }
      set { base["Surname"] = value; }
    }
  }
}

1 个答案:

答案 0 :(得分:3)

我相信我已经回答了我的问题。我必须使用.net 4.0来获得我希望特别是动态类型的结果。

因此改变问题中的现有类:

<强> Employee.cs

using System;
using System.Data;
using System.Collections;
using System.Data.Common;

namespace TypedDataSet {

  public class Employees : BaseModel<Employee> {

    public Employees(bool loadAll) {
        DbDataAdapter adapter = base.Adapter("SELECT * FROM Employees");
        adapter.Fill(this);
    }

    protected override DataRow NewRowFromBuilder(DataRowBuilder builder) {
        return new Employee(builder);
    }
  }

  public class Employee : DataRow {
    internal Employee(DataRowBuilder builder)
      : base(builder) {
    }

    public Int64 Id {
      get { return (Int64)base["Id"]; }
      set { base["Id"] = value; }
    }

    public string FirstName {
      get { return (string)base["Firstname"]; }
      set { base["Firstname"] = value; }
    }

    public string Surname {
      get { return (string)base["Surname"]; }
      set { base["Surname"] = value; }
    }
  }
}

现在介绍BaseModel,上面的类继承了


<强> BaseModel.cs

using System;
using System.Data;
using System.Collections;
using System.Data.Common;
using System.Data.SqlClient;

namespace TypedDataSet {

    public class BaseModel<T> : DataTable {
        protected DbDataAdapter _adapter;
        protected string _connectionString = TypedDataSet.Properties.Settings.Default.ConnectionString;

        public BaseModel() {
        }

        protected DbDataAdapter Adapter(string sql) {
            _adapter = new System.Data.SqlClient.SqlDataAdapter(sql, _connectionString);
            SqlCommandBuilder cb = new SqlCommandBuilder((SqlDataAdapter)_adapter);
            return _adapter; 
        }

        public dynamic this[int index] {
            get { return Rows[index]; }
        }

        public void Add(dynamic row) {
            Rows.Add(row);
        }

        public void Remove(dynamic row) {
            Rows.Remove(row);
        }

        public void Save() {
            _adapter.Update(this);
            this.AcceptChanges();
        }

        public dynamic GetNewRow() {
            dynamic row = (dynamic)NewRow();
            return row;
        }

        public IEnumerator GetEnumerator() {
            return Rows.GetEnumerator();
        }

        protected override Type GetRowType() {
            return typeof(T);
        }
    }
}

这允许我使用以下代码来使用该类:


Employees employees = new Employees(true);

Employee employee = employees.GetNewRow();
employee.FirstName = "Greg";
employee.Surname = "Focker";
employees.Add(employee);

employees.Save();

foreach (Employee e in employees) {
  Console.WriteLine(e.FirstName + ' ' + e.Surname);
}

我希望进一步加快这一进程,以便将来的StackOverflow用户如果您对这个小项目感兴趣,请查看我希望托管代码的http://bitbucket.org/Mozketo/typeddataset/