-
-
Notifications
You must be signed in to change notification settings - Fork 164
Implementing Business Rules Domain Policies
Business Rules - Domain Policies is created to ensure that the logical rules in an object are received from the outside. The rules of business are in a chain of successive chains, which may also reveal a more complex business rule.
I want to continue with a code example to understand the logic. In repository Domain Policies implemented by interface definition ;
public interface IIssueAssignmentPolicy
{
void CheckAssignment(Issue issue, User user);
}
These definition and below implementation located in Core layer.
public class IssueAssignmentPolicy : IIssueAssignmentPolicy
{
private readonly IIssueRepository _issueRepository;
private readonly IIssueAssignmentConfiguration _configuration;
public IssueAssignmentPolicy(IIssueRepository issueRepository, IIssueAssignmentConfiguration configuration)
{
_issueRepository = issueRepository ?? throw new ArgumentNullException(nameof(issueRepository));
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
}
public void CheckAssignment(Issue issue, User user)
{
if(_issueRepository.GetOpenIssueCountOfUser(user.Id) >= _configuration.MaxConcurrentOpenIssueCountForAUser)
{
throw new IssueAssignmentException($"Can not assign more than {_configuration.MaxConcurrentOpenIssueCountForAUser} open issues to a user!");
}
}
}
After definitions of rules, we should use this policies into domain functions as below way;
public void AssignTo([Required] User user, [Required] IIssueAssignmentPolicy policy)
{
Check.NotNull(user, nameof(user));
Check.NotNull(policy, nameof(policy));
policy.CheckAssignment(this, user);
AssignedUserId = user.Id;
}
So by this way, we can able to manage domain related business rules into the Core Entity objects. That means we protect to our entities unmanaged modifications.
You can check full repository documentations, step by step development and how to build your custom scenario's on this basement in 100+ page eBook PDF from here - http://www.aspnetrun.com.