What's "strange" is the way you've intentionally created an infinite
recursion in your code. Unfortunately, because even though you've been
informed many times in reply to past questions that without a
concise-but-complete code example, it's extremely difficult, if not
impossible, to know exactly what your problem is and thus provide useful
advice, you still have not provided a concise-but-complete code example
that reliably demonstrates the problem.
Hi Pete,
I am posting my entire code. I removed just a few methods to not be
sol long.
But if necessary I post them to.
BrandRepository:
public class BrandRepository : IBrandRepository {
private ProductRepository productRepository;
private XDocument brands;
private XDocument products;
public String Path {
get { return _Path; }
set {
_Path = value;
productRepository = new ProductRepository(String.Concat(_Path,
"Products.xml"));
brands = XDocument.Load(String.Concat(Path, "Brands.xml"),
LoadOptions.SetBaseUri);
products = XDocument.Load(String.Concat(Path, "Products.xml"),
LoadOptions.SetBaseUri);
}
} private string _Path;
public BrandRepository(String path) {
Path = path;
} // BrandRepository
// Delete
public void Delete(Guid id) {
// Delete brand
XElement brand = brands.Root.Elements("Brand").FirstOrDefault(b
=> b.Element("BrandId").Value == id.ToString());
if (brand != null) {
// Remove brand
brand.Remove();
brands.Save(new Uri(brands.BaseUri).LocalPath);
// Remove products directly on the XML file
IEnumerable<XElement> _products = products.Root.Elements
("Product").Where(p => p.Element("BrandId").Value == id.ToString());
if (_products != null) _products.Remove();
products.Save(new Uri(products.BaseUri).LocalPath);
// OR in alternative use productRepository
productRepository.Delete(brand.Element("BrandId");
}
} // Delete
// GetBrand
public Brand GetBrand(Guid id) {
Brand brand = (from b in brands.Root.Elements("Brand")
where b.Element("BrandId").Value == id.ToString()
select new Brand {
Id = new Guid(b.Element("BrandId").Value),
Created = DateTime.Parse(b.Element
("Created").Value),
Name = b.Element("Name").Value,
Updated = DateTime.Parse(b.Element
("Updated").Value)
}).SingleOrDefault();
return brand;
} // GetBrand
} // BrandRepository
ProductRepository:
public class ProductRepository : IProductRepository {
private BrandRepository brandRepository;
private XDocument Brands;
private XDocument Products;
public String Path {
get { return _Path; }
set {
_Path = value;
brandRepository = new BrandRepository(String.Concat(_Path,
"Brands.xml"));
Brands = XDocument.Load(String.Concat(Path, "Brands.xml"),
LoadOptions.SetBaseUri);
Products = XDocument.Load(String.Concat(Path, "Products.xml"),
LoadOptions.SetBaseUri);
}
} private string _Path;
public ProductRepository(String path) {
Path = path;
} // ProductRepository
// Delete
public void Delete(Guid id) {
XElement product = Products.Root.Elements
("Product").SingleOrDefault(p => p.Element("ProductId").Value ==
id.ToString());
if (product != null) product.Remove();
Products.Save(new Uri(Products.BaseUri).LocalPath);
} // Delete
// GetProduct
public Product GetProduct(Guid id) {
// Define product getting Brand directly from XML file
Product product = (from p in Products.Root.Elements("Product")
where p.Element("ProductId").Value ==
id.ToString()
select new Product {
Id = new Guid(p.Element
("ProductId").Value),
Name = p.Element("Name").Value,
Updated = DateTime.Parse(p.Element
("Updated").Value),
Brand = (from b in Brands.Root.Elements
("Brand")
where b.Element("BrandId").Value
== p.Element("BrandId").Value
select new Brand {
Id = new Guid(b.Element
("BrandId").Value),
Created = DateTime.Parse
(b.Element("Created").Value),
Name = b.Element("Name").Value,
Updated = DateTime.Parse
(b.Element("Updated").Value)
}).SingleOrDefault()
}).SingleOrDefault();
// OR define product getting the product Brand using Brand
repository
Product product = (from p in Products.Root.Elements("Product")
where p.Element("ProductId").Value ==
id.ToString()
select new Product {
Id = new Guid(p.Element
("ProductId").Value),
Name = p.Element("Name").Value,
Updated = DateTime.Parse(p.Element
("Updated").Value),
Brand = brandRepository.Get(p.Element
("BrandId").Value),
}).SingleOrDefault();
return product;
} // GetProduct
} // ProductRepository
In BrandRepository Delete method and in ProductRepository GetProduct I
display two options of my code:
One it uses the XML file; the other accesses the other repository.
When using the other repository I avoid having "duplicated" code on my
repositories.
For example if I change my Brands.xml file structure I don't need to
change code in various places.
The Path is common to all XDocuments or repositories. What changes is
the file that it refers to.
I define the XDocuments on the Path property because they use Path
value and they are used in almost all methods on the repository.
I understood Brian suggestion but if the dependence between
repositories increase with more repositories I think it will be a
little bit confused.
I feel that calling repositories from another repositories feels
strange to me ... But having duplicated code to.
So I am not sure what should I do.
I hope I explained it well.
Thanks,
Miguel