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 6828647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:24:28+00:00 2026-05-26T22:24:28+00:00

Say I have these 3 layers of my code: 1. Database layer (ORM) 2.

  • 0

Say I have these 3 layers of my code:
1. Database layer (ORM)
2. BusinessLogic
3. Application

Now, I write my code as follows:

  1. Database layer:
    This mainly has CURD operations over database.

    class MyDatabaseLayer {
        public int findValue(int k) {
            // find v
        }
    
        public void insertValue(int k, int v) {
            // Insert v
        }
    }
    
  2. BusinessLogic:
    This holds the actual logic to call database layer and do stuff.

    class MyBusinessLogic {
        private MyDatabaseLayer dbLayer;
        public MyBusinessLogic(MyDatabaseLayer dbLayer) {
            this.dbLayer  = dbLayer;
        }
    
        public int manipulateValue(int k) {
            dbLayer.findValue(k);
            //do stuff with value
        }
    }
    
  3. Application layer:
    This calls the business logic and displays the data

    MyBusinessLogic logic = new MyBusinessLogic(new MyDatabaseLayer ()); //The problem
    logic.manipulateValue(5);
    

Now if you see in the application layer, it knows about the database layer, which is wrong. It knows too much.

Misko Hevery says: Constructor injection is good. But if I follow that, how will I achieve abstraction? And how can Google Guice help me here?

  • 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-26T22:24:28+00:00Added an answer on May 26, 2026 at 10:24 pm

    Note that for the testability Misko refers to, you ideally want to make interfaces for MyDatabaseLayer, MyBusinessLogic, etc. and have the constructors take those interfaces rather than concrete classes so that in testing you can easily pass in fake implementations that don’t actually use the database, etc.

    With Guice, you would bind the interfaces to concrete classes in a Module or Modules. You would then create an Injector using those Modules and get some root object (for example, your application object) from the Injector.

    Injector injector = Guice.createInjector(new AbstractModule() {
      @Override protected void configure() {
        bind(MyDatabaseLayer.class).to(MyDatabaseLayerImplementation.class);
        // etc.
    });
    MyApplicationLayer applicationLayer = injector.getInstance(MyApplicationLayer.class);
    

    In MyApplicationLayer, you’d inject the business logic:

    @Inject
    public MyApplicationLayer(MyBusinessLogic logic) {
      this.logic = logic;
    }
    

    This is of course a very simple example and there are much more complex things you can do. In a web application, for example, you can use constructor injection on servlets using Guice Servlet rather than getting an object directly out of the Injector after creating it.

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

Sidebar

Related Questions

Say I have a database called Poll and have these two table structures. poll
Say I have this url: http://site.example/dir/ In this folder I have these files: test.ascx.cs
Say I have a database containing Books and Users and these users have certain
Say you have a string 3.4564. A sample set in the database has these
Say you have these two methods: Number 1: void AddPerson(Person person) { // Validate
Let's say we have these two classes: public class Base { public static int
Let's say I have these two arrays: string[] arr1 = new string[2]{Hello, Stack} string[]
I want to generate a list using my django model say I have these
Let's say I have DatabaseA with TableA, which has these fields: Id, Name. In
Say that I have many blogs, and each blog has_many posts. These are stored

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.