Skip to content

Instantly share code, notes, and snippets.

@ferclaverino
Created July 12, 2012 22:46
Show Gist options
  • Save ferclaverino/3101569 to your computer and use it in GitHub Desktop.
Save ferclaverino/3101569 to your computer and use it in GitHub Desktop.
Single responsability example, step 3
class FileLoader : IFileLoader
{
public Stream Load(string fileName)
{
return new FileStream(fileName, FileMode.Open);
}
}
class ProductRepository : IProductRepository
{
private readonly IFileLoader loader;
public ProductRepository()
{
loader = new FileLoader();
}
public IEnumerable<Product> GetByFileName(string fileName)
{
var products = new List<Product>();
using (Stream input = loader.Load(fileName))
{
var reader = XmlReader.Create(input);
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