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
SaveOrder
andReduceStock
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.