Ask Your Question

What is the preferred way of declaring class relationships?

asked 2012-12-18 20:42:44 -0600

Stefan gravatar image

updated 2013-01-03 14:23:28 -0600

llowder gravatar image

I often find myself of having a class of the following structure

class myapp {
  class { 'myapp::package' }
  class { 'myapp::database' }
  class { 'myapp::config' }
  class { 'myapp::service' }

The myapp class is intended to be specified at nodelevel and acts as a wrapper class. The inner classes have a relationship, e.g. the database can only be created with an SQL script provided by the package. And the application can only be started as a service if the service is configured and the database is ready.

I am aware of two options now:

  1. Declare the relationships in the wrapper class by using ...
edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2012-12-18 21:57:34 -0600

binford2k gravatar image

updated 2012-12-20 08:48:34 -0600

Hiding relationships inside the inner classes is very opaque. I would suggest something like the following:

class myapp {
  class { 'myapp::package': }
  class { 'myapp::database': }
  class { 'myapp::config': }
  class { 'myapp::service': }

  Class[myapp::package] -> Class[myapp::database] -> Class[myapp::config] ~> Class[myapp::service]

This separates your class definitions from the ordering of them, making it more flexible for future refactoring.

Keep in mind that this particular example also falls into the anchor pattern described by Jeff McCune at You may want to add anchors or slightly restructure.

edit flag offensive delete link more


Thanks for your answer. I first thought the author of the class myapp::database probably knows best what he depends on (e.g. myapp::package). On the other hand your ...(more)

Stefan gravatar imageStefan ( 2013-01-07 17:47:58 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2012-12-18 20:42:44 -0600

Seen: 393 times

Last updated: Dec 20 '12