Skip to main content
Stack Overflow for Teams is now Stack Internal: See how we’re powering the human intelligence layer of enterprise AI. Read more >
15 votes
Accepted

DDD Injecting Services on Entity Methods Calls

It's totally fine to pass a Domain service in an entity call. Say, we need to calculate an invoice sum with some complicated algorithm that can depend on, say, a customer type. Here is what it might ...
Vadim Samokhin's user avatar
14 votes

DDD Injecting Services on Entity Methods Calls

I am shocked to read some of the answers here. It's perfectly valid to pass domain services into entity methods in DDD to delegate some business calculations. As an example, imagine your aggregate ...
diegosasw's user avatar
  • 407
10 votes

DDD Injecting Services on Entity Methods Calls

Is it within best practices of DDD and OOP to inject services on entity method calls? No, you should not inject anything inside your domain layer (this includes entities, value objects, factories and ...
Constantin Galbenu's user avatar
8 votes
Accepted

Is it better to use lambda functions or boolean variables to record state

Using these kinds of object-oriented or functional techniques can be super neat and elegant. If you need a fancy name for what you are doing here, I suggest the State Pattern, with function objects ...
amon's user avatar
  • 136k
8 votes

DDD Injecting Services on Entity Methods Calls

The key idea in DDD tactical patterns: the application accesses all data in the application by acting on an aggregate root. This implies that the only entities which are accessible outside of the ...
VoiceOfUnreason's user avatar
5 votes

Extending the concept of Lazy Loading, to also unloading

The normal approach to caching would be to put stuff into the cache when you need it and remove it when you try to put a new thing into the cache but don't have enough memory to do so. You then have ...
Ewan's user avatar
  • 84.5k
4 votes

Best way to code lazy loading outside the model in vanilla c#

Your implementation is correct, but it's a wheel that did not need to be reinvented. There's a Lazy<T> class that already provides this functionality. MSDN link var myLazySchool = new Lazy<...
Flater's user avatar
  • 59.5k
4 votes
Accepted

Is it a good idea to use a Lazy wrapper type to encapsulate lazy initialization in Java?

There is nothing wrong with lazy initialization. In fact, that's a very common use case. I've written Lazy<T> helpers myself, and lazy initialization is one of the better uses of the singleton ...
amon's user avatar
  • 136k
4 votes

DDD Injecting Services on Entity Methods Calls

The answer is: definitely NO, avoid passing services in entity methods. The solution is simple: just let the Order repository return the Order with all of its LineItems. In your case the aggregate ...
xpmatteo's user avatar
  • 349
3 votes

Webpack and Lazy Load for large-scale Web Application

All mature JavaScript loading and bundling tools, from Webpack to RequireJS to SystemJS, provide techniques for doing this. In Webpack it's enabled by a feature known as code splitting. In SystemJS ...
Aluan Haddad's user avatar
3 votes

DDD Injecting Services on Entity Methods Calls

Generally speaking value objects belonging to aggregate don't have repository by themselves. It's aggregate root's responsibility to populate them. In your case, it's your OrderRepository's ...
ivenxu's user avatar
  • 622
3 votes

Is there a name for the counterpart of the lazy loading pattern?

Lazy Loading the design pattern is the opposite of Eager Loading the design pattern. This is when instead of deferring the load to hopefully avoid it, you perform the load immediately to avoid any ...
Kain0_0's user avatar
  • 16.6k
3 votes
Accepted

Is it a good idea to use "lazy val" for correctness?

Yes, it is considered okay in general. A relatively common use is to initialize fields of a supertype when they have to depend on the state of a subtype (but even there you need to be careful). See ...
Alexey Romanov's user avatar
3 votes

In what other locations besides infinite streams and infinite lists is memoized lazyness useful?

Some other situations that jump to mind: Iterative processes that need to use the results of some earlier values but not others (which provides for simple ways of efficiently implementing processes ...
Jules's user avatar
  • 17.9k
3 votes

Extending the concept of Lazy Loading, to also unloading

Many garbage collected languages have the concept of a WeakReference<T>, including Java and .Net. This allows your code that loads the data to have a reference that can be garbage collected. ...
Berin Loritsch's user avatar
3 votes

Modelling seats of a table in a social game

A Seat should know whether or not it is occupied. Simply because someone joins or leaves the game doesn't mean you need a new Seat(). The player has just vacated that spot at the table. That being ...
Greg Burghardt's user avatar
2 votes
Accepted

C++ tactics / data structures / design patterns to avoid or postpone unnecessary object creation?

I’d look into the Object Pool pattern that basically does the recycling you mention yourself. It’s actually how both Android and iOS handle the problem of “staggering” while scrolling on a list of ...
joakim's user avatar
  • 196
2 votes
Accepted

Throttling the factory function of a Lazy<T> instantiated with LazyThreadSafetyMode.PublicationOnly

As there is no standard way to get such a behaviour, and if you really need it, then by all means that is a valid approach. It may even be desirable to implement your own helpers to facilitate easier/...
Eugene Podskal's user avatar
2 votes
Accepted

Is there a better way to use Lazy<T> than what I'm doing?

Those two versions do different things. Consider what happens if the DataException is thrown and caught. The first will search _contactRepo at most once. The second will search _contactRepo each ...
Caleth's user avatar
  • 12.4k
1 vote

Best way to code lazy loading outside the model in vanilla c#

Your solution is not thread-safe. The existence check and the retrieval of data are two separate methods. So they are not treated as one, atomic method, which might result multiple calls of data ...
Peter Csala's user avatar
1 vote
Accepted

Is it good practice to call service layer through domain object getters?

is it good practice to call service layer methods through domain object getters? I strongly suspect you would come to regret that over the lifetime of a project. The usual pattern is that your domain ...
VoiceOfUnreason's user avatar
1 vote

C++ tactics / data structures / design patterns to avoid or postpone unnecessary object creation?

As long as no heap memory is allocated, creating objects in C++ (when no calculations are performed in the constructor) isn't more expensive than calling a function and assigning individual variables, ...
D. Jurcau's user avatar
  • 557
1 vote

REST Lazy Reference Create GET or POST?

It's not black and white. Section 9.2.1 of RFC 9110 answers your question completely: Request methods are considered "safe" if their defined semantics are essentially read-only; i.e., the ...
Grant Gryczan's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible