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, 5:19 AM EST
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, 5:17 AM EST
Good Article Akshay!
Chandradev PrasadPosted Apr 18, 2016, 3:08 AM EDT
Good one.
Kuppurasu NagarajTOP 100Posted Apr 17, 2016, 12:13 AM EDT
Nice Sharing..
Kumaresh RajalingamPosted Apr 16, 2016, 4:34 AM EDT
Good one
Debasis SahaTOP 100Posted Apr 15, 2016, 10:37 PM EDT
Nice One..
Vignesh ManiTOP 100Posted Apr 15, 2016, 12:28 PM EDT
Nice one
Rahul Kumar SaxenaTOP 100Posted Apr 15, 2016, 10:10 AM EDT
Good one
Sourabh SomaniTOP 100Posted Apr 15, 2016, 9:31 AM EDT
Nice one sir :)
Gowtham RajamanickamTOP 100Posted Apr 15, 2016, 9:20 AM EDT
Good article...
Kuppurasu NagarajTOP 100Posted Apr 15, 2016, 9:09 AM EDT
Nice Sharing..
Ranjan DailataPosted Apr 15, 2016, 8:56 AM EDT
Popularly know as Method Chaining DSL pattern.