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 6746633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:20:45+00:00 2026-05-26T12:20:45+00:00

My current project is split into multiple classes that correctly represent the way we

  • 0

My current project is split into multiple classes that correctly represent the way we think of the entities in the system. However, this is taking a performance price, so I’m trying to come up with a way of improving performance while maintaining the current class structure we have.

I think an example will best illustrate the problem: Let’s say I have an Order class that contains multiple Items. A naive implementation for shipping an Order is shipping all of its Items:

class Order 
{
    private List<Item> m_items;

    public void Ship()
    {
        foreach (Item item in m_items)
        {
            item.Ship();
        }
    }
}

Shipping an Item is a multi-stage process:

class Item
{
    public void Ship()
    {
        var receipt = m_boat.ShipToDestination(this);
        ProcessReceipt(receipt);
    }
}

As it turns out, shipping the Item on a Boat takes a long time. Luckily, the Boat can ship multiple Items concurrently (List<Receipt> Boat.ShipToDestination(List<Item> items)). However, to use this method I will have to rewrite Order.Ship() as follows:

class Order 
{
    public void Ship()
    {
        var receipts = m_boat.ShipToDestination(m_items);
        foreach (Receipt receipt in receipts)
        {
            ProcessReceipt(receipt);
        }
    }
}

This means that ProcessReceipt is now part of Order (rather than Item), and also that an Item no longer takes care of all of its shipping logistics.

In my real code there are many methods with this problem, so using this solution effectively means that most of the Item logic will move into the Order class. However, this seems like the wrong place to have this logic – it really should be in Item.

I considered using multiple threads (e.g. using a Task per Item) but this seems too wasteful, especially since an Order can have dozens of Items.

Another approach I considered is creating a QueuedBoat with a QueueForShipping(Item item) method and a ShipQueuedItems() method. Now Order can call ShipQueuedItems while Item can call QueueForShipping. The problem with this approach is that in my real code, the Receipt is returned only after the Item was shipped, so the code would have to look like this:

class Order 
{
    private List<Item> m_items;

    public void Ship()
    {
        foreach (Item item in m_items)
        {
            item.Ship();
        }

        m_queuedBoat.ShipQueuedItems();

        foreach (Item item in m_items)
        {
            item.FinalizeShipping();
        }
    }
}


class Item
{
    public void Ship()
    {
        m_queuedBoat.QueueForShipping(this);
    }

    public void FinalizeShipping()
    {
        ProcessReceipt(m_queuedBoat.GetReceiptForItem(this));
    }
}

This feels like it’s in the right direction (since Order knows how many Items it has, it must be the component that knows when everything is on the Boat), but it broke Ship into two stages. In my real code I have multiple stages (each can be vectorized separately), so the method would be broken into 3-4 pieces, which is not a pretty sight.

Can someone suggest a way of vectorizing Item.Ship() while keeping the business logic inside the Item class, or will I have to give up on the Order/Item separation in order to take advantage of vectorization?

Edit:

Another approach could be this:

class Order 
{
    private List<Item> m_items;

    public void Ship()
    {
        for (int i = 0; i < m_items.Count; ++i)
        {
            bool isLast = (i == (m_items.Count - 1));
            item.Ship(isLast);
        }
    }
}

class Item
{
    public void Ship(bool isLast)
    {
        m_queuedBoat.QueueForShipping(this, FinalizeShipping);
        if (isLast) m_queuedBoat.ShipQueuedItems();
    }

    private void FinalizeShipping(Receipt receipt)
    {
        ProcessReceipt(receipt);
    }
}

Where the QueuedBoat class invokes a callback for each item with its Receipt.

  • 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-26T12:20:46+00:00Added an answer on May 26, 2026 at 12:20 pm

    Well, anyway Ship is a kind of asynchronous operation: there must run several at once. So has to be two-stage: PrepareShipping (void) and PerformShipping (waits for the actual shipping to finish and returns processes its receipt).

    I would so something like that:

    class Item
    {
        public void PrepareShiping()
        {
            m_queuedBoat.QueueForShipping(this);
        }
    
        public void PerformShipping()
        {
            m_queuedBoat.StartActualShippingIfNotAlreadyStarted();
            m_queuedBoat.WaitForShippingEnd();
            ProcessReceipt(m_queuedBoat.GetReceiptForItem(this));
        }
    }
    

    So the Order can be something like that:

    class Order 
    {
        public void PrepareShipping()
        {
            foreach (Item item in m_items)
                item.PrepareShiping();
        }
    
        public void PerformShipping()
        {
            foreach (Item item in m_items)
                item.PerformShipping();
        }
    
        // and for those who don't need asynchronity
        public void Ship()
        {
            PrepareShipping();
            PerformShipping();
        }
    }
    

    The program can do additional things while shipping is in progress, that is, between Order.PrepareShipping() and Order.PerformShipping() (for example, ship out other orders as well, maybe on the same boat).

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

Sidebar

Related Questions

My current project involves deploying an upgraded .exe file that runs as a Windows
My current project is broken down into 3 parts: Website, Desktop Client, and a
Our current project has ran into a circular dependency issue. Our business logic assembly
For a current project, we're designing a client desktop application that parses text files
We recently split our current work into two different VS solutions, one which contains
While uploading my current project to our staging server I noticed that the Web.config
In my current django project I have a model that stores very long strings
I've become a maintainer of a shared library project. The library is split into
I found the need to use String.format in my project. However, this doesn't work
My current project is in Rails. Coming from a Symfony (PHP) and Django (Python)

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.