oshai
oshai

Reputation: 15365

add guice to an existing application

I want to start using guice in an existing application that has legacy code. Are there any pointers and BKM's on how to start doing that? I found one approach here, and looking to get some further information and details.
Thanks!

EDIT: some more details about my app: it is a server app, that is running in a home-built server application, so there is no gui involved. It has a lot of legacy code that I have doubts if/how to migrate it.

Upvotes: 1

Views: 652

Answers (2)

chubbsondubs
chubbsondubs

Reputation: 38706

Your strategy depends on how your application is built today. If you are using singletons excessively or some other framework. I've done a lot of porting applications from no framework into something like Spring or Guice. It's not an easy task, and how successful you are at it depends a lot on how much code, how many people are on your team, and how well the team accepts new ways of building the application. If you don't have buy in from your team then I'd start there before trying to code it. Convince people on your vision for the app, answer their questions, allow them to design a little in their heads, and welcome their input. If they have big concerns allow them to voice them, and if you don't have a solution acknowledge it, and go back and design one, then present it. Repeat it until you have everyone on board.

Now porting the code. It depends on the type of app you have: web based, desktop, etc. But, generally when I'm porting this stuff I create an empty project, and set that up the way you designed it with your team. Then start to pull over the long lived objects into the container. Singletons are good places to start because they typically live the entire time of the application. Start instantiating them in the container instead of their getInstance() methods. You might have to break the singleton pattern and expose their constructors, but eventually you want to rid yourself of singletons. You can keep the static ref. As you pull objects over refactor them from using getInstance() to using a reference that they are given through an instance variable/getter/setter.

Slowly you should be able to get to a point where you can execute your application from the Guice container. Maybe you've only ported a portion of the application at this point. Keep porting portions one by one and testing them in the container. At this point you've got enough of a base you can enlist help from other team members. Eventually you'll have the whole app ported over, and you can now start to refactor parts of it to work the Guice way. Go back and eliminate your static refs and calls to getInstance() where you can.

I've always found setting up a skeleton and porting over rather than refactoring in place to be much more efficient way of handling these types of conversions. Calling an all stop on new work is also critical, and it will save you lots of heartache in the end. I've tried not stopping new work, and these types of conversions can drag out over many releases because the team continues to new code in the legacy way. Sometimes starting after a new release during the lull period before real work starts on the next release can give you an extra boost so you can get to the point where other members can help you port because you have the skeleton laid down.

Remember management too. If they demand only new features you're going to have a hard time controlling the team. If they don't buy in to your effort they can make it very hard to finish.

Upvotes: 4

Andy
Andy

Reputation: 8949

Expect to be confused for a while. I was anyways. I love the video on the Guice home page.

I think incorporating Guice, or any dependency injection framework, into your application was accurately described to me as like pulling on a thread in a sweater and never really finding the end. What I mean by that, is that it could change your approach in several areas of your applications. Like, from servlets, to jdbc frameworks (we use mybatis-guice), to unit tests (check out jukito).

The Guice user group is a good resource.

Don't expect to do it all at once. It can take some time to get fully into developing the Guice way.

Upvotes: 3

Related Questions