Business logic in Entity Framework POCOs using partial classes?

I have a business logic that can either sit at the business logic / service level or be added to new members of an extended domain class (EF T4 generated by POCO) that uses a partial class function.

So, I could:

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

or

b) bool order.CanBeCancelledOnline() .. that is, the order itself knows whether it can be canceled.

For me, option b) is more OO. However, option a) allows for more complex logic, for example. using other objects or domain services.

At the moment, I have a mixture of both, and it does not seem elegant.

Any advice on this would be much appreciated!

+4
source share
4 answers

The OO keyword for me is that you tell objects about how to do something for you. You do not extract attributes and make decisions yourself (in the helper class or another).

So, I agree with your statement about option b). Since you require additional logic, there is no harm in performing operations on the object by passing references to additional auxiliary objects so that they interact. Regardless of whether you perform this during the operation itself or pre-fill your order object with these collaborating objects, you are very dependent on your current situation.

+6
source

You can also use extension methods for POCO to wrap your bll methods. This way you can continue to use your current bll. in C # something like:

 public static class OrderBusiness <- everything must be static, class and method { public static bool CanBeCancelledOnline(this Order order) <- notice the 'this' { logic ... 

And now you can do order.CanBeCancelledOnline ()

+2
source

This probably depends on the complexity of your application and requires some judgment that comes with experience. The short answer is that if your project is more than pretty simple, then it's best for you to put your logic in the domain classes.

Longer answer:

If you put your logic at the service level, you are emotionally following the script transaction scheme and ending with the anemic domain model . This may be a valid route, but it usually works best with simple and small projects. The problem is that the transaction level of the script (your service level) becomes more difficult to maintain as it grows.

Thus, an alternative is to create a rich domain model that contains the logic inside it. Keeping the logic together with the class to which it belongs is a key part of good OO design, and is very important in a complex project. This usually requires a little more thought and effort, so for very simple projects, people sometimes use a script transaction template.

If you are not sure what to do with it, it is usually not a very difficult task to reorganize your logic in order to move it from your service level to a domain, but you need to make a call early enough so that the work is not too big.

Unlike one of the answers, using POCO classes does not mean that you cannot have business logic in your domain classes. POCO does not apply infrastructure-specific structures for your domain classes , such as methods and interfaces specific to a particular ORM. A class with some functions for applying business logic obviously remains a Plain-Old-CLR-Object.

+2
source

The general question, as well as partially subjective.

IMO, you have to go with option A.

POCO should be exactly the same as objects with the plain old CLR. If you start applying business logic to them, they will cease to be POCO. :)

You can put your business logic in the same assembly as your POCOs, just don’t add methods to them, create helper classes to simplify business rules. The only thing your POCO should have is matching the properties of your domain model.

It really depends on how complex your business rules are. In our application, business process rules are very simple, so we use option A.

But if your business rules start to get confused, consider using the Specification template .

0
source

All Articles