Performance effects with object instances in EntityFramework

What behavior should be used for an entity infrastructure provider template?

public class TestProvider : IDisposable { public Entities entities = new Entities(); public IEnumerable<Tag> GetAll() { return entities.Tag.ToList(); } public ... #region IDisposable Members public void Dispose() { entities.Dispose(); } #endregion } 

Or is it ok to use:

 public class TestProvider { public IEnumerable<Tag> GetAll() { using (var entities = new Entities()) { return entities.Tag.ToList(); } } public ... } 

Does this imply performance? What are the pros and cons?

+4
source share
1 answer

It depends on how long the TestProvider should exist and what operations you want to perform on the extracted objects. Typically, an ObjectContext instance should be used as soon as possible, but it should also be a single unit of work. An instance of an ObjectContext should not be shared. I answered the corresponding question here .

This means that both approaches are suitable for some scenarios. The first approach is approved if you expect to receive objects, modify them, and save them with the same provider instance. The second approach is approved if you just want to get objects, you don’t want to change them right away, and you don’t want to select anything else.

+1
source

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


All Articles