Does Java have the concept of Generics like C#? I think a bigger question to consider is "Why is software engineering such an immature engineering discipline?"
Yes! Properly using a dependency injection container is a marvelous fix to this problem. We had a similar situation in a fairly large codebase, and we slowly fixed this problem over a few months time, eventually removing almost all dependencies on Context. We used Spring, but whatever. The thing to remember is NOT to shove Context into the DI container. Just put the dependencies. We would focus on one Context "getter" at a time and slowly remove all the usages of it, eventually eliminating the getter itself.
I highly recommend doing this! Test are so much easier.
Nice post, but you should be more careful about your use of "false negative". In testing, a "false negative" occurs when something was broken, yet the tests all passed. You're referring to a "false positive", in which the tests fail even though nothing is wrong.
what if my class needs too many configs - do I need to send each as separate constructor argument or should I wrap the configs in a wrapper and send that in constructor. Basically my class is dbutil class which needs jdbc url,user,password and table names. Now one method of dbutil fetches data from table1 and another from table2. Jdbc url,user,password,table1,table2 are read from configuration file at startup of application along with various other configs. What should I pass to dbutil constructor- a wrapper containing url,user,password,table1,table2 and store that in intance variable or just jdbc url,user,apssword in constructor and table name should be passed only when calling function of dbutil.
I was thinking about using Google Guice Providers to solve the Problem.
ReplyDeleteSomething like:
@Provider
EngineProvider(Context context) { ...}
public Engine get() { return context.getEngine(); }
Then you can have a Context and only inject Engine into the Mechanic class. The adaption is handled by Guice.
Peace
-stephan
Does Java have the concept of Generics like C#? I think a bigger question to consider is "Why is software engineering such an immature engineering discipline?"
ReplyDelete@stephan
ReplyDeleteYes! Properly using a dependency injection container is a marvelous fix to this problem. We had a similar situation in a fairly large codebase, and we slowly fixed this problem over a few months time, eventually removing almost all dependencies on Context. We used Spring, but whatever. The thing to remember is NOT to shove Context into the DI container. Just put the dependencies. We would focus on one Context "getter" at a time and slowly remove all the usages of it, eventually eliminating the getter itself.
I highly recommend doing this! Test are so much easier.
I hope the android team reads this post.
ReplyDelete@apexrally Yes, it does, although they wouldn't particularly help in this problem
ReplyDeleteFYI The needle referred to in the common phrase is not a sewing needle, it is a haystack needle, used for pinning the haystack together.
ReplyDelete:)
Is the android team reading this ?
ReplyDeleteWhy not just push the dev team for a better design?! :)
ReplyDeleteNice post, but you should be more careful about your use of "false negative". In testing, a "false negative" occurs when something was broken, yet the tests all passed. You're referring to a "false positive", in which the tests fail even though nothing is wrong.
ReplyDeletewhat if my class needs too many configs - do I need to send each as separate constructor argument or should I wrap the configs in a wrapper and send that in constructor.
ReplyDeleteBasically my class is dbutil class which needs jdbc url,user,password and table names. Now one method of dbutil fetches data from table1 and another from table2. Jdbc url,user,password,table1,table2 are read from configuration file at startup of application along with various other configs. What should I pass to dbutil constructor- a wrapper containing url,user,password,table1,table2 and store that in intance variable or just jdbc url,user,apssword in constructor and table name should be passed only when calling function of dbutil.