Click here to Skip to main content
Licence CPOL
First Posted 2 Jul 2009
Views 17,927
Bookmarked 21 times

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

By Salmanzz | 14 Aug 2009 | Technical Blog
How to use the Adapter Design Pattern in ASP.NET using C#
2 votes, 22.2%
1
1 vote, 11.1%
2
4 votes, 44.4%
3

4
2 votes, 22.2%
5
2.83/5 - 9 votes
μ 2.83, σa 2.54 [?]

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

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembershaikshavali6:02 17 Aug '10  
GeneralMy vote of 2 PinmemberVMykyt6:38 16 Jul '09  
GeneralRe: My vote of 2 PinmemberSalmanzz6:43 16 Jul '09  
GeneralMy vote of 1 PinmemberGiri Ganji21:01 6 Jul '09  
GeneralRe: My vote of 1 PinmemberSalmanzz5:00 9 Jul '09  

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.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120209.1 | Last Updated 14 Aug 2009
Article Copyright 2009 by Salmanzz
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid