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

The Archive Base Latest Questions

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

I have many methods like this: public IEnumerable<Director> GetAllDirectors() { using (var dbContext =

  • 0

I have many methods like this:

    public IEnumerable<Director> GetAllDirectors()
    {
        using (var dbContext = new BodModelContainer())
        {
            return dbContext.Members.OfType<Director>().ToList();
        }
    }

..or this..

    public Member GetMemberById(int memberId)
    {
        using(var dbContext = new BodModelContainer())
        {
            return dbContext.Members.Find(new[] {memberId});
        }
    }

or that:

    public SomeType GetOrDoSomethingWithDbBySomethingElse(T1 t1var, T2, T2 var, ...)
    {
        using(var dbContext = new BodModelContainer())
        {
            return dbContext.SomeType.DoSomething();
        }
    }

So I wanna reorganize all of these methods something like:

    // create db context here
    public void InDb(Func<BodModelContainer, out SomeGenericType???> workingWithDb)
    {
         using(var dbContext = new BodModelContainer())
         {
             workingWithDb(dbContext);
         }
    }

    // using it like
    public Member GetMemberById(int memberId)
    {
            InDb((dbContext) => 
            { 
                    return dbContext.Members.Find(new[] { memberId }); 
            });
    }

Question: How to make such generic InDb method, what is SomeGenericType???

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

    The actual problem it looks like you’re describing is the fact that you can’t figure out how to set the return type of the Func<>. To be able to make this generic you’ll need to make InDb require a generic type as well.

    public TRet InDb<TRet>(Func<BodModelContainer, TRet> workingWithDb) {
      TRet ret = default(TRet);
      using (var dbContext = new BodModelContainer()) {
        ret = workingWithDb(dbContext);
      }
      return ret;
    }
    
    public Member GetMemberById(int memberId) {
      return InDb(dbContext => { return dbContext.Members.Find(new[] { memberId }); });
    }
    

    Should work (all the code here is untested), or using anonymous methods you could have a local variable and make .Net do all the nasty work.

    public void InDb(Action<BodModelContainer> workingWithDb) {
      using (var dbContext = new BodModelContainer()) {
        workingWithDb(dbContext);
      }
    }
    
    public Member GetMemberById(int memberId) {
      Member member;
      InDb(dbContext => { member = dbContext.Members.Find(new[] { memberId }); });
      return member;
    }
    

    Of course, this all being said, I don’t know if this level of redirection/abstraction is useful – you’re making it slightly more complicated with little perceptible gain (if any). Unless there is a lot of work setting up and tearing down the dbContext used in InDb I don’t think it is very helpful.

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

Sidebar

Related Questions

I have a WCF service with many methods. I would like that after executing
I have an ActiveX/COM DLL. It contains many methods and properties. I would like
Not many are aware of this feature, but Python's functions (and methods) can have
I'm using NHibernate 3.2 and I have a repository method that looks like: public
I have a test method created using Selenium, something similar to this: [TestFixture] public
I have many methods which are calling using Delegate.DynamicInvoke . Some of these methods
Let's say I have a function that looks like this: public void saveBooking(/* some
I have a method like this: public void foo(String str){ do.something(); do.anotherthing(); do.somethingelse(); if(str.compareTo(choiceA)==0){
I have a form that has many long-methods. My quest is: What is the
I have a base class for many tests that has some helper methods they

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.