Click here to Skip to main content
15,896,500 members
Articles / Programming Languages / C#

Dynamic Binding Using the Factory Pattern

Rate me:
Please Sign up or sign in to vote.
3.00/5 (1 vote)
1 Nov 2009CPOL11 min read 26K   180   16  
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;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;

using Wycoff.Activation;
using Wycoff.Shared;

namespace Wycoff.SoaHost
{
    /// <summary>
    /// Main class for the Server Activated Object host application
    /// </summary>
    class Program
    {
        /// <summary>
        /// ITheService implementation that will be served by this process.
        /// This also implements the SaoActivator.IInitializer inteface so
        /// the object an be initialized.
        /// </summary>
        public class SoaTheService : MarshalByRefObject, ITheService, SaoActivator.IInitializer
        {
            /// <summary>
            /// Salutation
            /// </summary>
            private string _salutation = "Hello ";

            #region ITheService Members

            /// <summary>
            /// ITheService implementation method.
            /// </summary>
            /// <param name="name">
            /// Name of person to which the salutation should be addressed
            /// </param>
            /// <returns>
            /// Personalized salutation
            /// </returns>
            public string Hello(string name)
            {
                return string.Format("{0} {1}", _salutation, name);
            }

            #endregion

            #region IInitializer Members

            /// <summary>
            /// SaoActivator.IInitializer interface implementation to allow
            /// object initialization.
            /// </summary>
            /// <param name="arg">
            /// Salutation
            /// </param>
            public void Intialize(string arg)
            {
                _salutation = arg;
            }

            #endregion
        }

        /// <summary>
        /// Static main for the SOA host application
        /// </summary>
        /// <param name="args">
        /// Not used
        /// </param>
        static void Main(string[] args)
        {
            // Register our implementation to be remotely available
            TcpServerChannel serverChannel = new TcpServerChannel(9000);
            ChannelServices.RegisterChannel(serverChannel, false);

            RemotingConfiguration.RegisterWellKnownServiceType(typeof(SoaTheService),
                "TheService",
                WellKnownObjectMode.Singleton);

            // Keep running until we are done
            Console.WriteLine("SOA service ready.  Press [ENTER] to exit.");
            Console.ReadLine();

            // Unregister the channel to clean up
            ChannelServices.UnregisterChannel(serverChannel);
        }
    }
}

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)


Written By
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.

Comments and Discussions