2

I have a shopping website which allows users to place orders. In my web application when the users click 'Create Order' i call an OrderService class which looks like the below:

public class OrderService
{
    public void CreateOrder(OrderDTO dto)
    {
        SaveOrder saveOrder = new SaveOrder();
        ReduceStock reduceStock = new ReduceStock();
        CustomerOrderEmail customerOrderEmail = new CustomerOrderEmail();

        saveOrder.SaveToDatabase(dto);
        reduceStock.ReduceStockInDatabase(dto);
        customerOrderEmail.SendEmail(dto);
    }

    public void ApproveOrder(OrderDTO dto)
    {

    }

    public void DispatchOrder(OrderDTO dto)
    {

    }
}

The Create Order method performs 3 operations which i have broken into classes.

  • Save Order to database
  • Reduce stock in database
  • Email customer confirmation

We are having an internal discussion as to whether or not we should have another class called something like CreateOrderProcess which has 1 public method, this method would then call the 3 methods above. See below:

public class OrderService
{
    public void CreateOrder(OrderDTO dto)
    {
        CreateOrderProcess createOrderProcess = new CreateOrderProcess();

        createOrderProcess.Process(dto);
    }

    public void ApproveOrder(OrderDTO dto)
    {

    }

    public void DispatchOrder(OrderDTO dto)
    {

    }
}

public class CreateOrderProcess
{
    public void Process(OrderDTO dto)
    {
        SaveOrder saveOrder = new SaveOrder();
        ReduceStock reduceStock = new ReduceStock();
        CustomerOrderEmail customerOrderEmail = new CustomerOrderEmail();

        saveOrder.SaveToDatabase(dto);
        reduceStock.ReduceStockInDatabase(dto);
        customerOrderEmail.SendEmail(dto);
    }
}

We are new to this and are looking for some direction. What is best practice? Should we add this extra class or not?

Second question is that our SaveOrder,ReduceStock and CustomerOrderEmail classes are in a project called BusinessLogic. Are we right to define these classes as business logic?

Thanks in advance

2
  • I'm dubious about your SaveOrder and ReduceStock classes. Classes are (usually) nouns with a set of related properties and verbs. Your classes are instead verbs, which is unusual. Plus, both of these classes need to understand your database schema, so you probably have duplication of knowledge. Mar 22, 2016 at 2:41
  • What are the arguments within your team for and against making this change? What is your bias (are you for or against making this change)?
    – Daniel T.
    Mar 23, 2016 at 13:25

2 Answers 2

1

I wouldn't introduce anything.

This seems like something you could break into smaller pieces if it got difficult to manage. But right now, three function calls aren't hard to manage, so leave it simple until you need to make it complicated.

In fact, I'd take out some of these levels unless they're used by a different service. So more like;

public void CreateOrder(OrderDTO dto)
{
    SaveToDatabase(dto);
    ReduceStockInDatabase(dto);
    SendEmail(dto);
}

private void SaveToDatabase(OrderDTO dto) 
{
    ...

The OrderService's job is to deal with orders. Let it, and only break it apart if it starts to do too much, or starts doing things that aren't about orders.

For instance, SendEmail(dto) looks like it composes and sends an email about an order. OK, the part that does order-specific logic (like building a message saying "Thanks for your order") should live in the OrderService. If you send emails more generally, you might want something like a NotificationService, so that you could do this;

 private void SendEmail(OrderDTO order)
 {
     var subject = $"Thanks for ordering a {order.Product.Name}!";
     var body = $"Your {order.Product.Name} is on it's way";
     notificationService.Send(subject, body);
}

Note how the first two lines are ordering-specific, and the third line is a general system utility for sending emails. So the first two belong to OrderService, and the other can be broken out.

0

First of all, make everything works out. It's ok if you copied a function and put it in several places. When everything works out, it then will be called early version of your application (v1 or whatever). The next step is beautifying. This is when you see a lot of repetition in your code. You'll see which code block that need to be classified.

Beautification of code helps you shorten the code and documentation matter for future maintenance. But always remember not to stressed out on creating class or short code at early version as class does not help your code run. Class only help making you to program easier in the future as you will not need to copy and paste multiple times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.