views:

69

answers:

1

Hey!

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.

As you can see, I might need some help on this one!

A: 

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).

Chad Ruppert
So I should have a clientService.AddContact(Client client, Person person).It should reference a client-repos (obviously), a person-repos and a clientPerson-repos? Is that correct? Then validate the person data, insert person, insert clientPerson with info from passed client and inserted person. Wow.
Kordonme
Well, yeah, those are the steps required to do what you need done, correct? Is the process going to change frequently? Don't you want that process testable? By doing that and separating concerns in this way, you gain easy testability, maintainability, etc.
Chad Ruppert
If you are doing a quick site that is simple and not meant to be around forever its probably overkill, but that's really a matter of opinion. I know it sounds daunting, but you have to do all that stuff anyway, and this makes it maintainable.
Chad Ruppert
Well, I'm trying to learn to do it the right way ;) My only problem is now, that I'm not able to validate my Person, since validation logic is supposed to go in the servicelayer, right? And I only have access to the repository. Should I put validation logic for person creation in my client-service? What if I want to add a Person directly from the person service layer. Then I have the validation two times.
Kordonme
Validation can go where you want it to go. Personally I validate at the controller level (for ui hints. eg: model.errors) and at the service layer, throwing an exception if whats being validated is not, well, valid. Never trust the UI. :) Validation logic, for me, is done using nhib validators, which are attributes on my model. That way I have consistent validation.
Chad Ruppert