Design Decision: Generate OrderNumber which depends on a database value

天涯浪子 提交于 2019-12-12 04:02:07

问题


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:


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.



来源:https://stackoverflow.com/questions/7888352/design-decision-generate-ordernumber-which-depends-on-a-database-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!