Last active
January 15, 2016 19:26
-
-
Save miklund/dc30341a68e6daf1cd14 to your computer and use it in GitHub Desktop.
2009-01-24 Method or Property
This file contains hidden or 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
# Title: Method or Property | |
# Author: Mikael Lundin | |
# Link: http://blog.mikaellundin.name/2009/01/24/method-or-property.html |
This file contains hidden or 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
// Property is data of an empty string value | |
string s = string.Empty; | |
// Action returns normalized string | |
s.Normalize(); |
This file contains hidden or 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 string username; | |
public string Username | |
{ | |
get | |
{ | |
return this.username; | |
} | |
} | |
} |
This file contains hidden or 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 IEnumerable<User> Users | |
{ | |
get | |
{ | |
IList<User> result = new List<User>(); | |
using (DbConnection connection = dbFactory.GetDbConnection()) | |
using (DbCommand command = dbFactory.CreateCommand(connection)) | |
{ | |
command.CommandText = "SELECT username FROM user"; | |
using(DbDataReader reader = command.ExecuteReader()) | |
{ | |
while (reader.Read()) | |
{ | |
User user = new User { Username = reader.GetString(0) }; | |
result.Add(user); | |
} | |
} | |
} | |
return result; | |
} | |
} |
This file contains hidden or 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
private bool isDataSourceLoaded; | |
private IEnumerable dataSource; | |
public IEnumerable DataSource | |
{ | |
get | |
{ | |
// A significant side effect where the GET-property populates the field | |
if (this.isDataSourceLoaded) | |
{ | |
this.dataSource = GetUsers(); | |
this.isDataSourceLoaded = true; | |
} | |
return this.dataSource; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment