Question

I have the following objects:

Person <- Contact -> ClientsPerson <- Client

I'm using the repository pattern with a service layer. But already here, being a perfectionist, I'm lost!

I also have a PersonService, ClientService and I'm pretty sure I need to have a ContactService which should call the personService.Add(person) and clientsPersonService.Add(client) - or?

The job is to add a contact. The contact would have a Person object related, and a ClientsPerson object. And that ClientsPerson would ofc. have a Client.

Where should the different jobs go to? I've heard it's bad to call a service/repository from one to another.

Was it helpful?

Solution

This answer depends on your ORM and how it cascades saves. We will assume you aren't using an ORM that cascades changes.

Your service layer methods should reflect the actions that you need to accomplish from a business sense. Frequently I find that my controller Actions match up very closely to service methods.

In your example, I would have the service build and relate to the necessary entities and talk to repos to save them; that way if there is a problem along the way of talking to the repos it can roll itself back and do whatever cleanup is necessary.

Repos should be relatively dumb. Services should do the relationship and business work (barring something like NHib, and maybe even then).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top