Giter VIP home page Giter VIP logo

di's Introduction

Nette Dependency Injection

Downloads this Month Build Status

Purpose of the Dependecy Injection (DI) is to free classes from the responsibility for obtaining objects that they need for its operation (these objects are called services). To pass them these services on their instantiation instead.

Class Nette\DI\Container is a flexible implementation of the universal DI container. It ensures automatically, that instance of services are created only once.

Names of factory methods follow an uniform convention, they consist of the prefix createService + name of the service starting with first letter upper-cased. If they are not supposed to be accesible from outside, it is possible to lower their visibility to protected. Note that the container has already defined the field $parameters for user parameters.

class MyContainer extends Nette\DI\Container
{

	protected function createServiceConnection()
	{
		return new Nette\Database\Connection(
			$this->parameters['dsn'],
			$this->parameters['user'],
			$this->parameters['password']
		);
	}

	protected function createServiceArticle()
	{
		return new Article($this->connection);
	}

}

Now we create an instance of the container and pass parameters:

$container = new MyContainer(array(
	'dsn' => 'mysql:',
	'user' => 'root',
	'password' => '***',
));

We get the service by calling the getService method or by a shortcut:

$article = $container->getService('article');

As have been said, all services are created in one container only once, but it would be more useful, if the container was creating always a new instance of Article. It could be achieved easily: Instead of the factory for the service article we'll create an ordinary method createArticle:

class MyContainer extends Nette\DI\Container
{

	function createServiceConnection()
	{
		return new Nette\Database\Connection(
			$this->parameters['dsn'],
			$this->parameters['user'],
			$this->parameters['password']
		);
	}

	function createArticle()
	{
		return new Article($this->connection);
	}

}

$container = new MyContainer(...);

$article = $container->createArticle();

From the call of $container->createArticle() is evident, that a new object is always created. It is then a programmer's convention.

di's People

Contributors

dg avatar fprochazka avatar vrtak-cz avatar matej21 avatar enumag avatar hrach avatar kravco avatar vrana avatar milo avatar juzna avatar jantvrdik avatar pepakriz avatar majkl578 avatar ondrejmirtes avatar ondrahb avatar uestla avatar greeny avatar tomaswindsor avatar vasekpurchart avatar ujovlado avatar vojtech-dobes avatar lookyman avatar

Watchers

James Cloos avatar Peter Dave Hello avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.