I work on a Java backend enterprise app that heavily uses the Spring framework. It's a pretty substantial app at over 500k LOC but I can't shake the feeling that the overall design is pretty poor. Other than scenarios where it's imposed by a 3rd party framework there isn't a trace of inheritance, interfaces, and other things you would expect in an application written in an OO language. Classes over 5,000 lines are common place as are methods over 1,000 lines. In fact other than where the code interacts closely with a 3rd party framework the code base feels like procedural code with 1:1 mappings of classes to pages of the front end web app.
For example let's say there's a page for users to update information about vehicles for sale. Typically there will be a service class which talks directly to the client and a dao class that's autowired which queries the database.
What happens is everything gets piled into these types of classes resulting in a huge mess of code. Methods often contain a lot of conditionals to handle various cases which can be broken up:
I've been thinking about breaking up some of the large methods and classes out to something like this:
I don't know if this is the right approach because it has it's own issues such as potential class explosion. The other issue lies with Spring and how dependency injection works. In the current code all the Service and Dao classes are Spring managed beans. This is nice because you can just autowire all the needed dependencies into the class. If you introduce factory methods which create these updater objects outside of Spring they won't be able to use autowiring. You'd have to pass it to them, for example:
The above doesn't really make sense because the service classes doesn't even know which implementation of the updater is being returned and won't know which dao is suitable. The factory method could provide the dao to the updater directly but unless the factory class itself is declared as a Spring bean it won't be able to autowire the needed dao objects. Maybe that's the right thing to do but I'm not sure.
Another pain point with organizing code and using factory methods this way is unit testing. With dependency injection you can easily use mocks.
Sorry about the scatter brained post. I'm basically looking for ideas on how to do good design while utilizing Spring DI/IoC.
For example let's say there's a page for users to update information about vehicles for sale. Typically there will be a service class which talks directly to the client and a dao class that's autowired which queries the database.
Code:
@Service("vehicleUpdaterService")
public class VehicleUpdaterService {
@Autowired
private VehicleUpdaterDao dao;
//Methods called by vehicle updater page with business logic here
}
@Repository
public class VehicleUpdateDao {
//Methods with sql calls to update/read db needed by the update service
}
What happens is everything gets piled into these types of classes resulting in a huge mess of code. Methods often contain a lot of conditionals to handle various cases which can be broken up:
Code:
@Service("vehicleUpdaterService")
public class VehicleUpdaterService {
@Autowired
private VehicleUpdaterDao dao;
public void updateVehicle(Vehicle v) {
if (v.type.equals("car")) {
if (v.condition.equals("new")) {
...
} else if (v.condition equals("used")) {
...
} else if (v.condition.equals("recertified")) {
....
}
} else if (v.type.equals("suv") {
...
}
...
}
}
I've been thinking about breaking up some of the large methods and classes out to something like this:
Code:
VehicleUpdaterFactory //Returns relevant updater class for vehicle
IVehicleUpdater //Interface
CarUpdater //Superclass with common code
NewCarUpdater //Specific code
UsedCarUpdater
RefurbishedCar Updater
SuvUpdater
//etc..
I don't know if this is the right approach because it has it's own issues such as potential class explosion. The other issue lies with Spring and how dependency injection works. In the current code all the Service and Dao classes are Spring managed beans. This is nice because you can just autowire all the needed dependencies into the class. If you introduce factory methods which create these updater objects outside of Spring they won't be able to use autowiring. You'd have to pass it to them, for example:
Code:
@Service("vehicleUpdaterService")
public class VehicleUpdaterService {
@Autowired
private VehicleUpdaterDao dao;
public void vehicleUpdater(Vehicle v) {
IVehicleUpdater vu = VehicleFactoryUpdater.getInstance(v);
vu.setDao(dao);
vu.update();
}
The above doesn't really make sense because the service classes doesn't even know which implementation of the updater is being returned and won't know which dao is suitable. The factory method could provide the dao to the updater directly but unless the factory class itself is declared as a Spring bean it won't be able to autowire the needed dao objects. Maybe that's the right thing to do but I'm not sure.
Another pain point with organizing code and using factory methods this way is unit testing. With dependency injection you can easily use mocks.
Sorry about the scatter brained post. I'm basically looking for ideas on how to do good design while utilizing Spring DI/IoC.