0

例如,我有很多课程

    public class CountryModel
    {
        public string CountryCode { get; set; }
        public string CountryName { get; set; }
    }

    public class RegionModel
    {
        public string RegionId{ get; set; }
        public Int64 RegionName { get; set; }
        public string RegionType { get; set; }
    }

目前我调用使用不同的存储过程来流行具有类属性相关数据的类。例如;

    while (CountryResultSet.Read())
    {
      CountryModel MyObj = new CountryModel();
      MyObj.CountryCode = oRes["CountryCode"].ToString();
      MyObj.CountryName = oRes["CountryName"].ToString();
    }

然后

    while (RegionResultSet.Read())
    {
      RegionModel MyRegionObj = new RegionModel();
      MyRegionObj.RegionId = oRes["RegionId "];
      MyRegionObj.RegionName = oRes["RegionName"].ToString();
      MyRegionObj.RegionType = oRes["RegionType"].ToString();
    }

这很好用,但是对于从 MySQL 数据获取数据的不同请求,我需要重复多次。这似乎是在浪费重复的代码。

任何人都可以推荐一种方法,这样我就可以只使用一种可以满足所有类类型的生成方法。

4

1 回答 1

1

我使用映射到数据库表和 C# 激活器的代码生成类的组合。

以下内容与内部开发工具一起使用,并使用从数据库中检索到的记录填充列表:

    public void FillDbObjectList<T>(out List<T> list, string tableName) where T : CommonUtilities.DbObjectTableBase
    {
        list = new List<T>();

        ADODB.Recordset rcd;
        this.FillRecordset(EDbRecordSource.Table, tableName, out rcd);

        while (!rcd.EOF)
        {
            object[] paramArray = this.FillActivatorParameterArray(rcd);
            list.Add((T)Activator.CreateInstance(typeof(T), paramArray));
            rcd.MoveNext();
        }

        this.TryCloseRecordset(rcd);
    }

FillActivatorParameterArray 方法只是创建一个与字段顺序相关的对象数组。

    private object[] FillActivatorParameterArray(ADODB.Recordset rcd)
    {
        object[] paramArray = new object[rcd.Fields.Count];

        for (int i = 0; i < rcd.Fields.Count; i++)
        {
            if (rcd.Fields[i].Value == DBNull.Value)
            {
                paramArray[i] = null;
            }
            else
            {
                paramArray[i] = rcd.Fields[i].Value;
            }
        }

        return paramArray;
    }

这是代码生成类之一的示例。

public class Table_Archetype : DbObjectTableBase
{
    #region Fields
    private System.String name = "ChangeMe";
    private System.Int32 fK_ToolField_ID_DataSource = 0;
    private System.String description__DEV = "";
    private System.Int32 fK_EnumArray_ID_DataExportType = 19;
    private System.Boolean isDeleted = false;
    #endregion

    public Table_Archetype() { }

    public Table_Archetype(System.Int32 inID) { base.id = inID; }

    public Table_Archetype(
        System.Int32 inID
        , System.String inName
        , System.Int32 inFK_ToolField_ID_DataSource
        , System.String inDescription__DEV
        , System.Int32 inFK_EnumArray_ID_DataExportType
        , System.Boolean inIsDeleted
    )
    {
        base.id = inID;
        this.name = inName;
        this.fK_ToolField_ID_DataSource = inFK_ToolField_ID_DataSource;
        this.description__DEV = inDescription__DEV;
        this.fK_EnumArray_ID_DataExportType = inFK_EnumArray_ID_DataExportType;
        this.isDeleted = inIsDeleted;
    }

    #region Properties
    public System.String Name
    {
        get { return this.name; }
        set
        {
            if (this.name != value)
            {
                this.name = value;
                this.isDirty = true;
            }
        }
    }
    public System.Int32 FK_ToolField_ID_DataSource
    {
        get { return this.fK_ToolField_ID_DataSource; }
        set
        {
            if (this.fK_ToolField_ID_DataSource != value)
            {
                this.fK_ToolField_ID_DataSource = value;
                this.isDirty = true;
            }
        }
    }
    public System.String Description__DEV
    {
        get { return this.description__DEV; }
        set
        {
            if (this.description__DEV != value)
            {
                this.description__DEV = value;
                this.isDirty = true;
            }
        }
    }
    public System.Int32 FK_EnumArray_ID_DataExportType
    {
        get { return this.fK_EnumArray_ID_DataExportType; }
        set
        {
            if (this.fK_EnumArray_ID_DataExportType != value)
            {
                this.fK_EnumArray_ID_DataExportType = value;
                this.isDirty = true;
            }
        }
    }
    public System.Boolean IsDeleted
    {
        get { return this.isDeleted; }
        set
        {
            if (this.isDeleted != value)
            {
                this.isDeleted = value;
                this.isDirty = true;
            }
        }
    }
    #endregion
于 2013-08-28T17:00:00.917 回答