How many times does a compiled query have to be recompiled during the application life cycle?

On a website, if I have a class:

public class Provider { static readonly Func<Entities, IEnumerable<Tag>> AllTags = CompiledQuery.Compile<Entities, IEnumerable<Tag>> ( e => e.Tags ); public IEnumerable<Tag> GetAll() { using (var db = new Entities()) { return AllTags(db).ToList(); } } } 

On the page I have:

 protected void Page_Load(object sender, EventArgs ev) { (new Provider()).GetAll(); } 

How many times will the request be compiled? Every time a page loads ...? Once in an application ...?

+7
source share
5 answers

See that it compiled. I would say it once. Why recompile this? Isn't that the point of compiled queries?

Given that the compiled request is static, once per application instance / lifetime. Note. Life times may overlap.

+1
source

since it is a static member, once when the class is loaded into the application domain.

+4
source

I would say once per AppDomain, as this is a static variable.

+1
source

If you define an AllTags query this way, it will be compiled only once. Check out the blog post about compiled requests in web applications and web services Julie Lerman.

+1
source

http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx#Y696

"Static elements are initialized before the static member is available for the first time, and before the static constructor, if called."

Thus, it will compile a maximum each time the page is loaded. Since your class does not have a static constructor, it should not compile until you gain access to the static member. (According to MSDN.)

However, does this compile? It seems you are trying to load a static member from the generated class.

-one
source

All Articles