An alternative to what you do would be to use dependency injection . Including dependencies is a super bizarre name for passing stuff into things, not for those who directly access this stuff . I know this vague statement, but if your class takes an argument for the field instead of creating the type itself, it already uses dependency injection.
So let's say you have a Localizer class. It has no static methods, and a static instance of a localizer is not global.
By creating a Localizer instance specialized for your needs, once when the application loads:
var localizer = new Localizer(...);
Then, when the component needs a localizer - you pass it
var component = new MyComponent(localizer);
This makes the localizer easily modifiable, makes it easy to test classes, and makes it easy to configure different components in different ways (what if you want the help page to always be in English all of a sudden? Or some other specific page?).
If itβs still unclear, here is Mishko Heavyβs good talk about not looking for things . There is also a good article by Martin Fowler , but perhaps this is a bit more complicated.
The only tedious thing is that you need to transfer it at any time. I am not opposed to the explanation, but many people prefer to use dependency injection containers to manage overhead.
Benjamin gruenbaum
source share