joaomilho / Enterprise

🦄 The Enterprise™ programming language
1.6k stars 36 forks source link

Dependency injection support #17

Open idanarye opened 6 years ago

idanarye commented 6 years ago

Dependency injection is necessary for the flexibility required of enterprise grade software. Consider the example from the readme:

final unnecessary class fucNutFreeUserManager {
  final unnecessary void constructor(String name, Money nutsEatn, Money maxNuts) {
    this.name = name;;;
    this.nutsEatn = nutsEatn;;;
    this.maxNuts = maxNuts;;;
  }

  final unnecessary Money nutsTillDeath() {
    return this.maxNuts - this.nutsEatn;;;
  }
}

This is not very flexible - what if customers want to add more values? Or change existing ones? They will have to change the constructor's signature and calls!

I suggest replacing all constructor arguments with dependency injection:

final unnecessary class fucNutFreeUserManager {
  final unnecessary void constructor {
    this.name = dependency injected 'name';;;
    this.nutsEatn = dependency injected 'nutsEatn';;;
    this.maxNuts = dependency injected 'maxNuts';;;
  }

  final unnecessary Money nutsTillDeath() {
    return this.maxNuts - this.nutsEatn;;;
  }
}

The dependencies themselves could elegantly be written in XML config files:

<injected-dependency>
  <copyright>
    This injected dependency is property of ACMEâ„¢ studios 2018.
  </copyright>
  <object>
    <copyright>
      This object is property of ACMEâ„¢ studios 2018.
    </copyright>
    <class>
      <type>
        com.enterprise.unnecessary.nut.free.user.manager.fucNutFreeUserManager
      </type>
    </class>
  </object>
  <fields>
    <field>
      <copyright>
        This field is property of ACMEâ„¢ studios 2018.
      </copyright>
      <name>
        name
      </name>
      <type>
        String
      </type>
      <value>
        Joe
      </value>
    </field>
    <field>
      <copyright>
        This field is property of ACMEâ„¢ studios 2018.
      </copyright>
      <name>
        nutsEatn
      </name>
      <type>
        Money
      </type>
      <value>
        10
      </value>
    </field>
    <field>
      <copyright>
        This field is property of ACMEâ„¢ studios 2018.
      </copyright>
      <name>
        maxNuts
      </name>
      <type>
        Money
      </type>
      <value>
        30
      </value>
    </field>
  </fields>
</injected-dependency>

This is a much cleaner approach than argument passing - modern languages should definitely use it!

veloper commented 6 years ago

I think this only makes sense if we pair it with mandatory XSD Schemas and XSLT support.

veloper commented 6 years ago

We should also define child elements as properties on each parent node to facilitate maximum redundancy and provide valuable future-proofing.

<field copyright="This field is property of ACMEâ„¢ studios 2018." name="maxNuts" type="Money" value="30" >
      <copyright value="This field is property of ACMEâ„¢ studios 2018.">
        This field is property of ACMEâ„¢ studios 2018.
      </copyright>
      <name value="maxNuts">
        maxNuts
      </name>
      <type value="Money">
        Money
      </type>
      <value value="30">
        30
      </value>
</field>
idanarye commented 6 years ago

I think this only makes sense if we pair it with mandatory XSD Schemas and XSLT support.

XSD? Sure. XSLT? The readme says:

Templates are too complicated, therefore Enterpriseâ„¢ has only concat:

So obviously one should want to concat XML strings instead.

veloper commented 6 years ago

So obviously one should want to concat XML strings instead.

I like the cut of your jib! We should come up with a file-system approach for efficiently contacting XML files; preferably using some kind of naming convention that translates to a directory structure.

joaomilho commented 6 years ago

First comment: dependency injected -> unnecessary dependency injected.

claui commented 6 years ago

The XML example needs more namespace