Instantiator vs New for NamedObjectDomainFactory

I want to create an extension that looks like :

myExtension {
  configurations {
    named1 {
      val = "hello"
    }
    named2 {
      val = "bye"
    }
  }
}

Where configurations is of type NamedObjectDomainContainer<MyType> and I have a NamedObjectDomainFactory<MyType>. and I create the container in my extension using project.container(Named..Container<MyType>, Named..Factory<MyType>)

What I don’t really understand is the Instantiator, any example I’ve seen of NamedObjectDomainContainer has some variation of the instatiator being used to create objects. If I implement a NamedObjectDomainFactory, why don’t I just make new objects using new MyType(name) instead of instantiator.newInstance(MyType.class, name)

What exactly is this instantiator giving me?

Basically, everything enumerated here:

By encapsulating instantiation inside an Instantiator, it allows us to do stuff like “decorate” object with additional capabilities.

Okay that makes a lot of sense. Is there any reason why Instatiator is in an “internal” package then? Is there any reason to use NamedObjectDomainContainers without it?

It’s not part of the public API. In general, Gradle handles instantiating objects for you, and how that happens is an implementation detail. The NamedDomainObjectFactory interface is simply there if you want to provide the instantiation implementation. Otherwise, Gradle will simply look for a constructor with a single String argument which is used for the item’s name.

Ah okay, so I can avoid this for now. Thanks!