Skip to content

Instantly share code, notes, and snippets.

@developerdizzle
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developerdizzle/4c8bde6a055894072520 to your computer and use it in GitHub Desktop.
Save developerdizzle/4c8bde6a055894072520 to your computer and use it in GitHub Desktop.
Lazy-loading ghost example
public class User
{
private int _id;
private bool _loaded = false;
private string _firstName;
private string _lastName;
public User(int id)
{
_id = id;
}
private void InitializeIfNecessary()
{
if (!_loaded)
{
var user = db.Users.SingleOrDefault(u => u.UserId = _id);
_firstName = user.FirstName;
_lastName = user.LastName;
_loaded = true;
}
}
public string FirstName
{
get
{
this.InitializeIfNecessary();
return _firstName;
}
}
public string LastName
{
get
{
this.InitializeIfNecessary();
return _lastName;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment