We help IT Professionals succeed at work.

Building an object from several other objects

Eddie Shipman
on
120 Views
Last Modified: 2017-05-01
First off, this is not a homework assignment. It is to determine the best object building techniques. I am trying to wrap my head around building the bookSeller object below.
    public class bookSeller
    {
        public List<seller> sellers { get; set; }
    }

    // Resulting data sample:
    {
      "sellers": [
        {
          "sellerId": 6657742342,
          "name": "Tim's Booksellers",
          "books": [
            {
              "bookId": 7283137721,
              "year": 2014,
              "title": "Adventures of Laney"
            }
          ]
        },
        {
          "sellerId": 92081755410,
          "name": "All Old Books",
          "books": [
            {
             "bookId": 6941353265,
              "year": 2017,
              "title": "Signs of the Returning Pleistocene"
            }
          ]
        }
      ]
    }

Open in new window

The data comes from several web service calls. The first one gets a list of bookIds like this:
    public class bookIDs
    {
        public List<int> bookIds { get; set; }
    }

    // Resulting data sample:
    {
      "bookIds": [
        7283137721,
        6941353265
      ]
    }

Open in new window

I am looping through the returned list of bookIds and calling the second to get the information for each book like this:    
    public class bookInfo
    {
        public int bookId { get; set; }
        public int year { get; set; }
        public string title { get; set; }
        public int sellerId { get; set; }
    }

    // Resulting data sample:
    {
      "bookId": 7283137721,
      "year": 2014,
      "title": "Adventures of Laney",
      "sellerId": 1148459751
    }

Open in new window

I also need to get the seller information that returns info like this:
    
    public class sellerInfo
    {
        public int sellerId { get; set; }
        public string name { get; set; }
    }

    // Resulting data sample:
    {
      "sellerId": 6657742342,
      "name": "Tim's Booksellers"
    }

Open in new window

So...
    
    // This function call returns the list of bookIds
    bookIDs bkIds = GetBookIds(authKey); // authKey allows call to webservice function
    // Now we loop over them to get the bookInfo for each book and sellerInfo for each 
    // seller and build the resulting bookSeller object.
    for(int i=0; i< bkIds.bookIds.Count-1; i++)
    {
        book bookInfo = GetBookInfo(bkIds.bookIds[i]);
        // Here is where I need to get the sellerInfo and 
        // add the seller info and the book to the final bookSeller object.
        // Not sure how I should handle it.
        // Function to retrieve the sellerInfo takes the sellerId as parameter
    }

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
What is your question?
Eddie ShipmanAll-around developer
CERTIFIED EXPERT

Author

Commented:
I need to understand the way to build the bookSellers object. See comments in the for loop at the bottom.
Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Eddie ShipmanAll-around developer
CERTIFIED EXPERT

Author

Commented:
Definitely got me on my way, thanks.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.