What is the Dynamic Proxy generated by the Entity Framework?

4

What is the Dynamic Proxy generated by the Entity Framework? And a class? What is the function of it?

    
asked by anonymous 07.01.2015 / 14:06

1 answer

8

The dynamic proxy works similar to the nhibernate proxy, it is a proxy of the Entity (POCO class). It is a class that overrides the virtual properties of the entity to be able to do the lazy load. That comes down to loading the data only at the time they are requested.

Lazy load is widely used in object lists (eg an employee's dependency list) but can be used in common properties (eg string property that has very long text).

Take a look at this documentation about Lazy Load, which is the on-demand load, and Eager Load is a technique that eliminates a problem called n + 1, where the ORM makes a call to the DB to fetch 1 parent record and N child records. With eager load you search for everything in one call.

Responding to your comment:

Proxy is a design pattern ( link ) that creates a surrogate class that simulates the original class . The ORMs create a surrogate class to do Lazy Load as follows: They create a proxy class with just the filled-in ID, pass it as if it were the whole class, and monitor the proxy properties. When we try to access some property that has not been loaded (eg Name) ORM goes to the database and searches for the correct value. This is the lazy load. And you're correct EF also uses the proxy to track changes (change tracking)

    
07.01.2015 / 14:12