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

The Archive Base Latest Questions

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

I’m building an ordering system. And here I have an Order class, which has

  • 0

I’m building an ordering system. And here I have an Order class, which has a property named OrderNumber. As you know, OrderNumber can only be assigned once, so I don’t want to make it public (so I made it private).

However, the order number is generated according to a sequencial value stored in database.
For example, the value in database is 10, now I genearte an order number, the order number will be “Year-Month-10“, and the value will increase to 11. The value will be reset every month.

You see, the order number generation has a dependency to database. If I make OrderNumber private, I can only call the generate order number method in the Order class (maybe in the constructor), that’s not good and hard to do unit testing. And, I don’t want to use any ServiceLocator in the Order class, I think it’s bad too.

If I make the order number public, the problems above can be resolved. But the order number might be changed by some coders’ mistake now, it’s not safe.

So, can I make the OrderNumber property public? And your reasons?

Updates:

Thanks to Yahia for giving me a hint.
Now I consider making OrderNumber property readonly, and pass the generator (e.g. IOrderNumberGenerator) to the constructor of the Order class.

But there’s another problem:
In my senario, the order number is not assigned when the Order object is created. The customer can create and save an order without passing the order to the order verifier.
So before the order is passed to the order verifier, the order number is not assigned. OrderNumber is assigned only when the customer click the “Request Order Verification” button, and then the OrderNumber cannot be changed again. And the workflow moves to the next step (Order Verification).

I’ve created a new question, please look here: Design Decision: Generate OrderNumber which depends on a database value

BTW: I’m using C#

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:18:44+00:00Added an answer on May 26, 2026 at 10:18 am

    I would recommend having a public readonly property OrderNumber (i.e. make set private) based on a private field… this gives you the benefits of both without any negative aspects.

    Something you should consider:

    Cases like the ones you describe are usually solved by implementing the Factory pattern…

    EDIT – regarding the update of the OP:

    Make a class OrderNumber and put it as a public property into your Order class.
    The OrderNumber class has only private constructor and is only created via Factory pattern.

    Thus your Order has initially null assigned to the OrderNumber property… this property has a private set method which only allows assignment when the internal field is null… to assign an OrderNumber you have for example a Status property which when changed to Order Verification calls the saved reference to IOrderNumberGenerator (from the constructor) and set the OrderNumber once…

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
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 want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is

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.