Hi, all. I am using Entity Framework and decided to do some dependency injection in order to make my functions more testable. This seemed to bring up a few questions that I found difficult to solve. I guess the difficult part is figuring out the best practice of doing this, rather. So, to cut it short, I'm using Moq for mocking up the context class and the datasets. The class looks something like this:

public class Auth {
    private dbcontext _ctx;
    public Auth(dbcontext ctx) {
        _ctx = ctx;
    public bool Authenticate(string usr, string psw)
        var a = _ctx.accounts.Where( ... )
        return true;

Now, before I used constructor injection, I did like this inside every function in the class that needed database access: using (var ctx = new dbcontext()) {} I figured I could now (with the new class) just write the constructor inside the using statement when I instantiate the class and then use the object outside the using statement. The code looks very messy if I were to put everything in a using statement. Now the context object gets disposed, naturally, so instead I wrote it like this:

Auth auth(new dbContext());
auth.Authenticate("usr", "psw");

Is this an okay solution? What's your opinion? I'm quite new to dependency injection with EF, so be kind :)

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.