Real World Scenario
Consider a scenario where you have to create an application, having recharge and utility bill payment functionality. 
In order to create an application, you have to create Recharge class which accepts a series of values like Service, Operator, Circle, Mobile and Amount and then called the DoRecharge method. But there is a problem associated with this approach. One doesn’t know what to do with the Recharge object once it has finished doing the recharge. Can it be reused to do another recharge or should it be held to know the status of recharge transaction?
- namespace TrainWreckPattern
- {
- public class Recharge
- {
- public string Service
- {
- get;
- set;
- }
- public string Operator {
- get;
- set;
- }
- public string Circle {
- get;
- set;
- }
- public string Mobile {
- get;
- set;
- }
- public double Amount {
- get;
- set;
- }
- public void DoRecharge() {
- // Do Recharge
- }
- }
- }
- namespace TrainWreckPattern
- {
- class Program
- {
- static void Main(string[] args)
- {
- Recharge objRecharge = newRecharge();
- objRecharge.Service = "Topup";
- objRecharge.Operator = "Vodafone";
- objRecharge.Circle = "Gujarat";
- objRecharge.Mobile = "9769496026";
- objRecharge.Amount = 100;
- objRecharge.DoRecharge();
- }
- }
- }
Here, I have made couple of changes in the code. I have removed all properties, created a couple of methods like Service, Operator etc. and each one returns instance of class itself except DoRecharge() method.
- namespace TrainWreckPattern
- {
- public class Recharge
- {
- public Recharge Service(string service)
- {
- return this;
- }
- public Recharge Operator(string operators) {
- return this;
- }
- public Recharge Circle(string circle) {
- return this;
- }
- public Recharge Mobile(string mobile) {
- returnthis;
- }
- public Recharge Amount(double amount) {
- returnthis;
- }
- public void DoRecharge() {
- // Do Recharge
- }
- }
- }
- namespace TrainWreckPattern
- {
- clas sProgram
- {
- static void Main(string[] args)
- {
- new Recharge().Service("Topup").Operator("Vodafone").Circle("Gujarat").Mobile("9769496026").Amount(100).DoRecharge();
- }
- }
- }
So, using such pattern we can remove verbosity from code. Now, after looking at the above code, some of you might be thinking that we have been writing such code a million times particularly when we are writing LINQ query.
- string[] operators =
- {
- "Airtel",
- "Idea",
- "Vodafone",
- "Uninor"
- };
- IEnumerable < string > query = from x in operators
- where x.Contains("a") // Filter Elements
- orderby x.Length // Sort Elements
- select x.ToUpper(); // Project Each Element
We can also achieve the same thing using lambda:
- public static void DoRecharge(Action < Recharge > action)
- {
- Recharge recharge = newRecharge();
- action(recharge);
- // Do Recharge
- }
- Recharge.DoRecharge((recharge) =>
- {
- recharge.Service("Topup").Operator("Vodafone").Circle("Gujarat").Mobile("9769496026").Amount(100);
- });

Hemant MahajanPosted Feb 28, 2020, 10:19 AM
Few queries and help me please: 1) How do we ensure if a function is already called in chain we should not be able to call it again. eg recharge.Service("Topup").Service("Topup") should throw design time error. Is it possible?
Hemant MahajanPosted Feb 28, 2020, 10:17 AM
Good Article Akshay!
Chandradev PrasadPosted Apr 18, 2016, 7:08 AM
Good one.
Kuppurasu NagarajPosted Apr 17, 2016, 4:13 AM
Nice Sharing..
Kumaresh RajalingamPosted Apr 16, 2016, 8:34 AM
Good one
Debasis SahaPosted Apr 16, 2016, 2:37 AM
Nice One..
Vignesh ManiPosted Apr 15, 2016, 4:28 PM
Nice one
Rahul Kumar SaxenaPosted Apr 15, 2016, 2:10 PM
Good one
Sourabh SomaniPosted Apr 15, 2016, 1:31 PM
Nice one sir :)
Gowtham RajamanickamPosted Apr 15, 2016, 1:20 PM
Good article...
Kuppurasu NagarajPosted Apr 15, 2016, 1:09 PM
Nice Sharing..
Ranjan DailataPosted Apr 15, 2016, 12:56 PM
Popularly know as Method Chaining DSL pattern.