1

I am converting data from sql into a list of PersonModel. But my question is: is there a faster way ( less code ) to get this done without using any framework / dapper. LINQ is ALLOWED.

( The code right now is just working fine, but maybe it can be done in a simpler way ).

This is the code I have right now:

public List<PersonModel> GetPerson_All()
{
    var people = new List<PersonModel>();

    //Get the connectionString from appconfig
    using (var connection = new SqlConnection(GlobalConfig.CnnString(db)))
    {
        connection.Open();

        //Using the stored procedure in the database.
        using (var command = new SqlCommand("dbo.spPeople_GetAll", connection))
        {
            using (var reader = command.ExecuteReader())
            {
                //With a while loop, going trough each row to put all the data in the PersonModel class.
                while (reader.Read())
                {
                    var person = new PersonModel();

                    person.Id = (int)reader["Id"];
                    person.FirstName = (string) reader["FirstName"];
                    person.LastName = (string)reader["LastName"];
                    person.EmailAdress = (string)reader["EmailAddress"];
                    person.CellphoneNumber = (string)reader["CellphoneNumber"];

                    //Add the data into a list of PersoModel
                    people.Add(person);
                }
            }
        }
    }

    return people;
}

With ( dapper ) you can put all the data inmediatly to a list. Is something like this possible without dapper?

public List<PersonModel> GetPerson_All()
{
    List<PersonModel> output;
    using (var connection = new SqlConnection(GlobalConfig.CnnString(db)))
    {
        output = connection.Query<PersonModel>("dbo.spPeople_GetAll").ToList();
    }
    return output;
}
6
  • 1
    Code Review would probably be a better fit for this question Commented Jun 15, 2017 at 11:27
  • 2
    Why do you think libraries like Dapper exist? it's exactly to cut down on boilerplate code like this. Commented Jun 15, 2017 at 11:28
  • SQL query (stored procedure in the question) is the bottleneck of the routine; that's why any other implementations will not be much faster Commented Jun 15, 2017 at 11:28
  • @JeroenMostert I know? but I am not allowed to use something like dapper Commented Jun 15, 2017 at 11:32
  • 1
    @Alegou20: you can always write your own micro-ORM T Map<T>(DataReader) where T : new that creates and initializes a new object from a DataReader using reflection. This is only a few lines of code. It is terrifically slow, however -- optimizing it results in much more code, so much more that you'd make it a library and call it Dapper. Commented Jun 15, 2017 at 11:43

1 Answer 1

2

No, there is not an more optimal way to do this then to use a method or mapper library like Dapper or Entity Framework: that is the entire reason such libraries exist.

If you have repeating code blocks like this, but don't want to use external libraries, you can try to refactor this to a method which executes a statement, iterates over the result and instantiates and fills objects.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.