Click here to Skip to main content
Click here to Skip to main content

Adapter Design Pattern - How to use in ASP.NET using C#

, 13 Aug 2009
Rate this:
Please Sign up or sign in to vote.
How to use the Adapter Design Pattern in ASP.NET using C#

What is the Adapter Pattern?

The Adapter pattern is a structural design pattern which enables a system to use classes whose interfaces don’t quite match its requirements or in other words is used to make an interface which the client understands.

When to Use It?

  • It's quite useful when dealing with the legacy code especially that was written a while ago and to which one might not have access.
  • It is especially useful for off-the-shelf code, for toolkits, for libraries or any third party software.

Many of us use the enterprise library data access application block. Suppose later if we find that some other third party data access application block is better than Microsoft data access application block, then the project would not change just adapter internally would call the new data access application block.

Object Adapter Pattern

In Object adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.

// Existing class.. this may be your legacy code or third party code
class Adaptee
{
    // validates the email
    public bool IsEmail(string email)
    {
        return System.Text.RegularExpressions.Regex.IsMatch
		(email, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
    }
}

// Required standard implementing through interface
interface ITarget
{
    // Rough estimate required
    bool ValidateEmail(string email);
}
// Implementing the required standard via Adaptee
class Adapter : Adaptee, ITarget
{
    public bool ValidateEmail(string email)
    {
        return IsEmail(email);
    }
}

class Client
{
    static void Main()
    {
        // Showing the Adaptee in standalone mode
        Adaptee first = new Adaptee();
        Console.Write("Before the new standard: ");
        Console.WriteLine(first.IsEmail("anyemail@gmail.com"));

        // What the client really wants
        ITarget second = new Adapter();
        Console.WriteLine("\nMoving to the new standard");
        Console.WriteLine(second.ValidateEmail("anyemail@gmail.com"));
    }
}	
  • ITarget: The interface that the Client wants to use
  • Adaptee: An implementation that needs adapting
  • Adapter: The class that implements the ITarget interface in terms of the Adaptee
  • ValidateEmail: An operation that the Client wants
  • IsEmail: The implementation of Request’s functionality in the Adaptee

Note

The adapter pattern is also useful where an already existing class provides some or all of the services you need but does not use the interface you need. Also using this way, you can change third-party library easily without affecting your project. In the above example, if you want to change the validation method of email, all you need to do is to use ValidateEmail function for any new method or third party library functions.

View this article on my blog.

Your feedback is welcome.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Salmanzz
Software Developer (Senior) BMJ
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pinmembershaikshavali17-Aug-10 5:02 
GeneralMy vote of 2 PinmemberVMykyt16-Jul-09 5:38 
GeneralRe: My vote of 2 PinmemberSalmanzz16-Jul-09 5:43 
GeneralMy vote of 1 PinmemberGiri Ganji6-Jul-09 20:01 
GeneralRe: My vote of 1 PinmemberSalmanzz9-Jul-09 4:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 14 Aug 2009
Article Copyright 2009 by Salmanzz
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid