Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version
Go to top

Dynamic Binding Using the Factory Pattern

, 1 Nov 2009
Using the Factory design pattern to hide dynamic binding and use configuration strings to determine which classes should be instantiated.
/*************************************************************************
 *
 * Copyright 2009 Richard (Rick) Marvin Wycoff, All Rights Reserved
 * DO NOT ALTER OR REMOVE THIS COPYRIGHT NOTICE.
 *
 * NOTICE: You are permitted to use, modify, and distribute this file in 
 * accordance with the terms of the license agreement accompanying it.  
 * Any existing copyright or authorship information in any given source
 * file must remain intact.
 *
 **************************************************************************/

using System;

namespace Wycoff.Activation
{
    /// <summary>
    /// Creates a .NET Server Activated Object.
    /// </summary>
    public class SaoActivator : Locator.ILocatorActivator
    {
        /// <summary>
        /// Can be used by an Activator to initialize an object that does
        /// not directly support c'tor parameters.
        /// </summary>
        public interface IInitializer
        {
            /// <summary>
            /// Used to initialized a new object created with the default
            /// c'tor.
            /// </summary>
            /// <param name="arg">
            /// String representation of the argument(s)
            /// </param>
            void Intialize(string arg);
        }

        #region ILocatorActivator Members

        /// <summary>
        /// Create a Server Activated Object
        /// </summary>
        /// <param name="type">
        /// The type of the object being requested.
        /// </param>
        /// <param name="uri">
        /// Uri string in the format:  
        /// tcp://host[:port]/TheService[?InitializationString]<br />
        /// [ ] - means optional
        /// </param>
        /// <exception cref="ArgumentException">
        /// Thrown when the Uri includes and initialization parameter but
        /// the remote object does not support the IInitializer interface.
        /// </exception>
        /// <returns>
        /// Proxy to the remote object.
        /// </returns>
        public object Activate(Type type, Uri uri)
        {
            // if an initializatin parameter has been provided, it must be
            // stripped off before calling Activator.GetObject
            string realUri = (uri.Query.Length == 0) ?
                uri.AbsoluteUri :
                uri.AbsoluteUri.Substring(0, uri.AbsoluteUri.Length - uri.Query.Length);

            // Create the object
            object response = Activator.GetObject(type, realUri);

            // If an initiaization parameter has been provided, process it.
            if (uri.Query.Length > 0)
            {
                // if necessary, remove the "?"
                string initString = Uri.UnescapeDataString(uri.Query);
                if ('?' == initString[0])
                {
                    initString = initString.Remove(0, 1);
                }

                IInitializer initializer = response as IInitializer;

                if (null == initializer)
                {
                    // the user expected IInitializer support
                    throw new ArgumentException(initString);
                }
                else
                {
                    initializer.Intialize(initString);
                }
            }

            return response;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Richard (Rick) Wycoff
Software Developer (Senior)
United States United States
Rick's software passion is developing "invisible” software components, the kind that no one ever thinks about because they just do what they are supposed to do. He believes in standards, design patterns, and designing components for extensibility and reuse.

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 1 Nov 2009
Article Copyright 2009 by Richard (Rick) Wycoff
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid