Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 3rd, 2007, 03:37 PM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 5 Aricles/SiteProvider question HELP PLS

Hi all,

On page 223 we have this property below, this property is part of the ArticlesProvider class.

static public ArticlesProvider Instance
{
     get
     {
     if (_instance == null)
         instance = (ArticlesProvider)Activator.CreateInstance(Type.Ge tType(Globals.Settings.Articles.ProviderType));
            return _instance;
     }
}

Now I know this returns an instance of our provider type, which is the SqlArticlesProvider class, but a cast is done here converting it into its parent class; the ArticlesProvider class. Ok, all good so far. Now on page 228 we have a property within the SiteProvider class that simply uses the property above to return an ArticlesProvider instance, like so:

public static ArticlesProvider Articles
{
    get { return ArticlesProvider.Instance; }
}

All good so far. Now on page 231 we have the body property like so:

get
{
    if (_body == null)
        _body = SiteProvider.Articles.GetArticleBody(this.ID);
        return _body;
}

Now I can see that this uses the Articles property previously created in order to get an instance of the ArticlesProvider class, but appended to this is the GetArticleBody() method, and this is where I get confused. The ArticlesProvider class defines this method as an abstract method, the SqlArticlesProvider class provides the concrete implementation of this method BUT how is it possible to access this implemetation from the ArticlesProvider class? Remember that when we got the provider type we cast it into its parent ArticlesProvider class (as shown in the first property above) so it is no longer a SqlArticlesProvider instance, so how can we access the SqlArticlesProvider methods like this?

Hope someone can help.

Thanks
  #2 (permalink)  
Old May 3rd, 2007, 03:56 PM
Friend of Wrox
 
Join Date: Mar 2007
Location: Creetown, UK
Posts: 488
Thanks: 2
Thanked 11 Times in 10 Posts
Default

crazy - you delve too deeply :)

i'll take a look and 'hopefully' come up with something short of bullsh$t in the coming days!! i have to admit, i've naively accepted the casting and delegation and followed the factory pattern methodology, without too much question (in fact, i've been using the devilish tool from http://www.flixon.com which is coming in to it's own now. this tool takes a table def and creates the full dal and bll code as per the beerhouse standards - it's excellent. i'm just ironing out a few wrinkles with the author as we speak and hopefully a v1 is imminent)

as i said - i'll take a look and might even engage the old brain irrespective

jimi

http://www.jamestollan.com
  #3 (permalink)  
Old May 3rd, 2007, 04:04 PM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

crazy - you delve too deeply :)

This is probably why I'm crazy!!!

Thanks jimi, I would appreciate any help, I don't like getting stumped like this!!!

Thanks

  #4 (permalink)  
Old May 4th, 2007, 12:39 PM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey crazy,

You may have noticed that ArticlesProvider is an ABSTRACT class. That means that you can't actually create an instance of it.

Take this syntax:

ArticlesProvider ap = FactoryObj.CreateSqlArticlesProvider()

ap is NOT of type ArticlesProvider, the declaration ensures only that ap is of a type that INHERITS from ArticlesProvider, in this case SqlArticlesProvider.

So, what's the big deal? Let's re-visit that line in the Instance property:

instance = (ArticlesProvider)Activator.CreateInstance(Type.Ge tType(Globals.Settings.Articles.ProviderType));

This cast ensures that the Activator creates an object that inherits from ArticlesProvider. The ProviderType is declared in our web.config, but what if we didn't want to use Sql Server anymore, and we needed to migrate to MySQL?

After creating a section in the DAL whose only purpose is to shuttle data back and forth between MySQL (the MySQL Client, if you will), we could begin referencing our new MySQL database simply by changing the ProviderType in the web.config!

If the data access code wasn't written this way, we would have polluted our entire DAL with SQL Server specific code. To migrate, you would have needed to go through all your code looking for SQL Server connections, commands, parameters, etc... and change them over to MySQL objects. I've been there before, it's not fun.

This way, we create a specific client for each data provider we want to support in the DAL, update the web.config and we're done.

There's a better explanation of the Provider Model here:

http://aspnet.4guysfromrolla.com/dem.../101905-1.aspx

Hope that helps,

C.
  #5 (permalink)  
