Skip to content

Instantly share code, notes, and snippets.

@ferclaverino
Created July 12, 2012 22:40
Show Gist options
  • Save ferclaverino/3101538 to your computer and use it in GitHub Desktop.
Save ferclaverino/3101538 to your computer and use it in GitHub Desktop.
Single responsability example, step 2
private void btnLoad_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
var fileName = txtFileName.Text;
foreach (Product product in productRepository.GetByFileName(fileName))
{
var item = new ListViewItem(new[] {
product.Id.ToString(),
product.Name,
product.UnitPrice.ToString(),
product.Discontinued.ToString()
});
listView1.Items.Add(item);
}
}
class ProductRepository : IProductRepository
{
public IEnumerable<Product> GetByFileName(string fileName)
{
var products = new List<Product>();
using (var fs = new FileStream(fileName, FileMode.Open))
{
var reader = XmlReader.Create(fs);
while (reader.Read())
{
if (reader.Name != "product") continue;
var product = new Product();
product.Id = int.Parse(reader.GetAttribute("id"));
product.Name = reader.GetAttribute("name");
product.UnitPrice = decimal.Parse(reader.GetAttribute("unitPrice"));
product.Discontinued = bool.Parse(reader.GetAttribute("discontinued"));
products.Add(product);
}
}
return products;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment