locked
Infinite loop or Infinite recursion RRS feed

  • Question

  • Hello,

    I have a class named BrandRepository:

      public class BrandRepository : IBrandRepository {
        private ProductRepository productRepository;
        public String Path {
          get { return _Path; }
          set {
            _Path = value;
            productRepository = new ProductRepository(String.Concat(_Path,
    "Products.xml"));
          }
        } private string _Path;

    And a ProductRepository:

     public class ProductRepository : IProductRepository {
        private BrandRepository brandRepository;
        public String Path {
          get { return _Path; }
          set {
            _Path = value;
            brandRepository = new BrandRepository(String.Concat(_Path,
    "Brands.xml"));
          }
        } private string _Path;

    }

    For example, when I get a Product I have the BrandId and with it I need to fill the other Brand properties.

    So I end up with circular reference.
    The alternative is to place the Linq code for XML in all my repositories.
    But then I will have a lot of repeating code.
    And if I change something on the XML files structure I will need to change the same code in many places.

    Is there a way to fix this circular reference.
    Maybe some kind of implementation to avoid this and yet allow to keep the objectives.

    Thanks,
    Miguel
    Friday, July 24, 2009 7:36 PM

Answers

  • 1. Pick one to construct first.  (Let's say you pick BrandRepository).
    2. In the constructor for the other one add an incoming parameter accepting the type specified in step one. (public ProductRepository(BrandRepository brand), for example).
    3. Set the class-level variable to the parameter passed in.

    public class BrandRepository : IBrandRepository {
        private ProductRepository productRepository;
        public String Path {
          get { return _Path; }
          set {
            _Path = value;
            productRepository = new ProductRepository(this, String.Concat(_Path,
    "Products.xml"));
          }
        } private string _Path;

    And a ProductRepository:

     public class ProductRepository : IProductRepository {
        private BrandRepository brandRepository; 

        public class ProductRepository(BrandRepository brand, string xml)
        {
            brandRepository = brand;
            // do something with the XML parameter also. 
            _Path = xml;
        }


        public String Path {
          get { return _Path; } 
        }

        private string _Path;

    }


    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    • Proposed as answer by Reed Copsey, JrMVP Friday, July 24, 2009 7:45 PM
    • Marked as answer by MDMoura Wednesday, July 29, 2009 12:13 PM
    Friday, July 24, 2009 7:42 PM
  • Please visit www.asp.net, or one of the Architectural forums.

    There is no definitive answer to your question. 
    Sorry.  Your descriptions are too vague for me.

    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Bin-ze Zhao Wednesday, July 29, 2009 7:19 AM
    Saturday, July 25, 2009 1:33 PM

All replies

  • 1. Pick one to construct first.  (Let's say you pick BrandRepository).
    2. In the constructor for the other one add an incoming parameter accepting the type specified in step one. (public ProductRepository(BrandRepository brand), for example).
    3. Set the class-level variable to the parameter passed in.

    public class BrandRepository : IBrandRepository {
        private ProductRepository productRepository;
        public String Path {
          get { return _Path; }
          set {
            _Path = value;
            productRepository = new ProductRepository(this, String.Concat(_Path,
    "Products.xml"));
          }
        } private string _Path;

    And a ProductRepository:

     public class ProductRepository : IProductRepository {
        private BrandRepository brandRepository; 

        public class ProductRepository(BrandRepository brand, string xml)
        {
            brandRepository = brand;
            // do something with the XML parameter also. 
            _Path = xml;
        }


        public String Path {
          get { return _Path; } 
        }

        private string _Path;

    }


    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    • Proposed as answer by Reed Copsey, JrMVP Friday, July 24, 2009 7:45 PM
    • Marked as answer by MDMoura Wednesday, July 29, 2009 12:13 PM
    Friday, July 24, 2009 7:42 PM
  • And beside this approach is there a better way to do this?

    Consider I have more dependencies?

    Inside each repository I have methods that use Linq to query XML files.
    The XML files are planned as they were SQL tables.

    In this case I have only two repositories related but in the future I will a few more.
    Maybe I should create some base class or plan my repositories in a different way ... I don't know ... I am just wondering.

    For example, imagine I have:
    Posts > TagsInPosts < TagsInPosts
    Brands > Products > Files

    I mean if this can be more complex should I just have the Linq code in each repository without accessing other repositories?

    Thank You,
    Miguel
    Friday, July 24, 2009 7:51 PM
  • Yeah, it's hard to tell without seeing and analyzing your entire system.  I'm not completely sure what you're trying to accomplish, and what architecture you're trying to implement, but recursion within a set of repositories is a little odd.  If you were using Entity Framework, this should all be handled for you, but the simplest way seems to be to use a constructor and pass the same reference in. 
    David Morton - http://blog.davemorton.net/ - @davidmmorton - ForumsBrowser, a WPF MSDN Forums Client
    Friday, July 24, 2009 7:53 PM
  • But Entity Framework can be used with XML files?

    I am asking this because in my large projects I use SQL 2008 and LINQ.
    But when the project is small and the client does not want to pay for the SQL Server which usually needs to be paid on the hosting company I tend to use XML files.

    I was planning already to start using Entity Framework for SQL 2008 but I didn't know I could use it for XML files. Can I?
    Friday, July 24, 2009 8:04 PM
  • Your classes are more like queries than actual tables. 
    They appear to be two representations or views of the same underlying data structure. 
    Define that underlying structure.

    What is the basic architectural pattern that you are using?  Domain Model?
    Ever heard of Martin Fowler?


    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, July 24, 2009 8:05 PM
  • Your classes are more like queries than actual tables. 
    They appear to be two representations or views of the same underlying data structure. 
    Define that underlying structure.

    What? Yes I have models of my XML files. For example, for Brand I have:

      public class Brand {
        public Guid Id { get; set; }
        public String Name { get; set; }
        public DateTime? Created { get; set; }
        public DateTime? Updated { get; set; }
      } // Brand
    Then on my BrandRepository, that implements IBrandRepository, I have methods like:
        // Create
        public void Create(Brand brand) {
    
          // Create brand
          XElement _brand = new XElement("Brand",
            new XElement("BrandId", brand.Id == Guid.Empty ? Guid.NewGuid().ToString() : brand.Id.ToString()),
            new XElement("Created", DateTime.UtcNow.ToString()),
            new XElement("Name", brand.Name),
            new XElement("Updated", DateTime.UtcNow.ToString())
          );
          brands.Root.Add(_brand);
          brands.Save(new Uri(brands.BaseUri).LocalPath);
    Do you mean something different?

    Friday, July 24, 2009 8:30 PM
  • I am working ASP.NET MVC (Model View Controller) so I have models for everything.

    Then have repositories that work between the data and the controllers.
    Friday, July 24, 2009 8:31 PM
  • Please visit www.asp.net, or one of the Architectural forums.

    There is no definitive answer to your question. 
    Sorry.  Your descriptions are too vague for me.

    Mark the best replies as answers. "Fooling computers since 1971."
    • Marked as answer by Bin-ze Zhao Wednesday, July 29, 2009 7:19 AM
    Saturday, July 25, 2009 1:33 PM