Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Business logic in Entity Framework POCOs using partial classes?

I have business logic that could either sit in a business logic/service layer or be added to new members of an extended domain class (EF T4 generated POCO) that exploits the partial class feature.

So I could have:

a) bool OrderBusiness.OrderCanBeCancelledOnline(Order order) .. or (IOrder order)

or

b) bool order.CanBeCancelledOnline() .. i.e. it is the order itself knows whether or not it can be cancelled.

For me option b) is more OO. However option a) allows more complex logic to be applied e.g. using other domain objects or services.

At the moment I have a mix of both and this doesn't seem elegant.

Any guidance on this would be much appreciated!

like image 297
Mark Chidlow Avatar asked Dec 28 '22 06:12

Mark Chidlow


1 Answers

The key thing about OO for me is that you tell objects to do things for you. You don't pull attributes out and make the decisions yourself (in a helper class or other).

So I agree with your assertion about option b). Since you require additional logic, there's no harm in performing an operation on the object whilst passing references to additional helper objects such that they collaborate. Whether you do this at the time of the operation itself, or pre-populate your order object with those collaborating entities is very much dependent upon your current situation.

like image 71
Brian Agnew Avatar answered Jan 04 '23 23:01

Brian Agnew