A Proxy is a "Stand-in" for another object. It has the same interface as the original, but it intercepts all calls to allow for extra logic like **Lazy Loading**, **Security checks**, or **Logging**. It is the foundation of Aspect-Oriented Programming (AOP).
Why load a 50MB image from the database if the user might never scroll down to see it? The Proxy returns a "Placeholder" immediately and only performs the heavy database query the second someone calls .Render().
public class ImageProxy : IImage
{
private RealImage _realImage;
private string _filename;
public void Display()
{
// Only create the heavy object when EXPLICITLY needed!
if (_realImage == null) _realImage = new RealImage(_filename);
_realImage.Display();
}
}
The Proxy checks if the current user is an 'Admin' before passing the call to the actual DeleteDatabase() method. This keeps your business logic classes clean because they don't have to contain security checks.
Q: "How does Entity Framework Core use Proxies for Lazy Loading?"
Architect Answer: "When you mark a property as `virtual`, EF Core creates a dynamic class (a Proxy) that inherits from your entity. When you access `user.Posts`, you aren't talking to your class; you are talking to the Proxy. The Proxy detects the property access, fires a SQL query to the database, populates the list, and then returns the data. To the developer, it looks like magic, but it is actually the Proxy pattern intercepting property access."