In this post, I will show you how to implement the Adapter pattern in asp.net.Before going to implementation details, let's dive into adapter pattern definitions.
What is an Adapter Pattern?
The adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern that translates one interface for a class into a compatible interface
*How’s it implemented?
- You want to use an existing class, and its interface doe not match the one you need.
- You want to create a reusable class that cooperates with unrelated classes with incompatible interface********
Problem:-
Many of us use the .net Cache class for storing objects into memory. Suppose later if we find that some other third party Cache management library that is better than existing Cache class , then the project would not change just adapter internally would call the new Cache class.
-
Here
ICacheManager
(Target) the interface that the client wants to use= -
HttpContext (Adaptee)
An implementation that needs adapting. -
CacheManager (Adapter)
The class that implements the Target interface in terms of the Adaptee.
Implementation
Let's start implementing the adapter pattern right click on the project and add a new class named ICacheManager, and add the following code inside it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public interface ICacheManager
{
void Add(string key, object data);
void Remove(string key);
T Get<T>(string key);
}
Right-click on the project and add a new class, CacheManager and add the following code inside it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class CacheManager : ICacheManager
{
public void Add(string key, object data)
{
//Implements third party caching here
HttpContext.Current.Cache.Add(key, data);
}
public void Remove(string key)
{
if (HttpContext.Current.Cache["key"] != null)
HttpContext.Current.Cache.Remove(key);
}
public T Get<T>(string key)
{
T itemStored = (T)HttpContext.Current.Cache.Get(key);
if (itemStored == null)
itemStored = default(T);
return itemStored;
}
- Add a new page and add the following code inside it
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Adapter.aspx.cs" Inherits="Adapter" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text="Add" />
<asp:Button ID="btnRemove" runat="server" OnClick="btnRemove_Click" Text="Remove" />
<asp:Button ID="btnGet" runat="server" OnClick="btnGet_Click" Text="Get Value from Cache" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Adapter : System.Web.UI.Page
{
CacheManager manager = new CacheManager();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
manager.Add("Key", DateTime.Now);
}
protected void btnGet_Click(object sender, EventArgs e)
{
Response.Write(manager.Get<DateTime>("Key").ToShortDateString());
}
protected void btnRemove_Click(object sender, EventArgs e)
{
manager.Remove("Key");
}
}
}