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 3276018
In Process

The Archive Base Latest Questions

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

We have built an internal tool that generates the whole data access, each table

  • 0

We have built an internal tool that generates the whole data access, each table has a class the represents it’s data and all the common operations.(think lightweight Entity framework).

These DataAccess objects always have a constructor that receives a connection string, and a Load function that receives a SqlDataReader.

Something like this:

class Customer
{
    public string ConnStr;
    public int Id;
    public string Name;

    Public customers(string connStr)
    {
        ConnStr = connStr;
    }

    public Customer Load(SqlDataReader)
    {
        if(reader.Read())
        {
            Id = reader["Id"].ToString();
            Name = reader["Name"].ToString();
        }   
    }
}

I want to write a utility Data Access static method that will allow me to write my SQL and get a list of objects in return, following that previous object example:

string SQL = "SELECT * FROM Customers WHERE Name=@Name";
List<Customer> customers = GetList<Customer>(connStr, SQL, new SqlParameters("@Name", "John"));

I somehow can’t figure how to deal with it, I have tried interfaces but they don’t allow constructors or static methods, and using generics – i can’t call the methods i need to init the object(constructor + load), here is my latest try, commented out the section that doesn’t work:


public static List<T> GetList<T>(string connStr, string SQL, params SqlParameter[] prms)
        {
            List<T> list = new List<T>();

            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();

                SqlCommand cmd = new SqlCommand(SQL, conn);
                foreach (SqlParameter param in prms)
                {
                    cmd.Parameters.Add(param);
                }

                using (SqlDataReader reader = cmd.ExecuteReader())
                {

                    //T item = new T(connStr);
                    //item.Load(reader);
                    //list.Add(item);
                }
            }

            return list;
        }

Btw, we are interested in open sourcing our DataAccess generator, it’s amazing – it allow very effecient access to DB objects + creates a javascript data access layer that gives you FULL CONTROL of your DB from javascript(ofcourse this has security implications which can be managed).

If anyone here knows how to “open source” a project like this or any company that wants to join the development of this product – please feel free contacting me: eytan@titkadem.co.il

Thanks in advance,
Eytan

  • 1 1 Answer
  • 1 View
  • 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-17T19:12:37+00:00Added an answer on May 17, 2026 at 7:12 pm

    The Load is easy enough – you could have:

    interface IDataEntity {
        void Load(SqlDataReader reader);
    }
    

    then:

    public static List<T> GetList<T>(string connStr, string SQL,
          params SqlParameter[] prms) where T : IDataEntity, new()
    {
        .... 
        T item = new T();
        item.Load(reader);
        list.Add(item);
    }
    

    The new T(connStr) is trickier – does it really need this value? A public property would be easier:

    interface IDataEntity {
        void Load(SqlDataReader reader);
        string ConnectionString {get;set;}
    }
    class Customer : IDataEntity 
    { ... }
    

    etc. There isn’t any inbuilt (language) support for parameterised generic constructors. You can hack around it, but in many cases the parameterised forms of Activator.CreateInstance are fast enough (when compared to data-access over a network, reflection is negligible). If you need a parameterised version it can be done with Expression etc (let me know if you want an example).

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

Sidebar

Related Questions

So, I have a nice domain model built. Repositories handle the data access and
I have a project (built with Maven) which has many internal dependencies; some of
I have built so far an application that allows the user to drag and
I have built up an array of objects, created from a class, I wrote
I have built a game in HTML5 and a web form posts data to
I have built a suite of internal websites for our company to manage some
I have some internal-facing ASP.NET web services that have had numerous API additions over
I have a Spring framework based Java web application, which has been built in
I'm building a general purpose data translation tool for internal enterprise use, using Java
I have built an internal Database for Clients. Its C# with an MS-SQL DB.

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.