Questions tagged [lazy-initialization]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
3 answers
151 views

Ensuring run-once behavior in a multi-threaded environment via volatile lambdas?

I'm coding an enum strategy pattern where one of the strategies makes use of an ScheduledExecutor: class ControllerImpl { //... boolean applyStrat(StratParam param) { getStrat().apply(...
walen's user avatar
  • 355
0 votes
2 answers
466 views

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

I have to implement a LazyLoading on the properties of my Entites Class. I can't use any framework and external dll (nugets package are forbiden, I can't use Entity Framework or Castle Dynamic Proxy ...
user1594914's user avatar
2 votes
1 answer
912 views

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

In our code base, we have several static or instance members that we would like to initialize lazily. If the initialization cannot yield null, It's easy to implement. Otherwise, one could use an ...
mperktold's user avatar
  • 151
-3 votes
1 answer
1k views

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

Tell me anybody, is it good practice to call service layer methods through domain object getters? Let me show you with an example: public class User { private long id; private String name; ...
Artem's user avatar
  • 3
2 votes
1 answer
107 views

Modelling seats of a table in a social game

Assume we want to model a table where players can sit down to get together to play a game (card games, dice games, ...). Assume a few properties associated with each seat class Seat { public int ...
Benj's user avatar
  • 169
0 votes
2 answers
339 views

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

I recently started trying to use the Lazy class to implement lazy loading. However, I'm finding that doing so does not seem to have any advantages over simply implementing it myself via a null-...
Sarov's user avatar
  • 403
1 vote
2 answers
407 views

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

A couple of months ago I wrote a C++ program for computational mathematics that was supposed to compete with a highly optimized C code. After a while I did manage to get it fast enough to beat the C ...
mathreadler's user avatar
1 vote
1 answer
576 views

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

When you use the constructor of Lazy<T> requesting the valueFactory and mode parameters (I mean this one) you can specify LazyThreadSafetyMode.PublicationOnly. This way you can prevent the Lazy ...
Enrico Massone's user avatar
6 votes
1 answer
765 views

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, ...
Michael Veksler's user avatar
2 votes
2 answers
724 views

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

In Scala, declaring a val as lazy means that its value won't be evaluated until it's used for the first time. This is often explained/demonstrated as being useful for optimization, in case a value ...
Joseph Sible-Reinstate Monica's user avatar
0 votes
1 answer
200 views

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

Haskell is one of the few non-strict languages out there. In his paper Why Functional Programming Matters, John Hughes uses (memoized) lazy evaluation (as well as higher-order functions) to implement ...
Qqwy's user avatar
  • 4,769
0 votes
3 answers
867 views

Extending the concept of Lazy Loading, to also unloading

A system that sometimes will need to use a pretrained machine learning model. That model is about 10Gb on disk, and when loaded uses about 10Gb of RAM. Loading it from disk takes a nontrivial amount ...
Frames Catherine White's user avatar
4 votes
1 answer
1k views

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 ...
Ben Cheng's user avatar
  • 349
18 votes
6 answers
17k views

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 ...
e_i_pi's user avatar
  • 859
8 votes
1 answer
975 views

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 ...
R Sahu's user avatar
  • 1,986
1 vote
1 answer
412 views

What are the benefits of using lazy initialization when i.e. calling Doctrine's EntityManager?

TL;DR: I have noted that some tutorials are using an extra method call to retrieve a variable, in my case EntityManager of Doctrine. Is it generally a good idea to use such extra method call, when it ...
Dennis's user avatar
  • 8,187
6 votes
4 answers
5k views

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 ...
Shadow's user avatar
  • 361
2 votes
2 answers
5k views

REST Lazy Reference Create GET or POST?

I have a REST API where: GET or POST /foo/{foo_id}/bar?a=1,b=2,c=3 and GET /bar/{bar_id} both yield 200 { bar_id: <GUID>, foo_id: <foo_id>, a: 1, b: 2, c:3 } When no matching bar is ...
Blake Mitchell's user avatar
1 vote
1 answer
4k views

Is there an easier way to do this with Lazy<T>?

I was trying to find a concrete example online but I couldn't find one that also used one of the class' other attributes. So, can this be done more succinctly with a Lazy<T> object? public ...
user107775's user avatar
4 votes
1 answer
352 views

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 ...
Will Ray's user avatar
  • 141
6 votes
2 answers
2k views

How to populate Lazy object from database

I have these classes: public class Order { private Lazy<IEnumerable<Volume>> _volumes; long ID { get; private set; } string Description { get; private set; } IEnumerable&...
Tea With Cookies's user avatar
1 vote
1 answer
2k views

Is "lazy loading" factories in AngularJS using this $q.defer method good practice

I guess I don't believe in myself as a good programmer but I found out this method on my own (not suggestion I'm the first one to do it, but I didn't follow a guide) so it must be broken somehow. $q ...
Ingó Vals's user avatar
4 votes
2 answers
2k views

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 ...
TheCatWhisperer's user avatar
2 votes
2 answers
2k views

Is lazy loading always required?

I know lazy loading is required when an expensive database call is being made. It would make sense using lazy loading in this example. The below example is in the context of asp.net so it's thread ...
Howls Hagrid's user avatar
2 votes
1 answer
1k views

Applying initialization-on-demand holder idiom at variable level rather than class

I was looking for the idiomatic way to implement thread-safe lazy initialization for a configuration collection retrieved from the DB inside a Spring bean. I decided to adapt the initialisation-on-...
Adam's user avatar
  • 187
0 votes
1 answer
103 views

Does a create() method exist as part of a pattern? [closed]

Is there something special(i.e. a design pattern being related) to a method named create()? Background The case is, I often make some instances of objects (of the same class/prototype). These objects ...
humanityANDpeace's user avatar
6 votes
2 answers
2k views

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 ...
user avatar
-1 votes
2 answers
161 views

Is 'lazy' the correct term for timestamp-based skipping? [closed]

(1) The oldmakeutility looks at timestamps, and if the output is newer than the input, it skips (pointless, time-consuming) re-compilation of e.g. C files. The same is true for many languages and ...
Frank N's user avatar
  • 131
0 votes
1 answer
226 views

Lazy-initialization vs preprocessing in image-based iOS app

I am making an iOS app which contains museum gallery: user can look through exhibits and then select info about it, etc. On the exhibit screen I have two ScrollViews that are different, but are being ...
Richard Topchii's user avatar
5 votes
0 answers
2k views

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 ...
Travis Parks's user avatar
  • 2,505
2 votes
2 answers
807 views

Lazy loading if collection would have any entries

I often see model classes with properties like this: public IList<Note> Notes { get; set; } public bool HasNotes { get; set; } Where the list is initialized lazy, but the boolean property is ...
Philipp M's user avatar
  • 123
3 votes
1 answer
1k views

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 ...
TheSilverBullet's user avatar
3 votes
1 answer
852 views

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 ...
tunmise fasipe's user avatar
37 votes
6 answers
21k views

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's user avatar
  • 1,719
5 votes
4 answers
4k views

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 ...
Nicole's user avatar
  • 28.2k
10 votes
4 answers
2k views

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 ...
Nicole's user avatar
  • 28.2k
1 vote
1 answer
620 views

WPF: Reloading app parts to handle persistence as well as memory management

I created a app using Microsoft's WPF. It mostly handles data reading and input as well as associating relations between data within specific parameters. As a total beginner I made some bad design ...
Ingó Vals's user avatar