Skip to main content

How to implement Adapter pattern in asp.net



In this post,I will show you how to implement Adapter patter 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?
1.     You want to use existing class,and its interface doe not match the one you need.

2.     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.




1.     Here ICacheManager (Target) the interface that client wants to use
2.     HttpContext (Adaptee) An implementation that needs adapting
3.     CacheManager (Adapter) The class that implements the Target interface in terms of the Adaptee




Implementation

  • Let's start implementing the adapter patternRight click on the project and add a new class named ICacheManager and add 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 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 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");
    }
}
}

    Comments

    Popular posts from this blog