Java – how do I ensure that a given dependency is included in my application using the spring framework?
This will be an indescribable problem, but there are
We are using Delphi spring framework ( http://code.google.com/p/delphi-spring-framework/ )
Suppose I have interface a
Similarly, I have unitb to declare interfaceb implemented by ClassB
Both use spring container to register their interfaces and classes in their respective initialization sections
Interfacea depends on interfaceb, but since we use spring, UNITA does not have unitb in its uses clause In other words, we've done our job – we've separated UNITA and unitb, but we can still make interfacea dependent on interfaceb
However, in view of the above, we need to ensure that both UNITA and unitb are included in the project so that the dependencies can be resolved
Imagine, now, we start a new project The new project uses UNITA, but the developers don't realize that if they want to use UNITA, they must also include unitb in the project Compiler errors do not occur because dependencies are resolved at run time, not compile time
This is the question: what is the right way to ensure that this dependency on unitb is known before the application is deployed?
We can foresee the situation in complex applications. Despite comprehensive testing, the given code path may not be executed for a long time, and this missing dependency is not found before deployment
We have implemented a system in which each interface resolution call is accompanied by a requires call, which checks and throws exceptions at startup to ensure that we see errors However, we would like to know if there are "best practices" or standard methods to detect this problem or otherwise deal with it
Add: is this a problem with Java and other languages?
Solution
I'm a little confused It uses interfaces that allow you to loosely couple rather than IOC containers It is natural that UNITA will use unitb if the interface B is declared
Interface implementation is a different story Those that need to reference the interfaces they implement and any interfaces they use, but should not reference any other implementation
I don't use spring for Delphi, but I'm familiar with other IOC containers If it behaves similarly, you will register an interface and its implementation When you call resolve, you will pass in the name of the interface or other information about the interface (type information) and expect the IOC container to return a reference to the interface you requested The implementation behind the interface depends on which implementations are registered and the rules to resolve the request
An exception occurs if the interface you requested has never been registered Some IOC containers can resolve the whole dependency chain through one call
You need a way to determine whether dependencies will be resolved at run time at build time, but dependencies will not be registered until run time No matter what tool you use, I don't think it can be guaranteed
UNITA shall include unitb in its terms of use The implementation of interfaces in UNITA and unitb may and should be located in units separate from a and B, especially if each interface has multiple implementations
Then, developers who use UNITA in the project will also be forced to include unitb in the project If they use test driven development in new projects, they will soon find that they need to provide implementations for interfacea and interfaceb (even if they are only simulations) so that their tests can pass
Frankly, if you ignore a key dependency and deploy the project without it, the testing is not thorough enough Unit tests may not capture this, but this is what a set of integration tests usually capture
I would recommend integration tests like fit or fitness (although I'm not sure how mature the fit4 Delphi project is) Anyone who can write word documents or edit wikis can write tests. Developers only need to write classes that allow tests to drive production code If set correctly, you can run most integration tests against the actual release version of the project