Old May 4th, 2007, 02:47 PM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Chris,

Thanks for your response. I'm still a little confused though. I fully appreciate the reason for placing the provider type in the web.config file, but it's still that cast that throws me.

If the ArticleProvider class wasn't declared as abstract would the cast produce an ArticleProvider instance instead? I suppose I'm asking if a cast acts differently depending on if the parent class is abstract or not? If it isn't an abstract class then the cast would return an instance of ArticleProvider but when it is an abstract class a cast simply indicates that the type inherits from the type specified in the cast, in this case ArticleProvider. Is that correct?

Thanks for your help.


  #6 (permalink)  
Old May 5th, 2007, 12:37 PM
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi crazy,

You can't ever create an instance of ArticlesProvider - it's an abstract class. It's purpose is to ensure that all types that inherit from it implement certain properties and methods.

As for the cast, I'll refer to MSDN documentation for Activator.CreateInstance():

public static Object CreateInstance (Type type)

Note the return type - Object! We actually need our provider to be of a type that inherits from ArticlesProvider (that way we can access the inherited methods). Since CreateInstance returns Object, we need to cast it to a type that inherits from ArticlesProvider (In TBH, that's the SqlArticlesProvider in web.config, but could be any provider for which you want to support).

This can be confusing at first, and I hope I haven't muddied the waters even further. It's an exceptionally powerful paradigm, and if you haven't read the article I sent you, I'd suggest having a read of it. This stuff will make more sense.

Chris

  #7 (permalink)  
Old May 5th, 2007, 07:09 PM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Chris,

Thanks again for your response but I'm afraid I'm still not quite there yet. I fully appreciate and understand that you can't create an instance of the ArticlesProvider class. I now understand that the CreateInstance() method returns an object object rather than an SqlArticleProvider object which is why we'd need to do a cast (I should have checked this, silly me) but that cast is still an ArticlesProvider, the Instance property even returns an ArticlesProvider but as you say you can't do this as you can't create an instance of ArticlesProvider. So how does this cast translate as an SqlArticlesProvider instance when the cast is clearly to the base class ArticlesProvider? I know we wouldn't want to cast to a SqlArticlesProvider as that would be of no use if someone used a different backend, but somewhere this Instance property must be returning an SqlArticlesProvider instance or we wouldn't be able to access its members. Where is this happening and how?

I read the article but it didn't explain anything about this type of cast.

Thanks for all your help so far, I'll understand if you've lost patience with me.

Crazy
  #8 (permalink)  
Old May 5th, 2007, 09:27 PM
Authorized User
 
Join Date: Sep 2003
Location: , , .
Posts: 19
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Chris

Not to worry now I've worked it out. I had to go back to basics and do some inheritance testing before I got it. Thanks for all your help, you definitely got me thinking and on the right path.

Cheers
Crazy

  #9 (permalink)  
Old May 5th, 2007, 10:13 PM
Authorized User
 
Join Date: May 2006
Location: , , .
Posts: 99
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi Crazy,

It doesn't matter if the object instance is cast as an ArticlesProvider. The instance is still a SqlArticlesProvider instance.

  #10 (permalink)  
Old March 7th, 2008, 01:10 PM
Authorized User
 
Join Date: Sep 2006
Location: , , .
Posts: 73
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have recently been introduced to Spring and Spring.NET, and so I now referred back to TBH's factory implementation with Site Providers. It got me thinking - how does Microsoft's provider model fit with Spring? How easy or difficult would it be to combine TBH with Spring, and would it make sense to do this? Could someone familiar with Spring please comment on this?

Alex

Alex

- TheBeerHouse Mods Repository
http://www.sashka.com/TheBeerHouse/thebeerhouse.html
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SiteProvider Reza BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 9 September 7th, 2008 04:04 PM
SiteProvider Question zmijasu2k3 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 August 2nd, 2006 10:03 PM
Chapter - Struts - Traditional or Model1- Pls help ipatchu JSP Basics 0 November 18th, 2003 03:06 AM
dropdownlist question... pls help life_s Ng ASP.NET 1.0 and 1.1 Basics 2 September 26th, 2003 05:11 AM



All times are GMT -4. The time now is 08:14 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.