Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 488793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:44:36+00:00 2026-05-13T01:44:36+00:00

So, the object model is rather complex and hierarchical. Grandfather accepts is created by

  • 0

So, the object model is rather complex and hierarchical.
Grandfather accepts is created by a GrandfatherFactory that accepts a GrandfatherRepository in it’s constructor.

All is well.

Now when Grandfather needs to load it’s Children how does it do it?

How does it know about the ChildFactory and ChildRepository? It shouldn’t correct?

But then how does it load them? I wouldn’t think you pass a ChildFactory into the Grandfather constructor, as if you have Grandchildren (which this app does) you’d need to pass in a GrandchildFactory as well!

Do you instantiate some kind of EverythingFactory that has all of the different types of factories inside of it and pass that into each constructor and they make use of the one they care about?

Or is there some simple solution I’m just not seeing?

Currently all the objects call a webservice to load their data, and I’m trying to pull that out and make a Repository interface that can be mocked for testing purposes.

EDIT:
Ignore the factories, I don’t need them, I think I was just over-complicating things.

Currently the objects load their data, and child data by way of webservice calls scattered about in the objects.

Most of the objects constructors are marked private, and they have static methods like “LoadByID(int)” to retrieve them from the db.

Then when the property which contains it’s child methods is referenced, if it’s private “child list” variable is empty, it calls Child.GetAllChildrenFor(this); which is a static method of Child, and “this” is the Parent class. It calls the webservice, creates all the Childs, and returns a list of them, each of which has a reference to it’s parent.

I know this isn’t quite right…but I don’t know where I’ve gone wrong, or how to correct it to be able to REMOVE the webservice calls and put in a Repository interface that can be real or mock… and where in the code I set which to use!

EDIT2:
The code is something along these lines currently

public interface IEntity
{
    int ID { get; set; }
}

public class Parent : IEntity
{
    public int ID { get; set; }

    private Children children;
    public Children Children 
    { 
        get 
        {
            if (this.children == default(Children)) 
            {
                this.children = Children.GetChildrenFor(this);
            }
            return this.children;
        }
    }
}

public class Children : List<Child>
{
    private Children() { }

    public static Children GetChildrenFor(Parent parent)
    {
        Children children = new Children();

        DataSet childrenDataSet = new DataSet();
        using (webservice) 
        {
            childrenDataSet = webservice.Retrieve(parent.ID)
        }

        if (childrenDataSet.HasRows())
        {
            foreach (DataRow dr in childrenDataSet.Tables[0].Rows)
            {
                Child rr = Child.LoadByDataRow(dr);
                rr.Parent = parent;
                children.Add(rr);
            }
        }

        return children;
    }
}

public class Child : IEntity 
{
    public Parent Parent { get; set; }
    public int ID { get; set; }

    internal static Child LoadByDataRow(DataRow dr)
    {
        Child child = new Child();

        child.ID = dr.Field<int>("ID");
        child.GrandChild = GrandChildren.GetGrandChildrenFor(this);

        return child;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T01:44:36+00:00Added an answer on May 13, 2026 at 1:44 am

    One way to factor this code out of static methods and make it mock-able is to create an IRepository interface that defines a contract for classes that will supply your entities.

    public interface IRepository {
        Parent GetParent(int parentId);
        List<Child> GetChildrenByParent(int parentId);
        // ...
    }
    

    Next, change your entities to require an IRepository in their constructors:

    public class Parent: IEntity {
        private IRepository repository;
        public Parent(IRepository repository) {
            this.repository = repository;
        }
        // ...
    }
    

    A concrete IRepository implementation will contain code to call web services and map their results to each of your entities; it will have to pass itself in when it constructs entities, like this:

    public class ConcreteRepository: IRepository {
        public Parent GetParent(int parentId) {
            Parent parent = new Parent(this);
            // Call web service, perform mapping, etc.
        }
    }
    

    Entities constructed like this can use the repository as needed – for example, in the Parent.Children get accessor. Your applications will use a real repository, while unit tests can supply a mock or stub.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 242k
  • Answers 242k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Setting it in the environment by modifying os.environ['CXXFLAGS'] should work.… May 13, 2026 at 7:33 am
  • Editorial Team
    Editorial Team added an answer You can use negative lookbehind, e.g.: .*(?<!\.config)$ This matches all… May 13, 2026 at 7:33 am
  • Editorial Team
    Editorial Team added an answer Put the content you want to avoid the postback outside… May 13, 2026 at 7:33 am

Related Questions

I've written an image processing program in MATLAB which makes heavy use of the
I'm working on a query where I pull data from multiple tables using left
I have a very complex MySQL query that includes use of the same subquery
As a fairly junior developer, I'm running into a problem that highlights my lack

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.