企业库映射

时间:2016-03-16 17:23:34

标签: c# enterprise-library

有没有办法使用Enterprise Library将数据库字段映射到OOP复杂类型字段。我正在调用存储过程来检索我想要存储到自定义类中的所有数据。

这里我从sp:

中检索数据
            IEnumerable<WorkHistoryGrid> data = new List<WorkHistoryGrid>();
        return db.ExecuteSprocAccessor<WorkHistoryGrid>("PensionPDF_RetrieveParticipantWorkHistoryForFund", pensionId, fund);

这是我的班级

    public class WorkHistoryGrid : BindableBase
{        
    private string _rateType;
    private string _fundType;
    private string _employer;
    private string _employerName;
    private string _local;
    private string _dateBalanced;
    private string _plan;
    private string _fund;
    private WorkHistoryGridMergeData _mergeData;

    #region Properties
    public WorkHistoryGridMergeData MergeData
    {
        get { return _mergeData; }
        set { SetProperty(ref _mergeData, value); }
    }

    public string RateType
    {
        get { return _rateType; }
        set { SetProperty(ref _rateType, value); }
    }
    public string FundType
    {
        get { return _fundType; }
        set { SetProperty(ref _fundType, value); }
    }
    public string Employer
    {
        get { return _employer; }
        set { SetProperty(ref _employer, value); }
    }
    public string EmployerName
    {
        get { return _employerName; }
        set { SetProperty(ref _employerName, value); }
    }
    public string Local
    {
        get { return _local; }
        set { SetProperty(ref _local, value); }
    }
    public string DateBalanced
    {
        get { return _dateBalanced; }
        set { SetProperty(ref _dateBalanced, value); }
    }
    public string Plan
    {
        get { return _plan; }
        set { SetProperty(ref _plan, value); }
    }
    public string Fund
    {
        get { return _fund; }
        set { SetProperty(ref _fund, value); }
    }
            }
        }
    }

如果我要创建一个包含所有数据库字段的类,它可以正常工作,但我希望通过将数据库字段映射到自定义复杂类型属性来对其进行更多控制。

1 个答案:

答案 0 :(得分:1)

以下是我的答案,以防有人寻找类似的解决方案:

            var workHistoryGridSetMapper = new WorkHistoryGridSetMapper();

        db.ExecuteSprocAccessor<WorkHistoryGrid>("PensionPDF_RetrieveParticipantWorkHistory", workHistoryGridSetMapper, pensionId);

IResultSetMapper

    public class WorkHistoryGridSetMapper : IResultSetMapper<WorkHistoryGrid>
{
    public IEnumerable<WorkHistoryGrid> MapSet(IDataReader reader)
    {
        List<WorkHistoryGrid> workHistoryLst = new List<WorkHistoryGrid>();

        using (reader) // Dispose the reader when we're done
        {
            while (reader.Read())
            {
                WorkHistoryGrid workHist = new WorkHistoryGrid();

                workHist.Amount = reader.GetValue(reader.GetOrdinal("Amount")).ToString();
                workHist.DateBalanced = reader.GetValue(reader.GetOrdinal("DateBalanced")).ToString();
                workHist.Employer = reader.GetValue(reader.GetOrdinal("Employer")).ToString();
                workHist.EmployerName = reader.GetValue(reader.GetOrdinal("EmployerName")).ToString();
                workHist.Fund = reader.GetValue(reader.GetOrdinal("Fund")).ToString();
                workHist.FundType = reader.GetValue(reader.GetOrdinal("FundType")).ToString();
                workHist.Hours = reader.GetValue(reader.GetOrdinal("Hours")).ToString();
                workHist.Local = reader.GetValue(reader.GetOrdinal("Local")).ToString();
                workHist.Period = reader.GetValue(reader.GetOrdinal("Period")).ToString();
                workHist.Plan = reader.GetValue(reader.GetOrdinal("Plan")).ToString();
                workHist.RateAmount = reader.GetValue(reader.GetOrdinal("RateAmount")).ToString();
                workHist.RateType = reader.GetValue(reader.GetOrdinal("RateType")).ToString();
                workHist.Status = reader.GetValue(reader.GetOrdinal("Status")).ToString();
                workHist.WorkMonth = reader.GetValue(reader.GetOrdinal("WorkMonth")).ToString();
                workHist.MergeData = new WorkHistoryGridMergeData
                {
                    MergeDateMerged = reader.GetValue(reader.GetOrdinal("MergeDateMerged")).ToString(),
                    MergeLastUpdated = reader.GetValue(reader.GetOrdinal("MergeLastUpdated")).ToString(),
                    MergeLastUpdatedUserId = reader.GetValue(reader.GetOrdinal("MergeLastUpdatedUserId")).ToString(),
                    MergeLastUpdatedUserType = reader.GetValue(reader.GetOrdinal("MergeLastUpdatedUserType")).ToString(),
                    MergeNewSsn = reader.GetValue(reader.GetOrdinal("MergeNewSsn")).ToString(),
                    MergeNotes = reader.GetValue(reader.GetOrdinal("MergeNotes")).ToString(),
                    MergeOldSsn = reader.GetValue(reader.GetOrdinal("MergeOldSsn")).ToString(),
                    MergeTrustId = reader.GetValue(reader.GetOrdinal("MergeTrustId")).ToString(),
                    MergeUserName = reader.GetValue(reader.GetOrdinal("MergeUserName")).ToString()
                };

                workHistoryLst.Add(workHist);
            };
        }

        return workHistoryLst;
    }
}