C# and Entityframework - Best practices while working

I have my BAL project, which has many methods nearly 20 methods which will interact with database.

Public EmployeeInfo GetTotalCount(int Id)
{
  using(EmployeeEntities empDBContext = new EmployeeEntities())
  {
  var employee = empDBContext.sp_getEmployeDetails(Id).FirstOrDefault();
  }
}

Just like the above i have almost 20 methods, do i need to use the using block for all the 20 methods ??
using(EmployeeEntities empDBContext = new EmployeeEntities())

I can create below object globally on the form, and reuse empDBContext on all the methods, will coding in this way make any impact on performace ??
EmployeeEntities empDBContext = new EmployeeEntities()

That’s tricky as the best practice here varies per situation…
The general rule is that objects should live for short moments only. The using blocks just makes sure that you won’t use a database for longer than the minimum amount of time required. That’s good, especially if there are other users connected to the same database as the number of connections tend to be limited.

But connecting to a database tends to be a slow action and especially in the past it was considered a good practice to keep a connection open to the database for as long as the system needed data. That had it’s advantages and can still be practical these days.

But here, empDBContext isn’t a database context! It’s type is EmployeeEntities so it has a totally weird name! It should be named ‘employees’ or whatever. And it seems to call a stored procedure that returns something of type EmployeeInfo based on the ID. Which is supposed to be a total count…

Nope… The code doesn’t even make sense so there’s no way you can start with best practices any more. You’re already deep in bad practices here…

The best practice here would be by creating extension methods for your database context. Something like:
public static something GetSomething(this DbContext ctx, int Id){ ... }
These extension methods would contain the 20+ methods you have. As for the using block… Those would be used outside the function for the database context. But it would still be best practice if the creation of the context happens within a single method so the context is cleared when the function ends…

Keep in mind that databases and the context isn’t generally thread-safe, while more and more code is using asynchronous methods to process data. Especially in a GUI application I would assume the use of a lot of asynchronous code to keep the application active.

But your code example is a mess. The names are unclear…

I would suggest to use Dependency Injection and inject Context in your BAL class. Set life time of your dependency whether its singleton, once per call or whatsever. This way your object will be kept in case of Singleton to be reused whenever necessary. Singleton call to dependency is good when you have to use some resource multiple times. And using it per call will automatically destroy instance as soon as call completes.

It could be further discussed based on proper working example of code.

Happy coding

Nabeel

.NET Foundation Website | Blog | Projects | Code of Conduct