Created
July 12, 2012 22:46
-
-
Save ferclaverino/3101569 to your computer and use it in GitHub Desktop.
Single responsability example, step 3
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
class FileLoader : IFileLoader | |
{ | |
public Stream Load(string fileName) | |
{ | |
return new FileStream(fileName, FileMode.Open); | |
} | |
} |
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
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