Injection injection - need a bigger example?

I am looking for a more detailed example of dependency injection and how it can be implemented. If class A depends on class B and passes the class C reference to constructor B, should class A also refer to class C in its constructor? This means that the main method in the application is to create all the classes in reality, which sounds strange?

I understand that using DI frameworks we can somehow have it in XML files, but it seems like it would be difficult to quickly understand which type is really instantiated? Especially if it is a very large application.

+3
source share
4 answers

You are right, and each DI infrastructure has a different way of managing.

.., , "" , (, windsor Castle .net) xml, "" .

, A B, C. A C, B.

    public class C { }

    public class B { public B(C c) { ... }}

    public class A { public A(B b) { ... }}

    // manual wireup
    C c = new C();
    B b = new B(c);
    A a = new A(b);

   // DI framework
   InversionOfControlContainer container = new InversionOfControlContainer(... some configuration);
   A a = container.ResolveInstanceOf<A>();
   // container dynamically resolves the dependencies of A. 
   // it doesnt matter if the dependency chain on A is 100 classes long or 3.
   // you need an instance of A and it will give you one. 

, .

+2
  • A, B C, A B.
  • DI XML . , . - , , .
  • DI " ", . factory builder ", ,

, . #, Unity. (, , ):

container = new UnityContainer();
container.RegisterType<C>();
container.RegisterType<B>(); 
A a = container.Resolve<A>();
+1

PHP-, ,

class Users
{
    var $Database;
    public function __construct(Database $DB)
    {
        $this->Database = $DB;
    }
}
$Database = Database::getInstance();
$Users = new Users($Database);

new getInstance(),

$Users = new Users(Database::getInstance());

-

class Users
{
    /*Dependencies*/
    private $database,$fileWriter;
    public function addDependency($Name,$Object)
    {
        $this->$Name = $Object;
        return $this;
    }
}
$Users = new Users();
$Users->addDependency('database',new Database)->addDependency('fileWriter',new FileWriter);

Update:

, Injection Dependency, .

, .

:

abstract class Registry
{
    static $objects = array();
    public function get($name)
    {
        return isset(self::$objects[$name]) ? self::$objects[$name] : null;
    }

    public function set($name,$object)
    {
        self::$objects[$name] = $object;
    }
}

,

  • ,

, :

Registry::add('Database',new Database());
Registry::add('Reporter',new Reporter());

, :

class Users
{
    public function getUserById($id)
    {
         $query = "SELECT * FROM users WHERE user_id = :id";

         $resource = Registry::get("Database")->prepare($query);
         $resource->bindParam(':id',$id,PDO::PARAM_INT);

         if($resource->execute())
         {
              //etc
         }else
         {
              Registry::get('Reporter')->Add("Unable to select getUserById");
         }
    }
}

, .

+1

If someone else is looking for a good example that shows a DI without IoC containers (bad DI person), as well as an IoC container (Unity in this example) and type registration in code, as well as in XML, you can check this: https: //dannyvanderkraan.wordpress.com/2015/06/15/real-world-example-of-dependeny-injection/

0
source

Source: https://habr.com/ru/post/1770924/


All Articles