禾青谷

Practice ,Imitate and Insistance makes perfect

用户操作
[即时聊天] [发私信] [加为好友]
CrystalHareHusID:CrystalHareHus
10594次访问,排名10520(-5)好友0人,关注者1
CrystalHareHus的文章
原创 19 篇
翻译 0 篇
转载 1 篇
评论 24 篇
最近评论
文章分类
收藏
    相册
    友情链接
    Computer Graphics Changes Your Life
    ljgame
    寒星轩
    存档
    软件项目交易
    订阅我的博客
    XML聚合  FeedSky
    订阅到鲜果
    订阅到Google
    订阅到抓虾
    订阅到BlogLines
    订阅到Yahoo
    订阅到GouGou
    订阅到飞鸽
    订阅到Rojo
    订阅到newsgator
    订阅到netvibes

    原创 Developing Application Frameworks in .Net--(Note2, Configuration/Class Factory)收藏

    新一篇: Developing Application Frameworks in .Net --- (Note 3 Cach) | 旧一篇: English Practice ___ New Concept English 3(11-20)

    Configuration / Remoting  -----> Class Factory

    [ Configuration in C# ]
        COM components used the registry to configure components. Configuration of .NET applications is done by using configuration files. With registry configurations, an xcopy-deployment is not possible. The configuration files use XML syntax to specify startup and runtime settings for applications.

    Startup settings
    Runtime settings
    Remoting settings
    Security settings
    ....................... all are declared defaultly in < machine.config >
    ---- Application configuration files
         [web.config] ---> for web application
         [*.exe.config]--> for executable application <add app.config in startup project>

    ---- Machine configuration files
    ---- publisher policy files

    <appSettings>
        <add key = "DatabaseConnection" value = "Initial Catalog = Northwind; Data Source = localhost;"/>
    </appSettings>

    CLR affords:
    System.Configuration.ConfigurationSettings.AppSettings["DatabaseConnection"];

    All configuration information are read by implementation of IConfigurationSectionHandler which has only one method to override
    object Create ( object parent, object configContext, XmlNode section)

    object infor = ConfigurationSettings.GetConfig("sectionName1");
    ----- look for section named sectionName1
    ----- create handler object by type information with reflection mechanism
    ----- CLR call the create method of the object created just now
    ----- The third parameter XmlNode section contains the detail info of the section.
    ----- Parse the xml node and return an object
    *** CLR only instantiate the handler once. ***

    Four handler classes CLR affords:
    System.Configuration.DictionarySectionHandler     return Dictionary object, key/value
    System.Configuration.IgnoreSectionHandler         Ignore this type, return NULL
    System.Configuration.NameValueSectionHandler   return NameValueCollection object
    System.Configuration.SingleTagSectionHandler     retrun HashTable object, the data of attributes

    <configSections>
        <section name = "lunch" type = "System.Configuration.SingleTagSectionHandler, System">
    </configSections>
    ....
    <lunch soup = 'eggdrop' soda = 'coke' dish = 'beef'/>
    ....

    HashTable luchInfo = (HashTable)ConfigurationSettings.GetConfig("lunch");
    string soup = lunchInfo["soup"].ToString();
    string soda = lunchInfo["soda "].ToString();
    string dish  =  lunchInfo["dish"].ToString();

    Encapsulate information and use Intellisense functionality
    Handler class <Implement IConfigurationSectionHandler Interface>---> Configuration Manager class <constructor with XMLNode>----> configuration class <Property or Field to get settings>
    public class ConfigurationHandler  : System.Configuration.IConfigurationSectionHandler
        
    {
            
    public object Create(Object parent, object configContext, XmlNode section)
            
    {
                Type type 
    = System.Type.GetType(section.Attributes["type"].Value);
                
    object[] parameters = {section};
                
    //call the configuration object's constructor
                object configObject = null;
                
    try
                
    {
                    configObject 
    = Activator.CreateInstance(type, parameters);
                }

                
    catch (Exception ex)
                
    {
                    
    string x = ex.Message;
                    
    return null;
                }

                
    return configObject;    
            }

        }


        
    /// Provides access to configuraiton object for the framework component
        public class ConfigurationManager
        
    {
            
    public SAF.Configuration.ClassFactoryConfiguration ClassFactoryConfig;
            
    private XmlNode configurationData;

            
    /// Initialize all the configuration objects accessible through this configuration manager.
            public  ConfigurationManager (XmlNode sections)
            
    {
                configurationData 
    = sections;
                ConfigurationAgentManager cam 
    = new ConfigurationAgentManager(configurationData);
                ClassFactoryConfig 
    = new ClassFactoryConfiguration(cam.GetData("SAF.ClassFactory"));
            }

        }


        
    public class ClassFactoryConfiguration
        
    {
            
    private XmlNode classFactoryXml;

            
    /// the constructor is called by the configuraiton manager
            public ClassFactoryConfiguration(XmlNode configData)
            
    {
                classFactoryXml 
    = configData;
            }


            
    /// retrieve information about a class stored in the SAF.ClassFactory section
            public XmlNode GetFactoryData(string name)
            
    {
                
    return classFactoryXml.SelectSingleNode("Class[@name='" + name + "']");
            }

        }

    The second purpose: Integeration setting --- Agent
    public class ConfigurationAgentManager
        
    {
            
    private XmlNode configurationData;
            
    public ConfigurationAgentManager(XmlNode configData){
                configurationData 
    = configData;
            }


            
    /// it return the Xml containing the configuraiton settings for a given key 
            public XmlNode GetData(string key){
                XmlNode result
    =null;
                XmlAttribute agentAttribute 
    =null;
                
    if (configurationData.SelectSingleNode(key) != null){
                    
    //check if there is agent defined for a particular section or key
                    
    //if there is, load the agent and make it retrieve the data
                    
    //otherwise, just load the data from the configuraiton file
                    agentAttribute = configurationData.SelectSingleNode(key).Attributes["ConfigurationAgent"];
                    
    if ( agentAttribute == null) {
                        result 
    = configurationData.SelectSingleNode(key);
                    }
    else {
                        
    //retrive the data using the agent
                        string data = GetAgent(agentAttribute.Value).GetConfigurationSetting();
                        XmlDocument xml 
    = new XmlDocument();
                        xml.LoadXml(data);
                        result 
    = (XmlNode)xml.DocumentElement;
                    }

                }

                
    return result;
            }


            
    /// the method load the agent using reflection and return an instance of agent  to the caller
            private IConfigurationAgent GetAgent(string agentName){
                XmlNode agentNode 
    = configurationData.SelectSingleNode("//Agent[@name ='" + agentName +  "']");
                Type type 
    = Type.GetType(agentNode.Attributes["type"].Value);
                IConfigurationAgent agent 
    = (IConfigurationAgent)Activator.CreateInstance(type,null);
                
    //Initialize method setup the agent object with the parameter information specified
                
    //in the file that is needed for the agent to do its job
                agent.Initialize(agentNode);
                
    return agent;
            }

        }


        
    /// Interface that each agent class must implement.its two methods are called by agent manager at runtime.
           public interface IConfigurationAgent
        
    {
            
    void Initialize(XmlNode xml);
            
    string GetConfigurationSetting();
        }

    .Net Remoting

    server activation
        ---- singleton
        ---- single call
    client activation
    <configuration>

        
    <configSections>
            
    <section name="Framework" type="SAF.Configuration.ConfigurationHandler,SAF.Configuration" />
            
    <section name="MyApplication" type="SAF.Configuration.ConfigurationHandler,SAF.Configuration" />
        
    </configSections>

            
    <!--Remoting-->
            
    <system.runtime.remoting>
        
    <application>
             
    <service>
                
    <wellknown 
                   mode
    ="Singleton" 
                   type
    ="TestConcreteFactory.ConcreteRemoteProductFactory,TestConcreteFactory" 
                   objectUri
    ="ClassFactory.rem" />
             
    </service>
             
    <channels>
                
    <channel ref="http" port="8989"/>
             
    </channels>
          
    </application>
        
    </system.runtime.remoting>

            
    <!--Class Factory-->
        
    <Framework type="SAF.Configuration.ConfigurationManager,SAF.Configuration">
            
    <SAF.ClassFactory>            <Class name="ProductFactory-A" type="TestConcreteFactory.ConcreteProductFactory,TestConcreteFactory" />            <Class name="ProductFactory-B" type="TestConcreteFactory.ConcreteNewProductFactory,TestConcreteFactory" />            <Class name="Remote-ProductFactory-C" location="http://localhost:8989/ClassFactory.rem"   type="TestConcreteFactory.ConcreteRemoteProductFactory,TestConcreteFactory" />
            
    </SAF.ClassFactory>
        
    </Framework>

            
    <!--Agent-->    <MyApplication type="Application.Configuration.AppConfigurationManager,Application.Configuration">
              
    <Application.Configuration>
                
    <ConfigurationAgent>
                    
    <Agent name = "WSAgent1" type="TestConfigurationAgent.ConfigurationWSAgent,TestConfigurationAgent">
                        
    <Parameters>
                            
    <Section>Application.MessageQueue</Section>
                            
    <Environment>QAEnvironment</Environment>
                        
    </Parameters>
                        
    <Url>http://localhost/ConfigurationData/ConfigurationService.asmx</Url>
                    </Agent>
                
    </ConfigurationAgent>
            
    </Application.Configuration>
        
    </MyApplication>

    </configuration>

    Class Factory and Reflection
                 MyClass classA = new MyClass();
                 classA.DoWork();

                
    using System.Reflection;
                Assembly assm 
    = Assembly.Load("TestApp");
                Type objType 
    = assm.GetType("TestApp.MyClass");
                
    object objInstance = Activator.CreateInstance(objType);
                objType.InvokeMember(
    "DoWork", BindingFlags.InvokeMethod, null, objInstance, null);

    public class ClassFactory
        
    {
            
    private ClassFactory(){}
            
    /// Called by the client to get an instance of the factory class
            public static object GetFactory(string factoryName)
            
    {
                
    object factory = null;
                ConfigurationManager cm 
    = (ConfigurationManager)ConfigurationSettings.GetConfig("Framework");
                ClassFactoryConfiguration cf
    =  cm.ClassFactoryConfig;
                XmlNode classFactoryData 
    = cf.GetFactoryData(factoryName);        
        
                
    //obtain the type information
                string type = classFactoryData.Attributes["type"].Value;
                Type t 
    = System.Type.GetType(type);
                
    //creat an instance of concrete class factory
                if (classFactoryData.Attributes["location"!= null)
                
    {
                    
    // Remoting
                    string location = classFactoryData.Attributes["location"].Value;
                    factory 
    = Activator.GetObject(t,location);
                }

                
    else
                
    {
                    factory 
    = Activator.CreateInstance(t,null);
                }
     
                
    return factory;
            }

        }

    发表于 @ 2007年01月09日 15:57:00|评论(loading...)|编辑

    新一篇: Developing Application Frameworks in .Net --- (Note 3 Cach) | 旧一篇: English Practice ___ New Concept English 3(11-20)

    评论:没有评论。

    发表评论  


    当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
    Csdn Blog version 3.1a
    Copyright © CrystalHareHus