entity framework - Instantiating a context in LINQ to Entities -


I have seen two different etiquette that programmers approach when making reference to an entity in their code.

The first is, and you can find it in all examples of the MSDN code:

  public zero doSomething (using (taxable education education reference = new Taxable education) () Business / business logic whatever}}  

Secondly, in certain classes to create a reference in the form of a personal feature, your business logic Comes in front of you: So you will have something like this:

  Public class education_ LINQ {personal tax Physical Education Education = New Taxable Education (); Public Zero DoSomething () Select one of the {var results = one context.luAction; // Business logic and whatever}}  

How is it more efficient?

Assume that you have two methods, which are called DoSomething1 () and the second is called DoSomething2 (), and the context in both ways Include the statement that you use to open it and whatever you do with it Would you ask a method to call after another, is there a lot overhead, because basically they make references in both ways and then when they work they clear it? When a square object is installed, and then when the object is scratched again it is only opposed to creating a personal feature when it is cleaned up again?

Each time a new object is created, some 'overhead' is included in the originally included overhead one The copy of the metadata from the global cache relates to the specific objectTextExt.

This overhead is relatively minor, so often it is not worth worrying about it, especially when you have additional security in use pattern of usage.

The options that you choose for me depends on things:

  1. How long is it possible to have your wrapping class? If it lasts for a long time, objectcontact can increase to slow down many institutions to slow down. Therefore, every time a new object contactist can be a good idea.
  2. Are there calls to synchronized methods of your wrapping class? ObjectContestx class is not a threadseaf, so if you use another tape then you need to make sure that your wrapping class / repository thread is secure if you expect to call multiple threads.
  3. What methods are essentially unrelated? If so, you can get unexpected side effects, if they share a context between the methods.

My recommendation in general is that if the methods are stateless, i.e. forget the fire and forget a new reference for each method is probably a good idea.

If you have a relatively short term structure or something, then perhaps the shared context is a better idea.

UPDATE: I have time to put together


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -