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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:34:38+00:00 2026-05-26T16:34:38+00:00

I have a Data access library which I would like to return different data

  • 0

I have a Data access library which I would like to return different data formats (XML, Json, DataTable). I was trying to use Generics to achieve this.

public interface IDBInteractor<T>
{
    T ExecuteDSQuery(string myQuery)
}

public class DBInteractorDT : IDBInteractor<DataTable>
{
    public DataTable ExecuteDSQuery(string myQuery)
    {
        return new DataTable();
    }
}

public class DBInteractorJson : IDBInteractor<JsonString>
{
    public JsonString ExecuteDSQuery(string myQuery)
    {
        return new JsonString();
    }
}

I am having trouble invoking the correct method. Ideally I would to declare something like

     SomeClass<DataTable> dt = new SomeClass<DataTable>();
     SomeClass<JsonString> js = new SomeClass<JsonString>();

     DataTable myDT = dt.ExecuteDSQuery(myQuery);
     JsonString myJson = js.ExecuteDSQuery(myQuery);

I’m not sure how to declare SomeClass. I know I could do something like

    public class SomeClass<T> where T : IDBInteractor <T>
    {
        public T ExecuteQuery(T dtobject, string myQuery)
        {
           return dtobject.ExecuteDSQuery(myQuery);
        }
    }

But I don’t want to have to pass an object instance (dtobject) to each method call.

  • 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-26T16:34:39+00:00Added an answer on May 26, 2026 at 4:34 pm

    What exactly are you trying to do? This code works fine:

    using System.Data;
    
    namespace ConsoleApplication7
    {
        internal class Program
        {
            private static void Main(string[] args)
            {
                IDBInteractor<DataTable> dt = new SomeClass<DataTable>(new DBInteractorDT());
                IDBInteractor<JsonString> js = new SomeClass<JsonString>(new DBInteractorJson());
                /*
                or you can write 
    
                IDBInteractor<DataTable> dt = new DBInteractorDT();
                IDBInteractor<JsonString> js = new DBInteractorJson();
    
                 */
    
                DataTable myDT = dt.ExecuteDSQuery("");
                JsonString myJson = js.ExecuteDSQuery("");
            }
        }
    
        public interface IDBInteractor<T>
        {
            T ExecuteDSQuery(string myQuery);
        }
    
        public class DBInteractorDT : IDBInteractor<DataTable>
        {
            #region IDBInteractor<DataTable> Members
    
            public DataTable ExecuteDSQuery(string myQuery)
            {
                return new DataTable();
            }
    
            #endregion
        }
    
        public class DBInteractorJson : IDBInteractor<JsonString>
        {
            #region IDBInteractor<JsonString> Members
    
            public JsonString ExecuteDSQuery(string myQuery)
            {
                return new JsonString();
            }
    
            #endregion
        }
    
        public class SomeClass<T> : IDBInteractor<T>
        {
            private IDBInteractor<T> interactor;
    
            public SomeClass(IDBInteractor<T> interactor)
            {
                this.interactor = interactor;
            }
    
            public T ExecuteDSQuery(string myQuery)
            {
                return interactor.ExecuteDSQuery(myQuery);
            }
        }
    
        public class JsonString
        {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Data Access Layer library that I would like to make portable.
I have a Data Access Object TransactionDao. When you call TransactionDao.Save(transaction) I would like
I have created a library which can download JSON data which is then placed
I have a data access library that has a few classes that all implement
So I have a data access class library I make a linq to entities
I have an native C++ library which makes use of a large static buffer
There's this web app, which relies on a sort of data access library (simple
I have a web app of which I would like to create a mobile
I'd like to use the OpenSSL library to decrypt some AES data. The code
We have an ASP.NET 2.0 application that uses Enterprise Library Data Access Application Block

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.