Click here to Skip to main content
6,822,613 members and growing! (17,862 online)
Email Password   helpLost your password?
    Beginner License: The Code Project Open License (CPOL)

Adapter Design Pattern How to use in Asp.net using C#

By Salmanzz

codeproject What is 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 a interface which client understands. When to use it? Its quite use
C# (C#1.0, C#2.0, C#3.0), .NET (.NET2.0, .NET3.0, .NET3.5, .NET4.0), ASP.NET, Dev, Design
Revision:6 (See All)
Posted:2 Jul 2009
Updated:14 Aug 2009
Views:6,338
Bookmarked:14 times
Technical Blog
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this technical blog.
Popularity: 2.27 Rating: 2.69 out of 5
2 votes, 28.6%
1
1 vote, 14.3%
2
3 votes, 42.9%
3

4
1 vote, 14.3%
5

What is 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 a interface which client understands. 

When to use it?

  • Its quite useful when dealing with the legacy code specially 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's

Lots of us uses the enterprise library data access application block. Suppose later if we find that some other third party data access  application block is better than micorsoft data access application block  then in 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 Adapteee 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 in 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 effecting your project . In 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


Member

Occupation: Software Developer (Senior)
Company: BMJ
Location: United Kingdom United Kingdom

Other popular Design and Architecture articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
General[My vote of 1] What can I say? Pinmembershijun_china23:04 6 Aug '09  
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    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 14 Aug 2009
Editor:
Copyright 2009 by Salmanzz
Everything else Copyright © CodeProject, 1999-2010
Web22 | Advertise on the Code Project