Using a custom provider class to remove tight coupling

前端 未结 2 1969
囚心锁ツ
囚心锁ツ 2021-01-06 18:00

I have a custom framework where i have a class/method which uses my own Cache class.

Currently it is tightly coupled. So a method instantiates the

2条回答
  •  鱼传尺愫
    2021-01-06 18:01

    As per OP request

    Especially since it will be part of the framework, you should inject the Cache. A DI Container is the best solution here, you can config the actually Cache implementation as a singleton. Your proposed solution is tightly coupled to some service and hard to test in isolation. Actually it looks more of a service locator pattern rather than provider.

    If you're using a Factory that won't replace the DI Container. THe point of DI is that the code shouldn't be coupled to an outside static service. Unless you have a very good reason, any object should use only the injected (via constructor or as method argument) dependencies.

提交回复
热议问题