lucas
lucas

Reputation: 4685

Enterprise Library Mappings

Is there a way to map database fields to OOP complex type fields with Enterprise Library. I am calling stored procedures that retrieves all data that I would like to store into custom class.

Here I retrieve data from sp:

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

And here is my class

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

It works fine if I would create one class with all database fields, but I would like to have more control over it by mapping database fields to custom complex type properties.

Upvotes: 0

Views: 134

Answers (1)

lucas
lucas

Reputation: 4685

Here is the answer in my case, in case someone would look for similar solution:

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

Upvotes: 1

Related Questions