I'm wondering what would be best practice in this scenario:
I have method which calls some other method in using statement (disposable Database Context), and that other method also needs to access Database Context, so this is option 1:
using (var db = new Database())
{
// some code here
//calling other function
var cnt = SomeFunction();
}
int SomeFunction()
{
using (var db = new Database())
{
// some code here for example:
return db.Users.Count();
}
}
And here is option 2:
using (var db = new Database())
{
// some code here
//calling other function
var cnt = SomeFunction(db);
}
int SomeFunction(Database db)
{
return db.Users.Count();
}
Note: Database
looks smth. like this:
public class Database : IdentityDbContext<User>
{
public Database()
: base("DefaultConnection", throwIfV1Schema: false)
{
Configuration.LazyLoadingEnabled = true;
}
public DbSet<IdentityUser> AspNetUsers { get; set; }
}
I would go with option 2, because there is no need for initialization of Database Context again, but I'm not sure if this is correct way of doing this.