php - When can Dependency Injection Containers become too big, and what can I do about it? -
we know why dependency injection awesome because makes code less coupled, easier test, , nicer read! , decide use dependency injection container pimple php assist dependency inversion principle in solid.
so when creating dic using pimple, passing through controller, , having new objects created in closures instantiated when developer calls $container['object']
, great!
but happens when have very large set of classes in application? 1000+, , want these available in container?
development-wise, going nightmare placing these within 1 file. best way separate them, or alternative suggestion preferable?
on separation side, how about:
- creating container
- including several files classes grouped depending on application
- adding container incrementally until end of file includes
on flip side, know symfony2 uses xml/yaml dic configuration, doesn't go talk architectural side of things when application contains many classes.
what can developer when have such large codebase?
let's of classes going called via di container.
first little comparative case: if go store buy pair of trousers, using di @ several moments. instance not have tell size need, people have expertise find out. have kind of type of trousers want have, people of store present trousers according wishes (or let know that impossible). example not di answer question trousers come from. customer entering store answer question totally irrelevant. never reason enter shop, need pair of trousers of type is. can ask unspecified questions of type , exact answers. core of di. how done not of business nor concern. can not ask anything. can't buy bread in cloth store instance. questions must related specific di subject (interface).
pimple associative array. not di - container. far know, di - container return interface, di - container knows implementation load. let me give example show pimple not di - container. in store, have chosen pair of trousers , want buy it. going pay. how? di again. no problem, have several methods transaction available , choose one. how system answer not interesting you: nothing, wallet , start paying.
in pimple have select object use payments. client have 'i need payment object'. in true di - container have hand on money in legal way (interface behavior) , based on input data di - container know implementation choose (cash, credit card, master card). not have worry that. if still have worry that, why call dependency injection? pimple @ best service locator, use di have use interface. otherwise there nothing hide, no dependency inject. :-)
so, don't use pimple di. not di - container. if going use pimple (and can organize classes well) not call di. @ best service locator, not hiding implementation using interfaces. hence can not di.
try organize codebase. functions of different classes? classes need di? suggest use di classes execute functional requirements. when go case of store: functions in store personnel communicating client directly. not implementation how walk end trying find pair of trousers. not process how open or close shop. yes how welcome client , asking type of trousers wants have. in application: there interfaces/classes used directly interacting visitors of application , create type of contract how describe interaction? design of di - container. not use di on place. di comes performance penalty , maintenance problem. more di use, more layers have, less know happens where. use di preferrably beneficial , implementation change yet caller not know interface has changed nor caller interested in such change. if take guideline, can make distinctions classes hide via di - container , classes not.
Comments
Post a Comment