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

  • SEARCH
  • Home
  • 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 6734453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:53:25+00:00 2026-05-26T10:53:25+00:00

I’m building an ordering system using C# and got a problem on order number

  • 0

I’m building an ordering system using C# and got a problem on order number generation.

I have an Order class which have an OrderNumber property. I want to make the OrderNumber property readonly, because it’s not safe to change OrderNumber.

Because the ordering steps are very complex (4 steps, some steps might take 10 minutes to complete), so I need to save the order in each step. But the client don’t want to waste OrderNumber (order numbers are in “Year-Month-An_DB_Value” format), beause the last part of the OrderNumber indicates the order count in that month. So the OrderNumber should be assigned only when the user click “Request Verification” button.

For generating OrderNumber and keep Order class clean(have no dependency to database and can be unit tested), I want to make IOrderNumberGenerator interface as an argument of the constructor of Order class, like this:

public class Order {
    public string OrderNumber { get; private set; }

    public Order(IOrderNumberGenerator generator) {
        // ...
    }
}

But there’s a problem:

Order can be first saved without having OrderNumber assigned. So if some days later, the user want to pass an Order to the order verifier, he will click the “Request Verification” button, and the system will retrieve the Order object from database via O/R Mapper (I use NHibernate), note that here we have no way to pass the IOrderNumberGenerator, because the Order object is retrieve from O/R Mapper, the ORM will reconstitute the Order object by calling the default constructor of the Order class (so we have no way to pass IOrderNumberGenerator). Here is the demo code:

Order order = repository.GetOrder(orderId);

// code to generate the order number

xxx.SubmitChanges();

So my question is, how to resolve the order number generation problem while still keep the Order class clean and easy to do unit testing.

Updates:
I’m now thinking the create a method named GenerateOrderNumber() in order class, and pass the IOrderNumberGenerator, like this:

public class Order {
   public void GenerateOrderNumber(IOrderNumberGenerator generator) {
       _orderNumber = generate.Generate();
   }
}

I think it can resolve the problem, but any better solutions?

BTW: this is an extended question from my last question: Design Decision: OrderNumber property in Order class – Public or Private?

Thanks!

  • 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-26T10:53:25+00:00Added an answer on May 26, 2026 at 10:53 am

    Considering that you want to generate OrderNumber from code, and considering your note:
    “So the OrderNumber should be assigned only when the user click “Request Verification” button”

    I would say Generate order on “Request Verification” click using OrderNumberGenerator.

    Something like this:

    public class OrderNumberGenerator : IOrderNumberGenerator  
    {
       public OrderNumberGenerator(Order  order) 
       {
          //generate new order number for order parameter
       }
    }
    

    In other words, generate an order at the moment of request (only if necessary), and inverse dipendency of types Order<->IOrderNumberGenerator

    EDIT

    By the way consider also generation of OrderNumber on DB level, if it’s possible obviuosly in your case, like Yahia suggested, most reliable solution.

    Hope this helps.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.