The issue of data modeling is the separation of data and computing and access logic

Ok, so I wanted to get opinions on this topic.

I have a dumb data object - CustomerOrder.

CustomerOrder has a price and quantity, but also has a TotalCash property (price * quantity). therefore, I have the totalCash property, but I don’t want to directly calculate the object, because it violates the rule of the silent data object. I need to get cash flow all over the application again and again, so I need to centralize the calculations. I could create a cashFlowCalculator class and pass it to customerOrder, but I don't want a different class to be needed for every simple calculation.

Any ideas or best practices?

+3
source share
7 answers

In the same situation, I would break the "silent data object rule", since I do not expect this particular calculation to change often. I would probably use it as a getter.

For more complex scenarios, it makes sense to create an OrderCalculator class that accepts classes related to order and can perform all kinds of calculations, such as tax included, field measurement, etc. In this way, you delegate settlement responsibilities outside of CustomerOrder. Thus, CustomerOrder does not need to be aware of Texas state tax in order to determine if a sales tax is needed, for example.

+1
source

, -. LINQ , , , , -. , . , , - - , 1-1, , . factory . - - .

0

?

public sealed class CustomerOrder
{
    public decimal Price;
    public decimal Quantity;
}

public static class CustomerOrderExtensions
{
    public static decimal GetTotalCash(this CustomerOrder data)
    {
        return data.Price*data.Quantity;
    }
}

.

0

(DTO), datatable, TotalCash Datacolumn datacolumn Expression " * ". readCall TotalCash (DTO) , datatable. , .

0

[, ;-)] , SQL, , ( ); ,

0

, , , TotalCash / ...

- - , , TotalCash , .

, DTO , , TotalCostCalculation, .

2 .

0

TotalCash DTO ( YAGNI). TotalCash, , , , TotalCashCalculator Order DI.

0

All Articles