2016-03-16 140 views
0

有沒有辦法使用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

這裏是我的情況下答案,以防有人會尋找類似的解決方案:

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