Questions tagged [lazy-initialization]
37 questions
37
votes
6 answers
Why isn't lazy evaluation used everywhere?
I've just learnt how lazy evaluation works and I was wondering: why isn't lazy evaluation applied in every software currently produced? Why still using eager evaluation?

John Smith
- 1,719
- 2
- 17
- 20
17
votes
6 answers
DDD Injecting Services on Entity Methods Calls
Short format of question
Is it within best practices of DDD and OOP to inject services on entity method calls?
Long format example
Let's say we have the classic Order-LineItems case in DDD, where we have a Domain Entity called an Order, which also…

e_i_pi
- 839
- 1
- 7
- 18
10
votes
4 answers
Is there any reason lazy initialization couldn't be built into Java?
Since I'm working on a server with absolutely no non-persisted state for users, every User-related object we have is rolled out on every request.
Consequently I often find myself doing lazy initialization of properties of objects that may go…

Nicole
- 28,111
- 12
- 95
- 143
8
votes
1 answer
Is there a name for the counterpart of the lazy loading pattern?
Definition of the lazy loading pattern from Wikipedia:
Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in the…

R Sahu
- 1,966
- 10
- 15
6
votes
1 answer
Is it better to use lambda functions or boolean variables to record state
I have heard some people claiming that boolean state variables are generally bad and should be avoided when possible. Apparently in many cases it is possible to put state into lambda functions, instead of using booleans. An example to this approach…

Michael Veksler
- 179
- 4
6
votes
4 answers
How to avoid pollution of logic with lazy-loaded async properties
To be able to scale I would like to use async programming. It works really well if I have to read something from db and push to frontend, however I do not know how to use it correctly in blobs of buniess logic that live their own lives. I have …

Shadow
- 363
- 3
- 10
6
votes
2 answers
How to populate Lazy object from database
I have these classes:
public class Order
{
private Lazy> _volumes;
long ID { get; private set; }
string Description { get; private set; }
IEnumerable Volumes { get { return _volumes.Value; } }
…

Tea With Cookies
- 161
- 2
6
votes
2 answers
Does laziness yield more race conditions?
I recently ran into a race condition while accessing a configuration setting. After examining what I could of the code, I came to the conclusion that the Configuration class' laziness1 was the source of the race condition. Pondering that lead me…
user53019
5
votes
4 answers
What is a good pattern for combined caching and reinitialization?
I have a situation where I have three requirements:
Lazy initialization - don't create the collection until asked for it
Caching - keep the collection in memory on the object
Reinitialization - be able to reinitialize the collection when desired,…

Nicole
- 28,111
- 12
- 95
- 143
5
votes
0 answers
Entity Framework - Loading Related Entities Explicitly
I have been using Entity Framework for a few years. I have flip-flopped between calling out to repositories in my business logic or using lazy loading to retrieve data as I work my way through the code.
The problem with the first approach is that a…

Travis Parks
- 2,495
- 1
- 19
- 23
4
votes
1 answer
Webpack and Lazy Load for large-scale Web Application
Background
I am trying to develop with Webpack and JavaScript. Webpack would bundle all source code into one single file. When application becomes large, the file would be very large and cause negative impact to the performance.
Webpack
Webpack…

Ben Cheng
- 349
- 1
- 10
4
votes
1 answer
Is it ever appropriate to lazy load properties?
Consider the following contrived program:
class Program
{
static void Main(string[] args)
{
var myClass = new MyClass();
var stuff = myClass.MyProperty; // this takes 5 seconds
var stuff2 = myClass.MyProperty; // this…

Will Ray
- 141
- 4
4
votes
2 answers
Lazy loaded property signatures in business objects
Let's say I am designing business objects around a poorly optimized database that have no ability to change.
I have a Person object with a ShippingAddress property. The ShippingAddress is very expensive to load and located in a separate table, and…

TheCatWhisperer
- 5,231
- 1
- 22
- 41
3
votes
1 answer
Why does this static field always get initialized over-eagerly?
I am looking at this excellent article from Jon Skeet.
While executing the demo code, Jon Skeet says that we can expect three different kinds of behaviours. To quote that article:
The runtime could decide to run the type initializer on loading the
…

TheSilverBullet
- 1,091
- 1
- 11
- 22
3
votes
1 answer
Lazy loading can lead to stale data, violates IoC?
Trying to be a better programmer
I have an application that keeps track of Roles and Permissions, I had classes for Role and Permission which were just value objects.
class Role
{
int RoleID
string RoleName
}
class Permission
{
int…

tunmise fasipe
- 734
- 5
- 16