Adapter pattern C# real world example

What is Design Pattern?

A design pattern is a general recurring solution to a commonly occurring problem in software design.

In this article, I will explain to you what is the adapter design pattern and when to use it in a real-world example. Before discussing the problem, let’s create a simple application in. NET.

The Adapter pattern lets you adapt one interface to another

Let’s consider the following code.

void Main()

	var result = new ThirdParyAPI().GetCustomer();


public class Customer
	public string Name { get; set; }
	public string City { get; set; }
	public string Address { get; set; }
public static class CustomerDataProvider
	public static List<Customer> GetData() =>
	   new List<Customer>
			new Customer { City = "Italy", Name = "Alfa Romeo", Address = "Address1" },
			new Customer { City = "UK", Name = "Aston Martin", Address = "Address1"  },
			new Customer { City = "USA", Name = "Dodge", Address = "Address1"  },
			new Customer { City = "Japan", Name = "Subaru", Address = "Address1"  },
			new Customer { City = "Germany", Name = "BMW", Address = "Address1"  }

public class ThirdParyAPI
	public string GetCustomer()
		return JsonConvert.SerializeObject(CustomerDataProvider.GetData());

You are consuming one third party API in your application. Currently, the API is sending the response in JSON format now the company decided to change the response from JSON to XML.


public class ThirdParyAPI
	public XDocument GetXML()
		var xDocument = new XDocument();
		var xElement = new XElement("Customers");
		var xAttributes = CustomerDataProvider.GetData()
			.Select(m => new XElement("Customer",
								new XAttribute("City", m.City),
								new XAttribute("Name", m.Name),
								new XAttribute("Address", m.Address)));

		return xDocument;

W3sDesign Adapter Design Pattern UML.jpg
By Vanderjoe - Own work, CC BY-SA 4.0, Link

There are two ways to handle the above problem

  1. Make changes in your exiting application
  2. Add an Adapter between your application and third party API so that there are no changes required in your app. As shown in the below figure.

Adapter Pattern

As per the Wikipedia

The adapter pattern is a software design pattern (also known as Wrapper, an alternative naming shared with the decorator pattern) that allows the interface of an existing class to be used as another interface


To handle the above scenario, we can use the Adapter design pattern.

Let’s code the solution


create an interface named IConvertor (you can be named according to your application)

public interface IConvertor
	string ConvertXmlToJson();


Create a class named XmlToJsonAdapter and Inject the dependency

public class XmlToJsonAdapter : IConvertor
	private readonly ThirdParyAPI _xmlConverter;

	public XmlToJsonAdapter(ThirdParyAPI xmlConverter)
		_xmlConverter = xmlConverter;

	public string ConvertXmlToJson()
		string jsonText = JsonConvert.SerializeXNode(_xmlConverter.GetXML());
		return jsonText;

Now change your application as shown below

void Main()
	// var result=new ThirdParyAPI().GetJSON();
	// Console.WriteLine(result.ToString());

	var adapterResult = new XmlToJsonAdapter(new ThirdParyAPI()).ConvertXmlToJson();


When to Use Adapter

We should use the Adapter class whenever we want to work with the existing class, but its interface is not compatible with the rest of our code. The Adapter pattern is a middle-layer that serves as a translator between the system implemented in our project and some third party class or any other class with a different interface.

Design Patterns You’re Already Using in the .NET Framework

Runtime Callable Wrapper (RCW)

You may like

Next Post Previous Post
No Comment
Add Comment
comment url