-
-
Save developerdizzle/4c8bde6a055894072520 to your computer and use it in GitHub Desktop.
Lazy-loading ghost example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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