1

I have a problem with method UniversalConverter. Row received from database should be transferred in to entity. So row[pi.Name] must be converted in to entity type. How to perform this?

public void GetRequestSummary(string UserName, string Password)
    {
        List<EducationRequest> requestSummary = new List<EducationRequest>();
        OracleCommand command = new OracleCommand();

        AddParameter(command, _usernamePN, DbType.String, UserName);
        AddParameter(command, _passwordPN, DbType.String, Password);
        AddOracleCursor(command, _curPN, OracleType.Cursor, ParameterDirection.Output); 

        command.CommandType = CommandType.StoredProcedure;

        DataSet personaSet =
            FillDataset(command, _GetRequestSummaryCmd);

        DataTable personaTable =
            personaSet.Tables[0];

        var request = new EducationRequestSummary();
        foreach (DataRow row in personaTable.Rows)
        {
            UniversalConverter(request, row);
        }

        //any implementation

    }

public void UniversalConverter<T>(T entity, DataRow row)
    {

        foreach (var pi in typeof(T).GetProperties())
        {
            pi.SetValue(entity, row[pi.Name], null);
        }
    }
Max Kilovatiy
  • 798
  • 1
  • 11
  • 32

1 Answers1

1

You need or something like Automapper

or you need to map this objects manually, even by using reflection as you provided in your code. Kepp attention property names and types obviously in that cases.

Hope this helps.

Tigran
  • 61,654
  • 8
  • 86
  • 123
  • Thank you! Automapper is very helpfull) – Max Kilovatiy Aug 25 '11 at 12:01
  • 1
    Automapper is good. Others who are watching... try ValueInjector. This post helped me to do something similar with _neither_ of those: http://stackoverflow.com/questions/1398796/casting-with-reflection – FastAl Jan 13 '12 at 20:03