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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:16:25+00:00 2026-05-11T17:16:25+00:00

Edit: This is not a conflict on the theoretical level but a conflict on

  • 0

Edit: This is not a conflict on the theoretical level but a conflict on an implementation level.

Another Edit:
The problem is not having domain models as data-only/DTOs versus richer, more complex object map where Order has OrderItems and some calculateTotal logic. The specific problem is when, for example, that Order needs to grab the latest wholesale prices of the OrderItem from some web service in China (for example). So you have some Spring Service running that allows calls to this PriceQuery service in China. Order has calculateTotal which iterates over every OrderItem, gets the latest price, and adds it to the total.

So how would you ensure that every Order has a reference to this PriceQuery service? How would you restore it upon de-serializations, loading from DBs, and fresh instantiations? This is my exact question.

The easy way would be to pass a reference to the calculateTotal method, but what if your Object uses this service internally throughout its lifetime? What if it’s used in 10 methods? It gets messy to pass references around every time.

Another way would be to move calculateTotal out of the Order and into the OrderService, but that breaks OO design and we move towards the old “Transaction Script” way of things.

Original post:

Short version:
Rich domain objects require references to many components, but these objects get persisted or serialized, so any references they hold to outside components (Spring beans in this case: services, repositories, anything) are transient and get wiped out. They need to be re-injected when the object is de-serialized or loaded from the DB, but this is extremely ugly and I can’t see an elegant way to do it.

Longer version:
For a while now I’ve practiced loose coupling and DI with the help of Spring. It’s helped me a lot in keeping things manageable and testable. A while ago, however, I read Domain-Driven Design and some Martin Fowler. As a result, I’ve been trying to convert my domain models from simple DTOs (usually simple representations of a table row, just data no logic) into a more rich domain model.

As my domain grows and takes on new responsibilities, my domain objects are starting to require some of the beans (services, repositories, components) that I have in my Spring context. This has quickly become a nightmare and one of the most difficult parts of converting to a rich domain design.

Basically there are points where I am manually injecting a reference to the application context into my domain:

  • when object is loaded from Repository or other responsible Entity since the component references are transient and obviously don’t get persisted
  • when object is created from Factory since a newly created object lacks the component references
  • when object is de-serialized in a Quartz job or some other place since the transient component references get wiped

First, it’s ugly because I’m passing the object an application context reference and expecting it to pull out by name references to the components it needs. This isn’t injection, it’s direct pulling.

Second, it’s ugly code because in all of those mentioned places I need logic for injecting an appContext

Third, it’s error prone because I have to remember to inject in all those places for all those objects, which is harder than it sounds.

There has got to be a better way and I’m hoping you can shed some light on it.

  • 1 1 Answer
  • 1 View
  • 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-11T17:16:25+00:00Added an answer on May 11, 2026 at 5:16 pm

    I’ve found the answer, at least for those using Spring:

    6.8.1. Using AspectJ to dependency inject domain objects with Spring

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

Sidebar

Related Questions

This is clearly not the case though. My JS : $(.job_charge.item-block).live({ mouseenter: function(){ $(this).find('.edit-and-delete').stop(true,true).fadeIn();
This works: Entities.WorkOrderSet.Where(MyCustomMethod); This does not: Entities.WorkOrderSet.Where(o => MyCustomMethod(o)); ( [Edit] Even without new
[edit] I am NOT using jquery in this app. Looking for a way to
edit: I'm not looking for you to debug this code. If you are familiar
I am not sure if this is the cleanest way of writing an edit.
I'm not sure what kind of terminology to use in this so please edit
Not entirely sure of a good title for this, feel free to edit it
Edit: This problem was down to me passing the wrong view to the Touch
edit This question is solved! Having something weird. I'm using html { font-size: 100%
Edit: this question is outdated. The jsonlite package flattens automatically. I am dealing with

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.