2

In my application I keep my business data as a huge object containing a lot of small objects, properties etc.

To print reports based on this data I've created a printing module.

In printing module I need to access almost all of the properties, subobjects and fields thus I pass my HugeObject as a parameter to printing module.

The problem is that out HugeObject is being passed by reference can be unintentionally changed in printing module.

How to protect HugeObject from changes?

TOP KEK
  • 2,593
  • 5
  • 36
  • 62

2 Answers2

3

To deep copy the entire object, serializing and deserializing is a good way to do it. You can find an article on CodeProject, which describes the implementation details. http://www.codeproject.com/Articles/23832/Implementing-Deep-Cloning-via-Serializing-objects

Espen Burud
  • 1,871
  • 10
  • 9
1

Even you need almost the all from HugeEntity to be used in Print module I would suggest creating a special PrintableEntity and using special EntityToPrintableAdapter you can adapt business entity to PrintableEntity. In this way you could keep decoupled printing module from business module entities so business entities would not leak out in other modules. So even in future requirements for Printable Entity change - your business layer stay unchanged and you just have to update Adapter.

If you will decide cloning object, see this SO post, here is I've provided DeepCopy() extension method with NUnit test harness as well.

Community
  • 1
  • 1
sll
  • 61,540
  • 22
  • 104
  • 156