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

  • Home
  • SEARCH
  • 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 3271422
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:43:37+00:00 2026-05-17T18:43:37+00:00

I created a class where the main task is get data from the DB

  • 0

I created a class where the main task is get data from the DB and mapping it to some object. The problem is the same class needs to map different datareader to different object. So, what I tried to do is to get out the mapping method using delegates.

Here is part of my code. See the important rows in bold.

public class GetDetails<T>
{       
    **public delegate void DelegateMapping(T position, IDataReader reader);**
    **public DelegateMapping mappingMethod;**

    public T Get(T instance)
    {
        //Get IDs and Add to list
        _db.ExecuteReader(storedProcedure.ToString(), CommandType.StoredProcedure, reader =>
        {
            while ( reader.Read() )
            {
                **mappingMethod(instance, reader);**
            }

        }, parameterList.ToArray());

        return instance;
    }
}

And this is the class which is calling and using the “GetDetails” class

public class PositionDB : DbBase
{
    public Position GetPositionDetails(string IDs)
    {
        GetDetails<Position> getIDs = new GetDetails<Position>(base.db);
        getIDs.storedProcedure = StoredProcedure.NET_ADM_GetPositionDetails;

        //Set the Delegated Method
        **getIDs.mappingMethod = MappingPositionDetails;**

        //Set Parameters
        getIDs.parameterList.AddInParam("PositionIds", DbType.String, IDs);

        //Return the PositionId Collection
        return getIDs.Get(new Position());
    }

    **private void MappingPositionDetails(Position position, IDataReader reader)
    {
        position.Id = reader["CompPositionId"];
        position.Description = reader["Description"];
        position.ExpirationDate = reader["ExpirationDate"];
        position.Title = reader["Title"];
    }**

}

The code is working OK.

The questios are:

  1. Did I use delegate correctly?
  2. This kind of solution can cause problems in the future (performance)?
  3. There is another better solution?

Thank you very much

Sebastian

  • 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-17T18:43:37+00:00Added an answer on May 17, 2026 at 6:43 pm

    To specifically answer your questions:

    1. Yes, you did use delegates correctly
    2. Yes, it can cause problems due to concurrency issues while multithreading
    3. I think so, I detailed one possible solution below

    I would propose three changes:

    1. Move the delegate call into the method (concurrency issues, one thread could change the mapping delegate while another thread tries to access it, now trying to map a reader to completely different object than provided)
    2. Use the already present generic Action/Func delegates, no need to define your own.
    3. Use lambda expressions to define the mapping, no need for extra methods

    Notice: 2 and 3 will need at least .net 3.5.

    Employing these two proposals, your code would look like this:

    public class GetDetails<T>
    {       
        public T Get (T instance, Action<T, IDataReader> mappingMethod)
        {
            //Get IDs and Add to list
            _db.ExecuteReader(storedProcedure.ToString(), CommandType.StoredProcedure, reader =>
            {
                while ( reader.Read() )
                {
                    mappingMethod(instance, reader);
                }
    
            }, parameterList.ToArray());
    
            return instance;
        }
    }
    

    Now you can use this method in a multi-threaded environment as well.

    Edit

    just realized it’s just part of the code. I corrected my proposal to take this into account.

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

Sidebar

Related Questions

No related questions found

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.