Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6841411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:57:47+00:00 2026-05-26T23:57:47+00:00

I am getting into Inversion of Control, specifically using Guice and RoboGuice for Android

  • 0

I am getting into Inversion of Control, specifically using Guice and RoboGuice for Android and I have a question.

I have a method call that returns a Resource (which is essentially an XML or JSON String).

public Resource getResource(){
// Some implementation details that call a web service and throw the result in a string...
String resource = ........
}

The Resource class is really just a wrapped String, so I figured it made sense to pass it in in the constructor, since it is an essential part of a Resource object.

public class Resource{
   Resource(String theXMLorJSON){
   ...
   }
}

A couple of questions:

  1. How do I construct a new Resource in the getResource call? I would think that I want to use IoC and not call new in the method.
  2. If another class takes a Resource in the constructor, how can I use the Guice container to construct it when I need a dynamic String at construction time? I just asked a similar question and believe there may be a specific way to handle this using Guice.

Thanks so much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T23:57:48+00:00Added an answer on May 26, 2026 at 11:57 pm

    I think you may be misunderstanding something about dependency injection. You don’t need to try to avoid using new in all cases… you primarily want to avoid using new to create anything that you might want to be able to mock out for testing, and it’s generally best to allow the container to wire up any class that depends on such an object.

    Your Resource class, though, sounds like a simple value object that you can easily create manually in any testing you do. It also doesn’t depend on any kind of services… it just contains a String. So there’s no reason to try to have the container create it.

    The class containing the getResource() method, on the other hand, you definitely want the container to create, because you’d like to be able to use something that depends on that class in testing without having to actually call a web service.

    Note that if you have a class with a constructor that takes both dependencies you want injected by the container and parameters that are only known at runtime, you need to create an intermediate factory of some kind with a method that only takes the runtime parameters. With Guice you can automatically create such a factory from an interface using the Assisted Inject (not sure if that works with RoboGuice, but it’s easy to create such a factory implementation manually too).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just been getting into low level programming (reading/writing to memory that sort
I'm really getting into ko and have a question regarding the applyBindings function. It
I am just getting into MEF and I have come across a problem that
I'm getting into more of a TDD workflow, and have a mix of MVC
I'm just getting into creating some WCF services, but I have a requirement to
I just started getting into BizTalk at work and would love to keep using
I'm just getting into unit testing, and have written some short tests to check
I'm getting into dynamic data sites (remarkably powerful), and enjoying it. However, I have
I'm newly getting into creating and programming reports using SQL Server Reporting Services. One
I've been getting into Ruby over the past few months, but one thing that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.