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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:13:01+00:00 2026-06-02T17:13:01+00:00

I’m working on an app where I need to call one of two data

  • 0

I’m working on an app where I need to call one of two data methods based on the generic type of the calling class. For example, if T is of type Foo, I’ll call data.GetFoo():

private static List<T> GetObjectList(DateTime mostRecentProcessedReadTime)
{
    using (MesReportingDal data = new MesReportingDal())
    {
        return data.GetFoo(mostRecentProcessedReadTime);  // Notice GetFoo()
    }
}

And if T is of type Bar, I’ll call data.GetBar():

private static List<T> GetObjectList(DateTime mostRecentProcessedReadTime)
{
    using (MesReportingDal data = new MesReportingDal())
    {
        return data.GetBar(mostRecentProcessedReadTime);  // Notice GetBar()
    }
}

Before now, I only needed one DAL method because all types were retrieved the same way. I now need to call one of two methods, depending on the type of T.

I’m trying to avoid something like this:

private static List<T> GetObjectList(DateTime mostRecentProcessedReadTime)
{
    using (MesReportingDal data = new MesReportingDal())
    {
        if (T is Foo) { return data.GetFoo(mostRecentProcessedReadTime); }
        if (T is Bar) { return data.GetBar(mostRecentProcessedReadTime); }
    }
}

This violates OCP. Is there an elegant way to handle this, so I can get rid of my if statement?

Edit – This is what the types look like

public partial class Foo1 : IDataEntity { }
public partial class Foo2 : IDataEntity { }
public partial class Bar1 : IDataEntity { }
public partial class Bar2 : IDataEntity { }

These Foos and Bars are the DBML items used with Linq-to-SQL.

  • 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-06-02T17:13:03+00:00Added an answer on June 2, 2026 at 5:13 pm

    I would change GetFoo and GetBar to just be Get, and make MesReportingDal a generic too.

    So I think you would end up with something like this:

    private static List<T> GetObjectList(DateTime mostRecentProcessedReadTime)
    {
        using (var data = new MesReportingDal<T>())
        {
            return data.Get(mostRecentProcessedReadTime);        
        }
    }
    

    Incidentally, having the using statement also requires that the MesReportingDal implements IDisposable, otherwise you’ll get the following compile error:

    ‘MesReportingDal’: type used in a using statement must be implicitly convertible to ‘System.IDisposable’

    UPDATE

    So after thinking about this some more and reading your feedback, one option you have is to extract a repository interface and push the creation back to a factory method. This will allow you to maintain the single data.Get(...) call, but with different implementations based on T

    public interface IRepository<T> : IDisposable
    {
        IList<T> Get(DateTime mostRecentRead);
    }
    
    public class FooRepo : IRepository<Foo>
    {
        public IList<Foo> Get(DateTime mostRecentRead)
        {
            // Foo Implementation
        }
    }
    
    public class BarRepo : IRepository<Bar>
    {
        public IList<Bar> Get(DateTime mostRecentRead)
        {
            // Bar Implemenation
        }
    }
    

    Your factory could then look something like this

    public class RepositoryFactory
    {
        public static IRepository<T> CreateRepository<T>()
        {
            IRepository<T> repo = null;
            Type forType = typeof(T);
    
            if (forType == typeof(Foo))
            {
                repo = new FooRepo() as IRepository<T>;
            }
            else if (forType == typeof(Bar))
            {
                repo = new BarRepo() as IRepository<T>;
            }
    
            return repo;
        }
    }
    

    And this would allow you to keep your initial code block clean

    private static IList<T> GetObjectList(DateTime mostRecentProcessedReadTime)
    {
        using (var data = RepositoryFactory.CreateRepository<T>())
        {
            return data.Get(mostRecentProcessedReadTime);
        }
    }
    

    Hope that helps.

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

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.