5

Think that when one Branch saved, a Customer entity must created in n tier layered system architecture. All validation has already implemented in CustomerService.

  1. Should BranchService create CustomerService instance to create customer?
  2. Branch service implement validation again for Customer and call CustomerRepository?
  3. Branch must have Customer entity as a propery and should have Customer validation on BranchService?

Sample Classes are

Branch
    - Code
    - Description
    - CustomerId


Customer
    - Code
    - Description
    - CustomerType

BranchRepository
    - Add
    - Remove
    - Get

CustomerRepository
    - Add
    - Remove
    - Get

BranchService
    - AddBranch (Validates entities)

CustomerService 
    - AddCustomer (Validates entites)
hkutluay
  • 153

2 Answers2

1

Don't concentrate so much on making entity specific services, but use case specific services.

Your code should reflect your business logic and processes and their use cases. If you have a use case that says that customer and branch should be committed together, then create such a service.

Create a CustomerBranch service that would encompass both the customer and branch. You can store validation in entities themselves, or in a separate service. It's up to you. Or you can inject your current services into this one where you'll use their validation logic.

Then you can use the Unit of work pattern where you use your repositories to just "notify" ORM that there is a change in an entity, and UoW to Commit those changes to DB. UoW pattern enables you (among other things) to commit batches of changed entities.

Repository pattern, as it is advocated throughout the Internet (in my opinion wrong, but that's another subject), concentrates on changes from one entity only.

civan
  • 479
0
  1. There are two patterns which can solve this problem. i) Use repository pattern to design data layer. Reference - http://thinkinginobjects.com/2012/08/26/dont-use-dao-use-repository/ ii) Unit of work https://dzone.com/articles/generic-repository-and-ddd-rev

  2. Use run time serialization over objects using libraries available(Jackson - one of best available out there)

  3. Service end points seems correct to me however I would suggest keep reference object of Customer inside branch class.

  4. Allow service to accept the Json instead of qualified objects. This is where step 2 is handy.

  5. (additional) rest on spring handles step 2 and 4 quite elegantly. Reference - http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/

I assume you are building the solution using Java. Hope that helps.