Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-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 January 11th, 2007, 01:52 PM
Authorized User
 
Join Date: Sep 2006
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default from where is Instance actually instantiated?

Dear Developers,

I can't quite fully understand the details of the Instance machanism in this framework, could someone please explain how it works? For example, lets take Articles:


The text says that
   "Once the provider is created for the first time, it is saved in a static private property and won't be recreated again until the web application is shut down and restarted (for example, when IIS is stopped and restarted, or when the web.config file is changed"



There are calls such as this:

---

List<ArticleDetails> recordset = SiteProvider.Articles.GetArticles(
               categoryID, GetPageIndex(startRowIndex, maximumRows), maximumRows);

---

In my understanding this call references the static private property and calls its GetArticles() method, right???


But where is the statement that calls the following code in order to create the instance the first time the application is started?

      static private ArticlesProvider _instance = null;
      /// <summary>
      /// Returns an instance of the provider type specified in the config file
      /// </summary>
      static public ArticlesProvider Instance
      {
         get
         {
            if (_instance == null)
               _instance = (ArticlesProvider)Activator.CreateInstance(
                  Type.GetType(Globals.Settings.Articles.ProviderTyp e));
            return _instance;
         }
      }
   }
}

Thanks!
__________________
Alex

- TheBeerHouse Mods Repository
http://www.sashka.com/TheBeerHouse/thebeerhouse.html
 
Old January 13th, 2007, 03:24 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A static property doesn't need an instance for the caller to use it. A caller just specifies the classname followed by ".Instance" and that will perform the actions in the getter. That checks to see if there's already an instance constructed by examining the private static member named _instance to see if it's null. If it's not null, then he knows there's already an instance created so he passes that back. If it is null that means he needs to create an instance, and then he passes that back (and remembers it in _instance).

This is similar to the "lazy loading" pattern to cache data for this site. The idea is that you don't create an instance until someone needs one, and after that you keep handing out a pointer to the same instance. The caller won't know if he's the first to use an instance or not.

You have to be very careful with private instance fields in this case. You can't store user specific data in private fields unless you first get a lock. Locking would not be desireable in this kind of code, and you'd normally put user-specific data elsewhere, like in session state if you need to persist it.

You also have to be careful about garbage collection in this model. Once he creates the instance and sticks it in the private static field, that prevents it from being garbage collected for the life of the app domain. That is normally the behavior you want when you use this model, but you must be careful about that data that instance might be accumulating since it won't be cleaned up.

This is a good choice for a class containing only methods, which could also be implemented as a fixed static class.

You might be wondoring why he went to so much trouble, when a simple static class (or even an instance class) might have worked fine. The reason is that he wants to be able to instantiate a different flavor of class depending on the provider specified in configuration. Marco designed this site to support different back-ends. He only implemented a back-end for SQL Server due to time and space constraints in the book, but the infrastructure is there for people who wish to expand on it.

Eric

 
Old January 15th, 2007, 12:03 PM
Authorized User
 
Join Date: Sep 2006
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, I think I got it now - when the call is made such as:
   commentCount = SiteProvider.Articles.GetCommentCount();

a call is implicitly made to .Articles first before calling the GetCommentCount() method ?


 
Old January 16th, 2007, 12:16 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 917
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No, you need to look at how SiteProvider was created before that line of code. You can just search in all the files for ".Instance" (case sensitive) to see where these calls are being made. I don't have the code with me now, but it should be easy to find with a search.

Eric

 
Old January 16th, 2007, 02:18 AM
Authorized User
 
Join Date: Sep 2006
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This is a snippet of code in SiteProvider:

namespace MB.TheBeerHouse.DAL
{
   public static class SiteProvider
   {
      public static ArticlesProvider Articles
      {
         get { return ArticlesProvider.Instance; }
      }


but I couldn't find any code that calls .Articles, or that creates the SiteProvider...

 
Old March 1st, 2007, 03:52 PM
Authorized User
 
Join Date: Sep 2006
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I still haven't had any luck finding the code that makes the call to "public static ArticlesProvider Articles" in SiteProvider.cs, which in turn calls "get { return ArticlesProvider.Instance; }"

Can somebody please post the module and line number which makes this call?






Similar Threads
Thread Thread Starter Forum Replies Last Post
Why abstract class can't be instantiated? anisurr C++ Programming 5 July 23rd, 2008 01:32 AM
WinForm Resizing instantiated form inside Tab Page maulik33 C# 4 June 13th, 2007 01:48 PM
Class Can't be instantiated enderjs J2EE 0 September 29th, 2004 10:01 AM
Class can't be instantiated? enderjs XML 1 September 29th, 2004 02:05 AM
COM out of process server (EXE) instantiated twice sebdejonghe C++ Programming 0 March 31st, 2004 09:34 AM





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