简体   繁体   English

考虑到业务需求的变化,在这种情况下,POJO /模型或域驱动对象的实质是什么?

[英]What really is the essence of POJOs/models or domain-driven objects in the situation in context considering business requirements changes?

I was told this chunk of code is a really nasty anti-pattern. 有人告诉我这段代码是一种非常讨厌的反模式。

  @SuppressWarnings("unchecked")
  private static Map<String, Object> getArgs(Object obj) {
    return new HashMap<>((Map<String, Object>) obj);
  }

  public void buildAndUpdateCustomer(List<Object> list) {
    for (Object obj : list) {
      Map<String, Object> args = getArgs(obj);
      daoProvider.updateCustomerName(args);
      daoProvider.updateAgingMia(args);
    }
  }

  public void buildAndUpdateTax(List<Object> list) {
    for (Object obj : list) {
      Map<String, Object> args = getArgs(obj);
      daoProvider.updateTaxAmount(args);
    }
  }

  public void buildAndUpdateLedgerBal(List<Object> list) {
    for (Object obj : list) {
      Map<String, Object> args = getArgs(obj);
      daoProvider.updateLedgerBalance(args);
    }
  }

An argument for that is because: 这样做的原因是:

  • It removes all of the type safety the compiler provides and even removes the name checking. 它删除了编译器提供的所有类型安全性,甚至删除了名称检查。
  • Contains unnecessary casts (Map<String,Object>) obj 包含不必要的强制转换(Map<String,Object>) obj
  • Difficulty understanding what a method does by its signature because method parameters are of Object type. 由于方法参数属于对象类型,因此很难通过方法的签名来了解方法的作用。 List<Object> list

My List<Object> list could be a list of invoice tax amounts, list of customers, list of ledger balances et cetera...which could be rewritten like this. 我的List<Object> list可以是发票税金额,客户列表,分类帐余额等的列表...可以这样重写。

public void buildAndUpdateCustomer(List<Customer> list) {...}
public void buildAndUpdateTax(List<Tax> list) {..}
public void buildAndUpdateLedgerBal(List<Ledger> list) {..}

This means having to create Customer,Tax and Ledger POJO/entity/domain objects for each method. 这意味着必须为每种方法创建Customer,Tax和Ledger POJO /实体/域对象。 I have got over 100 buildAndUpdate() of these sort of methods updating and doing different things, do i have to create 100 of these POJO/entity/domain objects?? 我已经有超过100种的buildAndUpdate()这类方法可以更新并执行不同的操作,我是否必须创建100个这些POJO /实体/域对象? Maybe this is bad practice but i feel like having to add classes all over the place bloats the entire code base and kills maintainability. 也许这是一种不好的做法,但我觉得必须在各处添加类,从而使整个代码库blo肿,并破坏了可维护性。

You basically ask why not use Map<String, Object> instead of domain objects. 您基本上会问为什么不使用Map<String, Object>而不是域对象。

I can't imagine too many things which are worse for maintainability than Map<String, Object> . 我无法想象有太多事情比Map<String, Object>对可维护性更Map<String, Object> Heara are just a few reasons: Heara只是几个原因:

  • As a developer you'll have no idea which fields are allowed or are mandatory, and which types do they have - unless you dig through the source code. 作为开发人员,您将不知道哪些字段是允许的或哪些字段是必填的,以及它们具有哪些类型-除非您深入研究源代码。
  • As for digging through the source code, since it's just Map<String, Object> it's hard to know where this pseudo-class is actually used. 至于源代码的挖掘,由于它只是Map<String, Object>因此很难知道该伪类的实际使用位置。 Unlike "find references" of a normal class. 与普通类的“查找引用”不同。
  • You domain objects may have additional utility methods, for instance providing easier access for data. 域对象可能具有其他实用程序方法,例如,提供对数据的更轻松访问。
  • You can't enforce rules in Map<String, Object> . 您不能在Map<String, Object>强制执行规则。 Basically, you can create a instance which is invalid from the business logic point of view. 基本上,您可以创建一个从业务逻辑角度来看无效的实例。
  • Refactoring. 重构。 Imagine you'd need to rename a field. 假设您需要重命名字段。 Good luck finding all those places this field may be accessed. 祝您好运,找到可以访问此字段的所有那些地方。

So, yes, I'd definitely create a 100 domain objects. 所以,是的,我肯定会创建100个域对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM