💡 Introduction
In today’s fast-moving software world, businesses often face the challenge of integrating legacy systems with modern applications. A perfect example is when a new billing system works with JSON, but the old billing system still processes only XML.

Instead of rewriting or discarding the old code, we can solve this compatibility issue using the Adapter Design Pattern in C#. This article walks you through a real-world billing scenario and shows how to use the Adapter pattern to bridge JSON and XML seamlessly.
❌ The Problem: JSON vs. XML Incompatibility
Imagine you’re developing a modern billing application that expects data in JSON format. Unfortunately, the existing OldBillingSystem can only handle XML.
The New Billing System (Target Interface)
public interface INewBillingSystem
{
void ProcessBill(string jsonData);
}
The Old Billing System (Legacy Code You Can’t Change)
public class OldBillingSystem
{
public void SendBillXml(string xmlData)
{
Console.WriteLine("Old system processing XML: " + xmlData);
}
}
➡️ Here’s the challenge: the new system uses JSON, while the old system only works with XML. Without a bridge, these systems cannot talk to each other.
✅ The Solution: Adapter Design Pattern
The Adapter Pattern allows incompatible systems to work together. In this case, the Adapter will convert JSON input into XML so the old system can still be reused.
🔧 Step-by-Step Implementation
Step 1: Target Interface (New System)
public interface INewBillingSystem
{
void ProcessBill(string jsonData);
}
Step 2: Adaptee (Old Billing System)
public class OldBillingSystem
{
public void SendBillXml(string xmlData)
{
Console.WriteLine("Old system processing XML: " + xmlData);
}
}
Step 3: Adapter Class
using System.Text.Json;
using System.Xml.Linq;
public class BillingAdapter : INewBillingSystem
{
private readonly OldBillingSystem _oldBillingSystem;
public BillingAdapter()
{
_oldBillingSystem = new OldBillingSystem();
}
public void ProcessBill(string jsonData)
{
// Convert JSON to XML
var doc = JsonDocument.Parse(jsonData);
XElement xml = new XElement("Bill",
new XElement("Customer", doc.RootElement.GetProperty("Customer").GetString()),
new XElement("Amount", doc.RootElement.GetProperty("Amount").GetDecimal())
);
string xmlString = xml.ToString();
// Call old system
_oldBillingSystem.SendBillXml(xmlString);
}
}
Step 4: Client Code (Using the Adapter)
class Program
{
static void Main(string[] args)
{
INewBillingSystem billingSystem = new BillingAdapter();
string json = @"{ ""Customer"": ""John Doe"", ""Amount"": 250.75 }";
billingSystem.ProcessBill(json);
}
}
🖥️ Expected Output
Old system processing XML:
<Bill>
<Customer>John Doe</Customer>
<Amount>250.75</Amount>
</Bill>
🎯 Role of Each Component
| Role | Component |
|---|---|
| Target | INewBillingSystem |
| Adaptee | OldBillingSystem |
| Adapter | BillingAdapter |
| Client | Program.Main() |
🚀 Why Use Adapter Pattern Here?
- ✅ Reuses legacy code without modifications
- ✅ Bridges the gap between JSON (new) and XML (old)
- ✅ Keeps modern system clean by depending only on the new interface
- ✅ Promotes maintainability and flexibility
🧠 Final Thoughts
The Adapter Design Pattern in C# is extremely useful when:
- You can’t modify source code (like third-party or legacy systems).
- Two systems use incompatible interfaces (e.g., JSON vs. XML).
- You want to extend the life of old systems without rewriting everything.
By applying the Adapter Pattern, you can modernize your applications, integrate legacy services, and ensure smooth communication between old and new systems.
❓ Frequently Asked Questions (FAQs)
What is the Adapter Design Pattern in C#?
The Adapter Design Pattern is a structural design pattern that allows two incompatible interfaces to work together. In C#, it acts as a bridge between a new system and a legacy system by converting one interface into another.
When should I use the Adapter Pattern?
You should use the Adapter Pattern when:
You want to integrate legacy code into a modern system.
Two systems use incompatible data formats (e.g., JSON vs. XML).
You cannot modify the original source code (like third-party libraries).
What is the difference between Adapter and Facade Patterns?
Adapter Pattern: Converts an existing interface into a compatible one.
Facade Pattern: Provides a simplified interface to a complex subsystem without changing its functionality.
How does Adapter Pattern help in real-world projects?
The Adapter Pattern helps by:
Reusing old or third-party code without rewriting it.
Ensuring compatibility between different formats or APIs.
Saving time and cost by avoiding complete system rewrites.
Can the Adapter Pattern convert JSON to XML in C#?
Yes ✅. In C#, you can use the Adapter Pattern to parse JSON input and transform it into XML format before passing it to legacy systems. This is a common real-world use case for billing, reporting, and data migration systems.
Is Adapter Pattern part of Structural Design Patterns?
Yes. The Adapter Pattern belongs to the Structural Design Patterns category in the Gang of Four (GoF) Design Patterns, since it focuses on the structure and interaction between classes.