Created
July 12, 2012 22:40
-
-
Save ferclaverino/3101538 to your computer and use it in GitHub Desktop.
Single responsability example, step 2
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
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); | |
} | |
} |
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
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