Geeks With Blogs
Saqib Ullah BootStrapper Know How
Hello geeks after a long gap I come back with a very arousing blog. When ever you see .Net Framework or any Microsoft patterns & practices library configure, they allow the developer to load the external components through some configurations in Web.config or App.Config file and that will be later called by the framework at runtime. Ok let take an example of Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.ILogFormatter Interface that require to format a LogEntry in a way suitable for wire transmission and when you open your configuration file you will find the following key.
 
    <formatters>
      <add template="" type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null"
           name="Text Formatter" />
    </formatters>
 
What that mean, when Enterprise library going to write log it will automatically use the above specify TextFormatter for text logging. Microsoft Enterprise library has the provision for the developer to create custom formatter by implementing ILogFormatter Interface. 
 
Now you have an idea what I am going to implement. Let suppose I have a Theme Configuration management framework for my Windows application and my framework disclose some interfaces for custom implement like IGUIControl. 

namespace ThemeFramework
{
   public interface IGUIControl
   {
      void Render();
      void Draw();
   }
}
If you want to develop a control for my framework you need to implement the IGUIControl interface for creating GUI controls, so it’s also the responsibility of the developer to Implement IGUIControl and configure it so Theme framework get that assembly. Here I only explain the static factory function of my framework that load all define assemblies from configuration file. Generic knowledge is prerequisite. 

public static T[] LoadInstances<T>() where T : class
       {
           //You can use any other way read from configuration file.
           //It utilize the .net 2.0 feature. The main target is to get all assemblies names.
           Settings.Default.Reload();
           StringCollection TypeColl = Settings.Default.Types;
 
           List<T> components = new List<T>();
 
           foreach (string TypeInfo in TypeColl)
           {
               try
               {
                   string[] typeInfo = TypeInfo.Split(',');
                   string assemblyName = typeInfo[1];
                   string typeName = typeInfo[0];
 
                   if (string.IsNullOrEmpty(assemblyName) || string.IsNullOrEmpty(typeName))
                   {
                       continue; // invalid type info
                   }
                   assemblyName = assemblyName.Trim();
                   typeName = typeName.Trim();
 
                   string exeDir = Path.GetDirectoryName(Application.ExecutablePath);
                   if (!File.Exists(Path.Combine(exeDir, assemblyName + ".dll")))
                   {
                       continue; // invalid type information
                   }
 
                   Assembly assem = Assembly.Load(typeInfo[1].Trim());
                   // Dynamic instance creation
                   T instance = assem.CreateInstance(typeInfo[0].Trim()) as T;
                   if (instance != null)
                   {
                       components.Add(instance);
                   }
               }
               catch(Exception ex)                  
               { }
           }
           return components.ToArray();
       }
In preceding code I use Settings.setting class for loading assembly’s names, you can use any other provider to get all custom assemblies names from configuration files. After that I try to find the give assembly in the application execution folder and load it into generic List<T> array.  

IGUIControl[] dogs = FrameworkTest. LoadInstances<IGUIControl>();
         foreach (IGUIControl control in loadControls)
         {
             Control.Draw();
         }
Above code snippet load all assemblies that implement IGUIControl and call it Draw() method to draw the entry control.
 
Posted on Monday, July 28, 2008 7:42 PM OOPs , .Net | Back to top


Comments on this post: Allow your framework to load custom assemblies

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Saqib Ullah | Powered by: GeeksWithBlogs.net