@fatih uyanık, Welcome to Microsoft Q&A, based on your description, your current approach works in the short term but may cause maintenance issues as the project grows.
Here are the main concerns:
- Tight Coupling –
BookService
depends on IAuthorService
validation, creating unnecessary dependencies.
- Unclear Responsibilities –
BookService
handles both business logic and validation, which breaks the Single Responsibility Principle.
- Poor Scalability – If more services require cross-validation, adding more public validation methods will make the code harder to manage.
I recommend that you create Separate Validator Classes to deal with Validate.
Here is a code example you could refer to.
public interface IBookValidator { void Validate(Book book); }
public class BookValidator : IBookValidator
{
private readonly IAuthorValidator _authorValidator;
public BookValidator(IAuthorValidator authorValidator)
{
_authorValidator = authorValidator;
}
public void Validate(Book book)
{
if (string.IsNullOrEmpty(book.Title))
throw new ValidationException("Title is required.");
_authorValidator.Validate(book.Author);
}
}
public class BookService : IBookService
{
private readonly IBookValidator _bookValidator;
public BookService(IBookValidator bookValidator)
{
_bookValidator = bookValidator;
}
public void Save(Book book)
{
_bookValidator.Validate(book);
// Proceed with saving
}
}
By using the above method, you can know the following advantages:
- Services stay focused on business logic.
- Validators can be reused in multiple places.
- Easier testing and maintenance.
Hope it could help you.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.