Sunday, October 25, 2015

Immutable objects in C#

Introduction


There is saying which goes “MAKE THE POT WHILE THE MUD IS WET”. Once Mud becomes dry it cannot be changed. Immutable objects walks on the similar lines. Once the object is created it cannot be modified by anyway.

What are immutable objects?

Immutable objectsare objects which once loaded cannot be changed / modified by anyway external or internal.

Where are immutable objects used?

If I put in one line Immutable objects are used for data WHICH IS STATIC. Below are some of the instances of the same.
Master data: - One of the biggest uses of immutable objects is to load master data. Master data like country,currency , region etc rarely change. So we would like to load master data once in the memory and then we do not want it to be modified.

Configuration data: - All application needs configuration data. In Microsoft world we normally store these configuration data in to Web.config or App.config file. Such kind of data is represented by objects and these data once loaded in the application memory will not change.Its again a good practice to make these kind of configuration data objects as immutable.

Singleton objects: -In applications we normally create singleton objects for shared static data. So if the shared data is not changing it’s aawesome candidate for immutable objects. In case you are new to Singleton pattern please refer this article Singleton Pattern in C#
 

How can we create immutable objects in C#?

Immutable objects can be created only from immutable classes. To create an immutable class is a three step process :-

• Step 1:- Remove the setters of the class only have getters.
The first step towards creating an immutable class is to remove the setters. As we said values of a immutable class cannot be modified EXTERNALLY or INTERNALLY. So by removing the getters any client who creates the object of this class cannot modify the data once loaded.
 


• Step 2:- Provide parameters via constructor.
We have removed the getters so there is no way to load data in to the class. So to provide data we need to create constructors with parameters via which we can pass data to the object. Below is the modified code for the same.
 



• Step 3 :- Make the variables of the property READONLY

As we said in the previous definition immutableobjects cannot be modified by ANYWAY once the data is loaded. I will go one step further and say it cannot be modified EXTERNALLY by client or INTERNALLY by the class code himself.
 

But if you see our currency class the variables can be modified after the object creation. In other words see the below class code. We have created a method in the class called as “SomeLogic”. This method is able to successfully modify the variables after the object has been created.

So the solution for the above to make the variables “READONLY”. Below is the currency class modified where the property variables are made readonly.Readonly variables can be only initialized in the constructor and later they cannot be modified.

Hope you enjoyed this short note on Immutable objects. Immutable objects is a design pattern which was first officially discussed in Java concurrency in practice book. In case you are interested to learn design pattern I would suggest creating a full blown project and starting implementing design pattern in the same.
 

You can start from the below youtube video in which I have demonstrated design pattern with a full blown project.