Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5985211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:29:48+00:00 2026-05-22T22:29:48+00:00

I am working through a code sample, and I just want to hear some

  • 0

I am working through a code sample, and I just want to hear some opinions on the way that they have done things. They use plain old ADO.NET. They have a generic function called Read that brings back 1 record. Here is the code:

public static T Read<T>(string storedProcedure, Func<IDataReader, T> make, object[] parms = null)
{
   using (SqlConnection connection = new SqlConnection())
   {
      connection.ConnectionString = connectionString;

      using (SqlCommand command = new SqlCommand())
      {
         command.Connection = connection;
         command.CommandType = CommandType.StoredProcedure;
         command.CommandText = storedProcedure;
         command.SetParameters(parms);

         connection.Open();

         T t = default(T);
         var reader = command.ExecuteReader();
         if (reader.Read())
            t = make(reader);

         return t;
      }
   }
}

I don’t know why:

  • They use Func<IDataReader, T> make as part of the method signature? Is it efficient to do it like this? Is there a better way/best practice to do this?
  • I don’t understand T t = default(T);? Is it efficient to do it like this? Is there a better way/best practice to do this?
  • What does t = make(reader); do? Is it efficient to do it like this? Is there a better way/best practice to do this?

The calling function would look something like this:

public Customer GetCustomer(int customerId)
{
   // Other code here
   string storedProcedure = "MyStoredProcedure";
   object[] parameters = { "@CustomerId", customerId };
   return Db.Read(storedProcedure, Make, parameters);
}

private static Func<IDataReader, Customer> Make = reader =>
new Customer
{
   CustomerId = reader["CustomerId"].AsId(),
   Company = reader["CompanyName"].AsString(),
   City = reader["City"].AsString
};

I don’t understand the Func Make part? Can someone please explain to me what is happening here and if this is good practices. Any changes would be appreciated, but please provide detailed sample code 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:29:49+00:00Added an answer on May 22, 2026 at 10:29 pm

    The delegate for the make (materialization) is pretty versatile and flexible, but IMO makes for a bit of unnecessary work in the vast majority of cases. In terms of what it does – they use the delegate as a callback to get the caller to specify how to read the record.

    Note, since they don’t expect the consumer to change record, they should probably be exposing IDataRecord, not IDataReader (any reader also implements record).

    Note that if there are any error messages in the TDS stream after the first record, the approach shown won’t see them – but that is an edge case. If you wanted to mitigate against that, you could read to the end of the TDS stream:

    while(reader.NextResult()) {}
    

    Personally, though, I’d just use dapper-dot-net here – avoids having to write that per-type code manually:

    var cust = connection.Query<Customer>("MyStoredProcedure",
         new { CustomerId = customerId },
         commandType: CommandType.StoredProcedure).Single();
    

    this executes MyStoredProcedure as a sproc, passing in @CustomerId with the value from customerId, then applies a direct column<===>property/field match to create Customer records, then asserts that there is exactly one result – and returns it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working through an msdn example in VS08 on how to create a
for a project I'm working on, I want to be able to concatenate multiple
I am working on a large application and am adding some drag/drop functionality to
I have a simple page that loads a random image from an array and
Hello yeah I'm asking this question a second time, sorry about that but I
I need to get a script working for our billing system (WHMCS) using their
Scenario: Retrieve some entities Update some properties on those entities You perform some sort
This is my first stack overflow quesiton, so if i'm not posting correctly, or
(Feel free to skip the first two paragraphs :) I know this goes against
I am trying to learn about SQLite databases, but I really hate dealing with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.