Is it necessary to implement IDisposable when using Entity Framework in MVC?

I see in many examples the example of MVC , Repository , Unit of Work, and EF , for example here , that both interfaces and classes implement the IDisposable interface. I assume that this interface provides only the Dispose() method with two overloads.

However, in many other examples made by senior programmers, I do not see such an implementation. Actually, it seems logical to me that one component is rejected with every web request, since each request receives an instance of the brand new controller.

Or, even if this is not the case, I assume that using the dependency injection infrastructure such as Ninject , we delegate all these removal tasks to the platform itself.

It may also be that the implementation of IDisposable is required in an older version of the EF or MVC framework.

Can anyone point me in the right direction?

UPDATE

Automatic deletion of context can be seen in a multilevel application using the Service and the Repository . Suppose we return IQueryable<T> objects from both components, if we try to populate the objects from the controller, iterate over its elements or call the ToList() method, we get a runtime error indicating that the context is unreachable (closed)

+4
source share
3 answers

A common template is to have a repository instance in each controller and associate it with the controller's Dispose () Dispose ().

So, I would say that a template is usually required.

However, in many other examples made by senior programmers, I do not see such an implementation.

There are several possibilities:

  • this is a demo code, error and resource handling omitted.
  • the template is implemented in a non-obvious place (base class)

Give a specific example, and we can find out.

+3
source

Usually in most examples that you can find in the interner about the Repository template using EF, Context has a Dispose method.

Now you don’t just need to call the Dispose method on Context , but this may be good practice for the following reason:

The DataContext contains state (for example, SqlConnection and pointers to the objects you received). They will eventually be cleared by the GC after you release all the links, but some of these objects (for example, the base SqlConnection) may contain resources that you usually want to release as soon as you finish, rather than relying on the GC to clean.

For lazy people: you can even wrap the context in a using statement that will call dispose for you when the object leaves the scope of use itself

Additional Information:

http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/2625b105-2cff-45ad-ba29-abdd763f74fe/

here you can also find an example regarding the repository in EF

http://www.codeproject.com/Tips/309753/Repository-Pattern-with-Entity-Framework-4-1-and-C

+2
source

Regarding the state of the connection, EF is supposed to only support opening the connection well only when necessary ( http://msdn.microsoft.com/en-us/library/bb896325.aspx ). The link also shows how to get more Fine control over the state of the connection.

+2
source

Source: https://habr.com/ru/post/1403617/


All Articles