开发者

Organization of services in service layer?

开发者 https://www.devze.com 2023-04-12 00:53 出处:网络
I have a Java server application with a ton of different entities.So far, each entity top开发者_高级运维 level entity has its own CRUD service.By top level, I mean the root of a tree of entities that

I have a Java server application with a ton of different entities. So far, each entity top开发者_高级运维 level entity has its own CRUD service. By top level, I mean the root of a tree of entities that can stand alone.

Now I am getting into the heart of my Flex client and find I am needing/writing many different queries. But where best to put these queries?

Say for example, I have a query to find all "foos" based on their associate with a certain "bar". Currently that query is on the "foo" service (findAllByBar), but I am finding that it would be very convenient to have it (also?) in the "bar" service (findFoos). On the other hand, I could also create a query service and lump all the queries in there.

Whats a good practice to do here?


Try to layer your application in these perspectives:

  • Domain: design your class as entities like "Customer", value objects like "Address" or "Color", and aggregate roots (like "Order" which includes a list of "LineItem")

  • Repositories: these are the data access for the entities, create a repository for each aggregat root (CustomerRepository, OrderRepository, ...)

  • Services: create a coarse grained services spitted by logical business abstractions or bounded context not by entities, it is not logical to create a service for order and a service for items and a service for customers when all these entities are representing one atomic business value of order processing, then your service will use all required repositories to handle the data access.

example:

public class OrderRepository {
  public Foo getById(int id) {
     // 
  }

  public Foo getByCustomer(Customer customer) {
    //
  }
}

public class CustomerRepository {
  public Foo getById(int id) {
     // 
  }

  public Foo getByUserName(string userName) {
    //
  }
}

public class TradingService {
  private OrderRepository _orderRepository;
  private CustomerRepository _customerRepository;

  public TradingService(OrderRepositoryInterface orderRep, CustomerRepositoryInterface cusRep) {
    _orderRepository = orderRep;
    _customerRepository = custRep;
  }

  public void placeOrder(string customerUserName, Order order) {
    Customer customer = _customerRepository.getByUserName(customerUserName);
    order.setCustomer(customer);
    _orderRepository.add(order);
    // ....
  }
}


I would put queries in their respective classes instead of creating one (bloatable)query service

0

精彩评论

暂无评论...
验证码 换一张
取 消