C
C#ā€¢3y ago
bookuha

[ASP.NET] B-Logic Services, Validation

I am using anemic models, therefore I want to implement Business Logic Services. And the questions are: 1) When to create a service? Should it cover entity and entities related to it (like aggregate in DDD) Example: I have a class Book, that uses Description, Authors objects in it. Should I create a class BookService that can manage Books and their descriptions and authors, or should I create + 2 service classes for the related entites and use them along with the BookService? When do I separate the logic in them? 2) What should the methods return? I am aware of concept of Either/Result, but I don't want to implement them 3) Should I throw exception directly in these methods and rely on some middleware to make them shiny for the UI?
6 Replies
becquerel
becquerelā€¢3y ago
this sounds like you're starting with terms and philosophies about how to organise your code before you actually have any code i'll give my personal answers 1) create a BookService if you have an endpoint/controller/etc. that is actively concerned with books over and above the things that constitute books. the point of a service is to encapsulate work, so whether a service is necessary is the same question as if the work is necessary 2) depends entirely on what the methods do! 3) it depends on the kind of error. if you run into a truly exceptional or illegal circumstance, throwing an exception is the right thing to do if you're encounter regular, everyday errors (content not being found, invalid user input...) then an either/result type is often more appropriate
bookuha
bookuhaOPā€¢3y ago
@Becquerel Thank you! Yeah, I just can't start coding because I want my code to be as good as possible and then I'm getting lost in the terms With no code And I can't find any reference projects to check how the things are done
becquerel
becquerelā€¢3y ago
here's a pro tip code is never good the first time around šŸ™‚ it's like prose - all first drafts suck that's why you go back and edit them
bookuha
bookuhaOPā€¢3y ago
This is very sad!
becquerel
becquerelā€¢3y ago
not at all before you start writing code, you fundamentally do not know the problems you are solving it is very hard to create a good solution for something when you don't know what the question is!
bookuha
bookuhaOPā€¢3y ago
hmm, yeah. so i will look for problems

Did you find this page helpful?