Kevin Rave
Kevin Rave

Reputation: 14456

Passing Entities as method parameters vs sending new object created from part of entity

I get a list of @Entityes for a query (so called Main Entities). I have to fill some of the Transient fields in these Entities with values from different queries / entities. So each of these transient fields needs different combination of fields from the Main Entities and based on that I should run query and fill the transient fields. These are done in 3 to 4 different methods.

My question is, what is the better approach in terms of performance and practice?

  1. Pass Main Entities list (could be in 100s) to each of these methods and get them filled in the method or
  2. Create an object based on the fields needed for each method and get the result back and fill the entities?

When you pass the entities to the methods, you are not creating copies, you are just passing original list? No performance related issues?

Upvotes: 0

Views: 1942

Answers (1)

nif
nif

Reputation: 3442

Except for primitive types, every object passed to methods as parameter is passed as reference. Therefore you won't create copies of your entities if you pass a List<EntityClass> object to your methods.

If there isn't any other reason not to do so, I would go with option 1 and pass the list of entities to the methods.

Upvotes: 1

Related Questions