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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:14:15+00:00 2026-05-11T19:14:15+00:00

Just found a bit of code someone here had written to access some DB

  • 0

Just found a bit of code someone here had written to access some DB Entities…

public static OurCustomObject GetOurCustomObject(int primaryKey)
{
    return GetOurCustomObject<int>(primaryKey, "usp_GetOurCustomObjectByID");
}

public static OurCustomObject GetOurCustomObject(Guid uniqueIdent)
{
    return GetOurCustomObject<Guid>(uniqueIdent, "usp_GetOurCustomObjectByGUID");
}

private static OurCustomObject<T>(T identifier, string sproc)
{

    if((T != typeof(int)) && (T == typeof(Guid)))
    {
        throw new ArgumentException("Identifier must be a string or an int");
    }

    //ADO.NET Code to make DB Call with supplied sproc.
}

Theres just something about it that doesn’t seem very generic. The fact that the
sprocs are passed into the inner method feels ugly. but the only way I can see around that is to have an if/else in the private method along the lines of

if(type == int)
    sproc = "GetByID";
else if (type == Guid)
    sproc = "GetByGUID";

Also the exception throwing looks ugly as well… is there anyway to use a where T : clause

e.g.

private static OurCustomObject<T>(T identifier) where T : int OR Guid

Any suggestions on how to clean this up a little.

  • 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-11T19:14:15+00:00Added an answer on May 11, 2026 at 7:14 pm

    You can’t specify a constraint which says “it’s one of these two”, no.

    What you could do is:

    Dictionary<Type, string> StoredProcedureByType = new Dictionary<Type, string>
    {
        { typeof(Guid), "GetByGUID" },
        { typeof(int), "GetByID" }
    };
    

    Then use:

    string sproc;
    if (!StoredProcedureByType.TryGetValue(typeof(T), out sproc))
    {
        throw new ArgumentException("Invalid type: " + typeof(T).Name);
    }
    

    This is probably overkill for just a couple of types, but it scales well if you have a lot of types involved.

    Given that both types are value types, you can make it a bit more robust with a constraint of:

    where T : struct
    

    but that will still allow byte etc.

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

Sidebar

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are the delegate and dataSource properties of both tables pointing… May 12, 2026 at 1:14 am
  • Editorial Team
    Editorial Team added an answer If the encoding isn't correctly specified, I think you're going… May 12, 2026 at 1:14 am
  • Editorial Team
    Editorial Team added an answer Good question, I think you covered all the layers I… May 12, 2026 at 1:14 am

Related Questions

I've come across a switch statement in the codebase I'm working on and I'm
Is there any way to create a ViewDataDictionary with a model and additional properties
I've been looking for some guidelines on how to layout PHP code. I've found
My boss has come to me and asked how to enure a file uploaded

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.