jeudi 2 février 2017

Pattern to implement business rules based on Repositories results

I have to implement many business rules that depends on existing data.

So I created many specific methods in my Repositories that query the existing data so I can implement the business rules.

Then, my Services call these many specific methods to implement the rules.

But this doesn't feel right and I suspect that I can use some other pattern to encapsulate the business rules.

So instead of:

var users = userRepository.GetUserByUsername("johndoe");
if (users.Count > 0)
throw new Exception("Username already exist.");

I want:

if (someOtherPattern.DoesUserExist("johndoe"))
throw new Exception("Username already exist.");

Then, I would inject my repositories to this "someOtherPattern". The question is - what pattern can I use? Would that be the Specification pattern?

Aucun commentaire:

Enregistrer un commentaire