Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Service/Object Locater

Service/Object Locater

Scheduled Pinned Locked Moved .NET (Core and Framework)
regex
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cybernate
    wrote on last edited by
    #1

    Hi, As a part of one of my project implementation I was keen on using a centralized object creator along with the abstract fatory pattern. As a part of it I have written the following class to solve the purpose of creating an object given an interface:

    public class SimpleServiceLocater : ProviderBase, IServiceLocater
    {
    Dictionary<string, ConstructorInfo> typesDictionary = new Dictionary<string, ConstructorInfo>();

    //Utils
    
    private object\[\] defaultParamsArray = new object\[\] { };
    private Type\[\] defaultTypesArray = new Type\[\] { };
    
    //private object\[\] defaultParamsArray = null;
    //private Type\[\] defaultTypesArray = null;
    
    
    public override void Initialize(string name, NameValueCollection config)
    {
      base.Initialize(name, config);
    }
    
    public override string Name
    {
      get
      {
        return base.Name;
      }
    }
    
    #region IServiceLocater Members
    
    public virtual bool Register<IT, CT>() where CT : IT, new()
    {
      return this.Register<IT, CT>("");
    }
    
    public virtual bool Register<IT, CT>(string Name) where CT : IT, new()
    {
      ConstructorInfo t = null;
      if (!typesDictionary.TryGetValue(typeof(IT).FullName + Name, out t))
      {
        t = null;
      }
      
      if(t == null)
      {
        this.Unregister<IT>(Name);
      }
      typesDictionary.Add(typeof(IT).FullName + Name, typeof(CT).GetConstructor(defaultTypesArray));
      return true;
    }
    
    public virtual IT Resolve<IT>() where IT : class
    {
      return this.Resolve<IT>("");
    }
    
    public virtual IT Resolve<IT>(string Name) where IT : class
    {
      ConstructorInfo t = null;
      if (!typesDictionary.TryGetValue(typeof(IT).FullName + Name, out t))
      {
        t = null;
      }
      
      if (t == null)
      {
        return default(IT);
      }
      else
      {
        //return Activator.CreateInstance(t) as IT; 
        return t.Invoke(defaultParamsArray) as IT; 
      }
    }
    
    public virtual bool Unregister<IT>()
    {
      return this.Unregister<IT>("");
    }
    
    public virtual bool Unregister<IT>(string Name)
    {
      return typesDictionary.Remove(typeof(IT).FullName + Name);
    }
    
    #endregion
    
    #region IDisposable Members
    
    public void Dispose()
    {
      this.typesDictionary.Clear();
    }
    
    #endregion
    

    }

    However I am not sure ab

    M 1 Reply Last reply
    0
    • C Cybernate

      Hi, As a part of one of my project implementation I was keen on using a centralized object creator along with the abstract fatory pattern. As a part of it I have written the following class to solve the purpose of creating an object given an interface:

      public class SimpleServiceLocater : ProviderBase, IServiceLocater
      {
      Dictionary<string, ConstructorInfo> typesDictionary = new Dictionary<string, ConstructorInfo>();

      //Utils
      
      private object\[\] defaultParamsArray = new object\[\] { };
      private Type\[\] defaultTypesArray = new Type\[\] { };
      
      //private object\[\] defaultParamsArray = null;
      //private Type\[\] defaultTypesArray = null;
      
      
      public override void Initialize(string name, NameValueCollection config)
      {
        base.Initialize(name, config);
      }
      
      public override string Name
      {
        get
        {
          return base.Name;
        }
      }
      
      #region IServiceLocater Members
      
      public virtual bool Register<IT, CT>() where CT : IT, new()
      {
        return this.Register<IT, CT>("");
      }
      
      public virtual bool Register<IT, CT>(string Name) where CT : IT, new()
      {
        ConstructorInfo t = null;
        if (!typesDictionary.TryGetValue(typeof(IT).FullName + Name, out t))
        {
          t = null;
        }
        
        if(t == null)
        {
          this.Unregister<IT>(Name);
        }
        typesDictionary.Add(typeof(IT).FullName + Name, typeof(CT).GetConstructor(defaultTypesArray));
        return true;
      }
      
      public virtual IT Resolve<IT>() where IT : class
      {
        return this.Resolve<IT>("");
      }
      
      public virtual IT Resolve<IT>(string Name) where IT : class
      {
        ConstructorInfo t = null;
        if (!typesDictionary.TryGetValue(typeof(IT).FullName + Name, out t))
        {
          t = null;
        }
        
        if (t == null)
        {
          return default(IT);
        }
        else
        {
          //return Activator.CreateInstance(t) as IT; 
          return t.Invoke(defaultParamsArray) as IT; 
        }
      }
      
      public virtual bool Unregister<IT>()
      {
        return this.Unregister<IT>("");
      }
      
      public virtual bool Unregister<IT>(string Name)
      {
        return typesDictionary.Remove(typeof(IT).FullName + Name);
      }
      
      #endregion
      
      #region IDisposable Members
      
      public void Dispose()
      {
        this.typesDictionary.Clear();
      }
      
      #endregion
      

      }

      However I am not sure ab

      M Offline
      M Offline
      Manish Sharma 2007
      wrote on last edited by
      #2

      This is so basic.

      Manish.

      C 1 Reply Last reply
      0
      • M Manish Sharma 2007

        This is so basic.

        Manish.

        C Offline
        C Offline
        Cybernate
        wrote on last edited by
        #3

        Agreed. I am interested in knowing the downside of using Activator.Createinstance vs Constructor.Invoke and also about do's n dont's while using them.

        Regards, Chandra V

        C 1 Reply Last reply
        0
        • C Cybernate

          Agreed. I am interested in knowing the downside of using Activator.Createinstance vs Constructor.Invoke and also about do's n dont's while using them.

          Regards, Chandra V

          C Offline
          C Offline
          Curtis Schlak
          wrote on last edited by
          #4

          When I had that same question, this blog post by the amazing Haibo Luo answered it quite well. Activator.CreateInstance and beyond[^] I hope you enjoy it.

          "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

          C 1 Reply Last reply
          0
          • C Curtis Schlak

            When I had that same question, this blog post by the amazing Haibo Luo answered it quite well. Activator.CreateInstance and beyond[^] I hope you enjoy it.

            "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

            C Offline
            C Offline
            Cybernate
            wrote on last edited by
            #5

            Thanks will go over it and let you know my thoughts.

            Regards, Cybernate

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups