Click here to Skip to main content
16,008,183 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
Questionpatterns and practices Pin
Member 39813665-Mar-09 19:55
Member 39813665-Mar-09 19:55 
AnswerRe: patterns and practices Pin
Pete O'Hanlon5-Mar-09 23:17
mvePete O'Hanlon5-Mar-09 23:17 
AnswerRe: patterns and practices Pin
Curtis Schlak.6-Mar-09 1:52
Curtis Schlak.6-Mar-09 1:52 
AnswerRe: patterns and practices Pin
Mark Graham9-Mar-09 1:45
Mark Graham9-Mar-09 1:45 
Question(Windows CE 5.0) Emulator Pin
saksp5-Mar-09 19:34
saksp5-Mar-09 19:34 
Questionproblem in executing the dynamically refered dll in MMC Pin
renuga52985-Mar-09 18:50
renuga52985-Mar-09 18:50 
QuestionHelp needed for bluetooth application... Pin
Jain Vijay5-Mar-09 18:46
Jain Vijay5-Mar-09 18:46 
QuestionService/Object Locater Pin
Cybernate5-Mar-09 10:42
Cybernate5-Mar-09 10:42 
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
  }
<pre>

However I am not sure about any performance implications of using the Activator.CreatInstance vs construcotr.Invoke.
Can any one please guide me or comment on the above class? 

<div class="ForumSig">Regards,
Chandu</div>

JokeRe: Service/Object Locater Pin
Manish Sharma 20075-Mar-09 14:32
Manish Sharma 20075-Mar-09 14:32 
GeneralRe: Service/Object Locater Pin
Cybernate5-Mar-09 14:41
Cybernate5-Mar-09 14:41 
GeneralRe: Service/Object Locater Pin
Curtis Schlak.6-Mar-09 9:39
Curtis Schlak.6-Mar-09 9:39 
GeneralRe: Service/Object Locater Pin
Cybernate6-Mar-09 9:44
Cybernate6-Mar-09 9:44 
QuestionRDP Client Pin
vineeshV4-Mar-09 22:14
vineeshV4-Mar-09 22:14 
AnswerRe: RDP Client Pin
Dave Kreskowiak5-Mar-09 3:53
mveDave Kreskowiak5-Mar-09 3:53 
QuestionCreating one file from all the files created using 'Publish' Pin
shabya4-Mar-09 19:45
shabya4-Mar-09 19:45 
AnswerRe: Creating one file from all the files created using 'Publish' Pin
Curtis Schlak.5-Mar-09 9:45
Curtis Schlak.5-Mar-09 9:45 
GeneralRe: Creating one file from all the files created using 'Publish' Pin
shabya5-Mar-09 19:44
shabya5-Mar-09 19:44 
GeneralRe: Creating one file from all the files created using 'Publish' Pin
Eddy Vluggen6-Mar-09 1:45
professionalEddy Vluggen6-Mar-09 1:45 
GeneralRe: Creating one file from all the files created using 'Publish' Pin
Curtis Schlak.6-Mar-09 1:50
Curtis Schlak.6-Mar-09 1:50 
GeneralRe: Creating one file from all the files created using 'Publish' Pin
Eddy Vluggen6-Mar-09 2:33
professionalEddy Vluggen6-Mar-09 2:33 
GeneralRe: Creating one file from all the files created using 'Publish' Pin
Curtis Schlak.6-Mar-09 2:37
Curtis Schlak.6-Mar-09 2:37 
GeneralRe: Creating one file from all the files created using 'Publish' Pin
Eddy Vluggen6-Mar-09 3:06
professionalEddy Vluggen6-Mar-09 3:06 
AnswerRe: Creating one file from all the files created using 'Publish' Pin
Curtis Schlak.6-Mar-09 1:48
Curtis Schlak.6-Mar-09 1:48 
QuestionAxWebBrowser - Customizing Print Content Pin
CoolCoder_New4-Mar-09 19:11
CoolCoder_New4-Mar-09 19:11 
QuestionPing Using UDP Pin
tiger08064-Mar-09 4:58
tiger08064-Mar-09 4:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.