I have a simple class to which I am providing some values via constructor and I would like to use DI. This class has no relation to Drupal(not a plugin, form, entity,...).

It serves as a wrapper for dataset where I need to do some data manipulation by using few services.

I wanted to use the ContainerInjectionInterface but it only has the create() method and it does not look like it can be used for this purpose.

Can the DI in D8 even be used for this purpose?


Yes. Just require the services you need in your constructor, and wherever you create your object, pass them in.

That is the very definition of dependency injection.

The container/services are just a way to describe your dependencies to the environment/Drupal so it can do that automatically but that is in no way a requirement.

What you probably want to do is to use the factory pattern, so that you have a factory, which you can define a service, which offers a method to get your value object with the necessary services injected. Drupal 8 has various examples for the, for example KeyValueDatabaseFactory (keyvalue.database), which has a get($collection) method. Each call creates a new object, passes the dependencies that object needs to it and then returns it.

  • Yep, factory makes sense. – user21641 Dec 19 '15 at 13:48

Yes, DI can be used for that purpose, but you don't deal with the ContainerInjectionInterface interface in any way: You just define a service, which could use any class, such as the class you already have (if it can be used), a wrapper around that class, or a class that instantiate objects of that class; in any case, you don't have to implement a specific interface. Since you normally create a service when you want to allows other modules to implement alternatives to the service, the class normally implements an interface you make public.

For example, Drupal defines the http_client service, which by default implementation uses the \GuzzleHttp\Client class; it also defines the email.validator service, which by default uses the \Egulias\EmailValidator\EmailValidator class.

  • Can you tell me what is DI? – Adrian Cid Almaguer Dec 18 '15 at 22:14
  • Thanks, that makes sense. Although in my case the class is not a service itself and it will be instantiated multiple times via new MyClass($values). It's more like config entity but not tied to Drupal internals in any way.. it just uses some services to manipulate its data(like with the entity example imagine id, label, created...). – user21641 Dec 18 '15 at 22:48
  • @AdrianCidAlmaguer Dependency Injection. – user21641 Dec 18 '15 at 22:48
  • Yes, you don't want to use a service for value objects, since you can have various instances of them. But you can use a service as a factory and inject the dependencies through that, see my answer. – Berdir Dec 19 '15 at 0:02
  • @IvanJaros I was not suggesting to use that exact class, but I was not clear enough, it seems. I was merely saying there isn't any specific interface you need to implement, such as the ContainerInjectionInterface one. – kiamlaluno Dec 19 '15 at 4:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy