6

I have a class with some static lists. For demonstrative purposes I'll only show two:

public class Foo
{
    public static readonly List<long> FirstList(EfEntities dbContext)
    {
        return dbContext.SomeTable.Where(x => x == 1).ToList();
    }

    public static readonly List<long> SecondList(EfEntities dbContext)
    {
        return dbContext.SomeTable.Where(x => x == 2).ToList();
    }
}

I'm not a big fan of passing my database context to each and every static method. Do you have any suggestions on different approaches?

2
  • 1
    It would help if you'd explain what you don't like about it first, and the context in which you're using it. For example, why not wrap the context in another class which has these as instance methods? Commented Jul 2, 2013 at 7:49
  • @JonSkeet In my first tests I had static hard coded lists. But I would like the code to be more data driven, so I would like to change it to the actual table keys that I hard coded in the first place. I suppose your suggested approach is the way to go. Commented Jul 2, 2013 at 8:08

4 Answers 4

5

A good idea in that case (of course if usage of static methods is justified by the architecture, but this seems to be out of scope of this question) might be creating your static methods as extension methods:

public static class EfEntitiesExtensions
{
    public static readonly List<long> FirstList(this EfEntities dbContext)
    {
        return dbContext.SomeTable.Where(x => x == 1).ToList();
    }

    public static readonly List<long> SecondList(this EfEntities dbContext)
    {
        return dbContext.SomeTable.Where(x => x == 2).ToList();
    }
}

After that you can call them like this:

...
EfEntities dbContext = new EfEntities();
List<long> firstList = dbContext.FirstList();
Sign up to request clarification or add additional context in comments.

Comments

3

I personally don't like the idea of passing a dbContext object as a parameter. You could separate the data layer completely and store it in another class.

public class DataAccess {
   private EFEntities _dbContext { get; set; }

   public EfEntities GetDbContext() {
        if (_dbContext != null) {
             return _dbContext;
        } else {
            _dbContext = new EFEntities(.....);
            return _dbContext;
        }
   }
}

Then you can reference the DataAccess class which contains the context you need rather than passing it as a parameter each time.

Comments

1

Don't make it static. You require context for those lists to be returned. Static members are designed as context less. So basically what you want is to move these methods to a class instance and provide a context upon construction via dependency injection or some other sort of factory.

What is the design decision to stick to statics? An awful workaround would be passing the context once to a static field and then use it but that's basically exactly the thing you should do with a class instance

1 Comment

Thanks for the tip. I made them static when I had hard coded lists. I suppse this is a good time to change the type
0

I know this is an old question but I did this solution. At most it is syntax sugar.

public List<Cert> List()
{
    return db.Certs.ToList();
}

public static List<Cert> All()
    {
    return new CertsController().List();
}

Usage:

List<Cert> Certificates = CertsController.All();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.