Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parent reference in contained class

Status
Not open for further replies.

misterstick

Programmer
Apr 7, 2000
633
GB
i have a wrapper which provides access to a web service.

i'd like to provide a lookup facility on the wrapper so that the developer can write code like this:

Code:
string code = arbitraryValue;
Wrapper wrapper = new Wrapper();
if ( wrapper.Code.Exists(code) )
{
 txtField1.Text = wrapper.Code.Description;
 txtField2.Text = wrapper.Code.OtherData;
}

my initial thought was to have the lookup code in a class wholly contained by the wrapper.

Code:
public class Wrapper
{
 public class CodeLookup
 {
  public bool Exists(string code)
  {
   CodeTable record = [red]wrapper[/red].LookupByKey(code);
   if ( record == null ) return false;
   return true;
  }
  //...
  private CodeLookup lookup = new CodeLookup();
  public CodeLookup Code { get { return lookup; } }
 } 
}

the actual database call to do the lookup should be via a wrapper object. i could create a new one, but since the lookup is wholly contained by a wrapper object this seems unnecessary.

is there a way i can get at the containing instance of the parent class?

do i have to cache a pointer to it in the contained class?

if it's IDisposable, would i have to make the contained class IDisposable too and dispose of the pointer?

forgive my overdensity,
many thanks,


mr s. <;)

 
why not keep the instance of the webservice class static? This way you can set it once and use it from anywhere.

if you want your wrapper class to do all the work for EVERY request and you're ok with only ever using one instance of the class in your entire application you will want to implement the singleton pattern.
 
many thanks, a different perspective is much appreciated. one problem with being a geek is that we don't provide problems, just broken solutions.

i'll look into whether the wrapper class has private members (i suspect it doesn't) and see if i can do some research on implementing singleton nicely and neatly.

thanks again,


mr s. <;)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top