dongxiaowei_1234 2016-05-31 22:03 采纳率: 0%
浏览 24
已采纳

Silverstripe在Injector中查找类,以便可以通过依赖注入覆盖类

According to the Silverstripe documentation:

Using the create() method provides chainability, which can add elegance and brevity to your code, e.g. Player::create()->write(). More importantly, however, it will look up the class in the Injector so that the class can be overriden by dependency injection.

Can somebody explain the "it will look up the class in the Injector so that the class can be overridden by dependency injection" part?

  • 写回答

2条回答 默认 最新

  • dongtingrun4973 2016-05-31 22:38
    关注

    When your Player Class depends on AnotherClass it is good practice to pass an instance of AnotherClass to Player. You can read more about this here: What is dependency injection?

    SilverStripe has a build in Solution for Dependecy Injection: The Injector Class. See https://docs.silverstripe.org/en/3.3/developer_guides/extending/injector/

    So when you call Player::create(); SilverStripe will look up the correct Class to use (defaults to Player Class but can be overridden with config files), create a new instance of it, inject its Dependencies and return that to you.

    If you are new to SilverStripe you can probably ignore all that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来