Daily Archives: January 27, 2010

Passing around an ObjectContext: by value or reference?

<Refresher>

When you pass parameters in VB, you use the ByVal and ByRef keyworks. The default in VB is ByRef with a nuance (see ByRef Parameters Passed by Value)  In C#, no keyword means by value and alternatively, you use the ref keyword to pass a reference.

The key difference is that when you pass a variable by value , you are just passing a copy. The originating variable cannot be replaced.

When passing by reference, you are working with the actual variable and can replace it.

The part that frequently evades developers is that if the value is a reference type (i.e. does not contain it’s values directly compared to, for example, an int which does) you can modify those related values whether you pass by reference or by value.

</Refresher>

Now let’s look at this with respect to an ObjectContext being passed as a parameter. An ObjectContext has it’s own values but also has references to other values (e.g. the ObjectStateEntry types that it maintains to keep track of entities).

Whether you pass the context by value or reference, the entities that the context is tracking will be impacted when you execute queries or call SaveChanges with the context.

When passing it by value you won’t replace the context, e.g, passedinContext=new MyEntities() won’t affect the originating context.

When passing it by ref, you CAN replace the context e.g., passedinContext=new MyEntities() will reinstantiate the originating context. And if you replace the context, you will lose ALL of the state information for the currently tracked entities. Those entities will be orphaned – just sitting in memory with nobody tracking them and if you do reattach them to the new instance of the context, they will all be unchanged.

That’s a big problem.

So unless you are trying to achieve some specific behavior, you should definitely pass byval. In fact you should define your method signature that takes the parameter to ensure that you are passing by value.