Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
DevExpress UI Controls
Search :       Advanced Search »
Home » Design & Architecture » Wrapper Patterns in C#: Part I

Wrapper Patterns in C#: Part I

Did you ever wish for a superhuman power to be impervious to bullets or travel outside your body? How about the superpower to be able to breathe underwater or fly? Or how about a changing the way you look so you can disguise yourself as anyone, or anything? In this series of four articles, we will travel down the C# wrapper rabbit hole and see how it is all possible with some patterns: Proxy, Decorator, and Adapter.

Author Rank :
Page Views : 45830
Downloads : 381
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
BasicWrapper.zip
 
 
Team Foundation Server Hosting
Become a Sponsor
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Did you ever wish for a superhuman power to be impervious to bullets or travel outside your body?  How about the superpower to be able to breathe underwater?  Or how about a changing the way you look so you can disguise yourself as anyone, or anything?  In this series of four articles, we will travel down the C# wrapper rabbit hole and see how it is all possible with some patterns: Proxy, Decorator, and Adapter. 

Creating a Basic Wrapper.

These three patterns, proxy, decorator, and adapter are all implemented identically. They are all wrappers at the basic level. The functionality they provide is how we can make the distinction between them. Let's first look at a template for a basic wrapper and then at how to use a wrapper to implement each of these patterns can help us in our quest for the bizarre.

Before we begin to understand how to perform super-feats, we need to understand the basic super-power all C# developers are born with - our secret ability to wrap (not to be confused with rap). Here is how a wrapper works:

Let's say we have a Thing with a ThingString property and a method PrintThingString() as below:

class Thing

{

          private string m_string;

          public string ThingString

          {

                    get { return m_string; }

                    set { m_string = value; }

          }

          public void PrintThingString()

          {

                    Console.WriteLine(m_string);

          }

}

 

Here is how we create a basic ThingWrapper to wrap the Thing. It contains a reference to the Thing being wrapped and has the same signature. The ThingWrapper just passes requests into the wrapped Thing.

class ThingWrapper

{      

          private Thing m_thing;

          public ThingWrapper(Thing pThing)

          {

                    m_thing = pThing;

          }

          public string ThingString

          {

                    get { return m_thing.ThingString; }

                    set { m_thing.ThingString = value; }

          }

          public void PrintThingString()

          {

                    m_thing.PrintThingString();

          }

}

 

Because the ThingWrapper has the same signature as the Thing, it is really no different dealing with the Thing or the ThingWrapper. We can work with either the Thing or the ThingWrapper access our internal string m_string, or perform the base functionality which is to print a line to the console.

Thing t = new Thing();

t.ThingString = "This is the ThingString";

t.PrintThingString();

 

ThingWrapper tWrap = new ThingWrapper(t);

tWrap.PrintThingString();

tWrap.ThingString = "This is still the ThingString";
tWrap.PrintThingString();

We can wrap any class this way. Now that we understand this "wrapping power", we should only use it for good (or to do some cool stuff). Just remember, things can get pretty complicated if we have wrappers that are really not needed. Sometimes, however, we just can't live without them. Starting in the next article, let's next look at some of the super powers we can manifest using wrappers.

Until then

-Only use your powers for good.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Matthew Cochran
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
DevExpress Free UI Controls
Become a Sponsor
 Comments
update zip file by wendy On February 22, 2010

No Main method in your attached zip file. it's better change your run method name to Main method name. and add one more line
  Console.Read();

Reply | Email | Modify 
Link to Next Article by Premanshu On June 10, 2011
hi there, this is indeed a good article and I love the way you make it so simple, with each line explained. I wonder why there's no link to the next article, in the series.
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.