Skip to content

Instantly share code, notes, and snippets.

@miklund
Last active January 15, 2016 19:26
Show Gist options
  • Save miklund/dc30341a68e6daf1cd14 to your computer and use it in GitHub Desktop.
Save miklund/dc30341a68e6daf1cd14 to your computer and use it in GitHub Desktop.
2009-01-24 Method or Property
# Title: Method or Property
# Author: Mikael Lundin
# Link: http://blog.mikaellundin.name/2009/01/24/method-or-property.html
// Property is data of an empty string value
string s = string.Empty;
// Action returns normalized string
s.Normalize();
public class User
{
private string username;
public string Username
{
get
{
return this.username;
}
}
}
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;
}
}
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