服务层中的服务组织?

发布于 2024-12-09 18:53:48 字数 302 浏览 0 评论 0原文

我有一个包含大量不同实体的 Java 服务器应用程序。到目前为止,每个实体顶级实体都有自己的CRUD 服务。我所说的顶层是指可以独立存在的实体树的根。

现在我正在进入 Flex 客户端的核心,发现我需要/编写许多不同的查询。但最好把这些查询放在哪里呢?

举例来说,我有一个查询,根据与某个“bar”的关联来查找所有“foos”。目前该查询位于“foo”服务(findAllByBar)上,但我发现将其(也?)放在“bar”服务(findFoos)中会非常方便。另一方面,我还可以创建一个查询服务并将所有查询集中在其中。

这里有什么好的做法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里人 2024-12-16 18:53:48

尝试从以下角度对您的应用程序进行分层:

  • 域:将您的类设计为“Customer”等实体、“Address”或“Color”等值对象以及聚合根(如“Order”,其中包含“LineItem”列表) ”)

  • 存储库:这些是数据访问对于实体,为每个聚合根创建一个存储库(CustomerRepository、OrderRepository,...)

  • 服务:创建由逻辑业务抽象或有界上下文而不是实体组成的粗粒度服务,创建订单服务、商品服务和客户服务是不合逻辑的。这些实体代表订单处理的一个原子业务价值,那么您的服务将使用所有必需的存储库来处理数据访问。

例子:

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);
    // ....
  }
}

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);
    // ....
  }
}
安穩 2024-12-16 18:53:48

我会将查询放在各自的类中,而不是创建一个(可膨胀的)查询服务

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文