Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2008 > C# 2008 aka C# 3.0
|
C# 2008 aka C# 3.0 Discuss the Visual C# 2008 (aka C# 3.0) language
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2008 aka C# 3.0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 27th, 2008, 10:06 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default Question about a piece of code

Hi

I am have a question about this piece of code


Code:
        private ProductsTableAdapter _productsAdapter = null; 
        protected ProductsTableAdapter Adapter 
        { 
            get 
            { 
                if (_productsAdapter == null) 
                    _productsAdapter = new ProductsTableAdapter(); 
                return _productsAdapter; 
            } 
        }


First is this a property? If it is I am not use to seeing it where it is private and then protected and if so why is it done like this and not the private/public way?

Also I am wondering what is the if statement for? Like could you not have just done this instead?


Code:
   private ProductsTableAdapter _productsAdapter = new ProductsTableAdaptor();
        protected ProductsTableAdapter Adapter 
        { 
            get 
            { 
                return _productsAdapter; 
            } 
        }
Like why do you need to make a the new object in their and then check every single time if the object is null or not?

http://www.asp.net/learn/data-access...ial-02-cs.aspx

Thanks





 
Old November 27th, 2008, 11:06 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Its called "lazy loading".

Adapter is a read-only property (no "set" accessor) that returns a reference to a ProductsTableAdapter held in a private field named _productsAdapter.

You would typically see this type of property in a base class that other classes subclass. The base class holds a reference to the ProductsTableAdapter. But perhaps not all classes that inherit the base class need a ProductsTableAdapter, or maybe they don't need one immediately (i.e., when they are constructed). So the ProductsTableAdapter is "lazy loaded", created only on first use. That way, if a derived object needs a ProductsTableAdapter, it can call its Adapter property when it needs to; if it doesn't need one, or doesn't need one when its instantiated, it doesn't have to incur the overhead of creating one when its constructed.

The protected access modifier just ensures that the property can be accessed only through the derived classes.

HTH,

Bob

 
Old November 27th, 2008, 11:50 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

Hmm

I never really done anything using base class and very little with inheritance.

Would my way work that I had stated and maybe instead of protected have just public?

 
Old November 28th, 2008, 06:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

 
Quote:
quote:Would my way work that I had stated and maybe instead of protected have just public?


If it compiles, it works. There are alwyas 20 ways to do eveything. In the context of your tutorial, declaring the Adapter property as private, public, protected or internal will all compile. You're on step 2 of building a simple business logic layer for a 75 project tutorial. I worked through Scott Mitchell's Data Access Tutorials mysel a couple of years ago. Came away from the experience realizing that I really don't like the whole TableAdapter/Strongly-Typed DataSet thing. Like custom buisness objects and DataReaders much better. Don't know that that means that they are better. Probably just means that there a 20 ways of doing eveything.

Obviously the ProductsBLL class isn't serving as a base class, but its important to know what protected scope means and how protected members must be accessed. And lazy loading does become important when working with objects that have scores of properties, only some of which require, say, validation objects or restricted role access objects. Its just another technique to know about.

With your tutorial, just try eveything every way you can think of, and enjoy the learining curve.

HTH,

Bob

 
Old November 29th, 2008, 12:20 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

To be perfectly honest with you I can't think of a reason why your property wouldn't be just as happy if it were declared private. It only needs to be visible to the methods in the BLL class itself. No idea why its protected.

And ignore the "scores of properies" bit. Just meant a lot. If you find yourself writing more than a dozen or so properties on a class, its probably time to start thinking about another development iteration.

 
Old November 29th, 2008, 05:10 PM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

How about that if statement is it really necessary?

 
Old November 30th, 2008, 01:28 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

The 'if' conditional statement checks whether a ProductsTableAdapter already exists or not. If it does you don't want to incur the overhead of again allocating memory and creating another one, and another one, and another one for _productsAdapter to reference. You only want to set the _productsAdapter field's reference the first and only time the Adapter property is called.

Another example (I stumbled across today) would be the following. This is how a typed dataset tableadapter initializes its adapter object.

Code:
private SqlDataAdapter _adapter;
private SqlDataAdapter Adapter 
{
    get 
    {
        if ((this._adapter == null)) 
        {
            this.InitAdapter();
        }
        return this._adapter;
    }
}
The InitAdapter() method that gets called in the 'get' accessor is 50 lines of code that creates DataAdapter and DataTableMapping objects, adds column mappings for every column in the table, configures delete, insert, and update commands, and initializes a bunch of command parameters. You don't want to run all that initialization code every time you need a reference to the DataAdapter. So the 'if' condition ensures that the DataAdapter is initilized ONCE, on first use. After that, the 'if' condition evaluates to 'false', the initialization routine is bypassed, and you get a reference to an already initialized DataAdapter.

Bob

 
Old November 30th, 2008, 02:18 AM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default

if you made it like this though


private ProductsTableAdapter _productsAdapter = new ProductsTableAdapter();

 public ProductsTableAdapter Adapter { get {return _productsAdapter;}}

Wouldn't this make only one object? Like when the person would call that class up they would make a new object of that class and it would then go and make that object. So it would only be made once(unless of course you make another object of that class).

 
Old November 30th, 2008, 11:53 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

Yes, you could write it like that. However, your app is working with Web Forms data binding components. On of the wierd behaviors of both Web and Windows Forms data binding is that the data binding components invoke the same property many, some times many, many, times, even though only the first call actually needs to be honored. No one understands it. Its just a fact of life.

Set a break point on your Adapter property and run one of the reporting samples. You'll see the Adapter property gets called several times when you load a new page. The 'if' condition just prevents a new tableadapter object from being constructed every time it gets called.

Bob






Similar Threads
Thread Thread Starter Forum Replies Last Post
understand a piece of code in assembler rbulus BOOK: Professional Assembly Language 1 January 10th, 2010 07:45 AM
How to delete a piece of code Charlie05 C# 2005 2 August 3rd, 2008 11:32 AM
Need help with a small piece of code (New to VB) kwik10z Excel VBA 2 October 12th, 2007 06:46 AM
This piece of code is giving an error sourik C# 2 July 15th, 2006 09:18 AM
What does this piece of code do? mito_lal Oracle 1 July 11th, 2006 06:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.