Wrox Programmer Forums
|
BOOK: Professional Refactoring in C# & ASP.NET
This is the forum to discuss the Wrox book Professional Refactoring in C# & ASP.NET by Danijel Arsenovski; ISBN: 9780470434529
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional Refactoring in C# & ASP.NET 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 September 30th, 2009, 09:51 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default Abstract Factory With Reflection

In Chapter 12, page 375, the Author mentions an option in which one could instantiate a factory by means of reflection.

In my opinion this is preferable, as it adheres more closely to the Open-Closed principle. This way, the DataProviderFactory remains open for extension, but closed for modification.

In case anyone is interested on how that works, I have attached a solution (.ZIP file) which shows the implementation.

About the example, please note:

1. A default implementation is provided, so that OleDbProviderFactory is used if no provider is specified.

2. The DataProviderFactory class is implemented as a singleton. A static "Instance" property is used to ensure that no more than one provider instance can ever exist.

3. To enforce the singleton behavior, the three concrete providers have protected constructors in place, so that they cannot be instantiated in any other way except through the Instance property mentioned above. In other words, it prevents you from doing this:

DataProviderFactory factory = new MsSqlProviderFactory();

Enjoy.
Attached Files
File Type: zip AbstractFactoryWithReflection.zip (13.0 KB, 112 views)
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}

Last edited by Lee Dumond; October 1st, 2009 at 04:00 PM.. Reason: edited: upload a revised sample





Similar Threads
Thread Thread Starter Forum Replies Last Post
about factory tyc ADO.NET 2 August 29th, 2008 06:53 PM
Abstract classes guppyheart C# 1 September 9th, 2003 05:23 PM





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